הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, December 19, 2015 8:12 AM
I am trying to print a local report in winforms with custom paper size using the code
Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms
Public Class Demo
Implements IDisposable
Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Private Function LoadSalesData() As DataTable
' Create a new DataSet and read sales data file
' data.xml into the first DataTable.
Dim dataSet As New DataSet()
dataSet.ReadXml("..\..\data.xml")
Return dataSet.Tables(0)
End Function
' Routine to provide to the report renderer, in order to
' save an image for each page of the report.
Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
Dim stream As Stream = New MemoryStream()
m_streams.Add(stream)
Return stream
End Function
' Export the given report as an EMF (Enhanced Metafile) file.
Private Sub Export(ByVal report As LocalReport)
Dim deviceInfo As String = "<DeviceInfo>" & _
"<OutputFormat>EMF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth>" & _
"<PageHeight>11in</PageHeight>" & _
"<MarginTop>0.25in</MarginTop>" & _
"<MarginLeft>0.25in</MarginLeft>" & _
"<MarginRight>0.25in</MarginRight>" & _
"<MarginBottom>0.25in</MarginBottom>" & _
"</DeviceInfo>"
Dim warnings As Warning()
m_streams = New List(Of Stream)()
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
For Each stream As Stream In m_streams
stream.Position = 0
Next
End Sub
' Handler for PrintPageEvents
Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
' Adjust rectangular area with printer margins.
Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX), _
ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
ev.PageBounds.Width, _
ev.PageBounds.Height)
' Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect)
' Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect)
' Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex += 1
ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
End Sub
Private Sub Print()
If m_streams Is Nothing OrElse m_streams.Count = 0 Then
Throw New Exception("Error: no stream to print.")
End If
Dim printDoc As New PrintDocument()
If Not printDoc.PrinterSettings.IsValid Then
Throw New Exception("Error: cannot find the default printer.")
Else
AddHandler printDoc.PrintPage, AddressOf PrintPage
m_currentPageIndex = 0
printDoc.Print()
End If
End Sub
' Create a local report for Report.rdlc, load the data,
' export the report to an .emf file, and print it.
Private Sub Run()
Dim report As New LocalReport()
report.ReportPath = "..\..\Report.rdlc"
report.DataSources.Add(New ReportDataSource("Sales", LoadSalesData()))
Export(report)
Print()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
If m_streams IsNot Nothing Then
For Each stream As Stream In m_streams
stream.Close()
Next
m_streams = Nothing
End If
End Sub
Public Shared Sub Main(ByVal args As String())
Using demo As New Demo()
demo.Run()
End Using
End Sub
End Class
found in this page by changing the device info to this
Dim deviceInfo As String = "<DeviceInfo>" & _
"<OutputFormat>EMF</OutputFormat>" & _
"<PageWidth>3in</PageWidth>" & _
"<PageHeight>8in</PageHeight>" & _
"<MarginTop>0.05in</MarginTop>" & _
"<MarginLeft>0.05in</MarginLeft>" & _
"<MarginRight>0.05in</MarginRight>" & _
"<MarginBottom>0.05in</MarginBottom>" & _
"</DeviceInfo>"
Here is the output i got
The printed output, which is rendered using a pdf printer, is actually 8.5 x 11 instead of 3 x 8 that i defined, i just chopped some part at the bottom out.
The image is stretched, I dont know why this happens
Can anyone tell me what is wrong
Do you know we will see our dead loved ones again? John 5:25
All replies (20)
Saturday, December 19, 2015 1:44 PM
It appears the size of the report is something other than the size of the paper you have defined. You mention custom paper size and then show 8.5 x 11? What paper size is the report? Are the margins the correct size? You show:
"<MarginTop>0.05in</MarginTop>" & _
Should that be 0.5in instead of 0.05?
This thread shows an example maybe it will help.
If not maybe show all the code to reproduce the problem.
The report and paper (with margins included) must be the same size.
PS you show:
"<PageWidth>3in</PageWidth>" & _
"<PageHeight>8in</PageHeight>" & _
But you say the paper is 8.5 x 11 so shouldn't it be:
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
Also the margins on the printer are 1" by default so if you are using something else you need to set the printer margins to what you are using in the report ie 0.5 in.
Saturday, December 19, 2015 2:17 PM
I meant the printed document, that is the image, is 8.5 x 11 instead of 3 x 8.
I am printing to a pdf printer which converts printed data to pdf on the fly
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 3:55 PM
I meant the printed document, that is the image, is 8.5 x 11 instead of 3 x 8.
I am printing to a pdf printer which converts printed data to pdf on the fly
Do you know we will see our dead loved ones again? John 5:25
So why are you defining it as 3 x 8 in deviceInfo that you show above?
THe method you reference captures the drawing of the report to a metafile memory stream. Then it sends that emf stream to the printer. You define a size of 3 inches wide x 8 high and then draw that on 8.5 x 11 (not counting margins). So you are taking a 3 inch image and stretching it sideways to 8.5 inches (less margins). That is the problem? Change it to 8.5 x 11 and see what happens.
I cant test it as I dont have your RDLC report.
If you can show me how to make a simple report from code that we can all run then I can show you how to print it they way you want. See this question.
Saturday, December 19, 2015 4:00 PM
So why are you defining it as 3 x 8 in deviceInfo that you show above?
THe method you reference captures the drawing of the report to a metafile memory stream. Then it sends that emf stream to the printer. You define a size of 3 inches wide x 8 high and then draw that on 8.5 x 11 (not counting margins). So you are taking a 3 inch image and stretching it sideways to 8.5 inches (less margins). That is the problem? Change it to 8.5 x 11 and see what happens.
I cant test it as I dont have your RDLC report.
If you can show me how to make a simple report from code that we can all run then I can show you how to print it they way you want. See this question.
so how do i take a 3 x 8 and draw on the same paper 3 x 8 ?
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 4:06 PM
So why are you defining it as 3 x 8 in deviceInfo that you show above?
THe method you reference captures the drawing of the report to a metafile memory stream. Then it sends that emf stream to the printer. You define a size of 3 inches wide x 8 high and then draw that on 8.5 x 11 (not counting margins). So you are taking a 3 inch image and stretching it sideways to 8.5 inches (less margins). That is the problem? Change it to 8.5 x 11 and see what happens.
I cant test it as I dont have your RDLC report.
If you can show me how to make a simple report from code that we can all run then I can show you how to print it they way you want. See this question.
so how do i take a 3 x 8 and draw on the same paper 3 x 8 ?
Do you know we will see our dead loved ones again? John 5:25
Answer my questions and I will tell you.
Saturday, December 19, 2015 4:13 PM
Answer my questions and I will tell you.
I have a 3 x 8 inch report already designed, and i need to print programatically without preview. hence i defined the emf details as
"<PageWidth>3in</PageWidth>" & _
"<PageHeight>8in</PageHeight>" & _
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 5:20 PM
Answer my questions and I will tell you.
I have a 3 x 8 inch report already designed, and i need to print programatically without preview. hence i defined the emf details as
"<PageWidth>3in</PageWidth>" & _ "<PageHeight>8in</PageHeight>" & _
Do you know we will see our dead loved ones again? John 5:25
I dont want to play questions with you.
Is the printer set to 3 x 8 paper? How do you do that? Why do you say it is 8.5 x 11 paper? You are not making sense. The report and the paper have to be the same size. The printer must be set to the paper size and margins. How are you doing that?
You have only shown a small part of what does not work to you. I dont plan on wasting the day asking you questions of what you have and have not. Maybe show your printing code or something other than just stating "it does not work".
Saturday, December 19, 2015 5:28 PM
Answer my questions and I will tell you.
I have a 3 x 8 inch report already designed, and i need to print programatically without preview. hence i defined the emf details as
"<PageWidth>3in</PageWidth>" & _ "<PageHeight>8in</PageHeight>" & _
Do you know we will see our dead loved ones again? John 5:25
I dont want to play questions with you.
Is the printer set to 3 x 8 paper? How do you do that? Why do you say it is 8.5 x 11 paper? You are not making sense. The report and the paper have to be the same size. The printer must be set to the paper size and margins. How are you doing that?
You have only shown a small part of what does not work to you. I dont plan on wasting the day asking you questions of what you have and have not. Maybe show your printing code or something other than just stating "it does not work".
please see the code i am using in the question
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 6:01 PM
please see the code i am using in the question
Do you know we will see our dead loved ones again? John 5:25
I am not sure but try changing the size rectagle you print on the paper to 3 x 8.
Dim adjustedrect As New Rectangle(1, 1, 3, 8)
Show me what you get in the pdf.
Saturday, December 19, 2015 6:19 PM
please see the code i am using in the question
Do you know we will see our dead loved ones again? John 5:25
I am not sure but try changing the size rectagle you print on the paper to 3 x 8.
Dim adjustedrect As New Rectangle(1, 1, 3, 8) Show me what you get in the pdf.
I am sorry, the units may be in 100ths by default for the printer so you need
Dim adjustedrect As New Rectangle(100, 100, 300, 800)
for the size of the metafile rectangle on the paper.
Saturday, December 19, 2015 6:40 PM
please see the code i am using in the question
Do you know we will see our dead loved ones again? John 5:25
I am not sure but try changing the size rectagle you print on the paper to 3 x 8.
Dim adjustedrect As New Rectangle(1, 1, 3, 8)
Show me what you get in the pdf.
it prints on a standard 8 x 11 paper instead of 3 x 8, you can see the extra spaces beside and bottom
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 7:06 PM
please see the code i am using in the question
Do you know we will see our dead loved ones again? John 5:25
I am not sure but try changing the size rectagle you print on the paper to 3 x 8.
Dim adjustedrect As New Rectangle(1, 1, 3, 8)
Show me what you get in the pdf.
it prints on a standard 8 x 11 paper instead of 3 x 8, you can see the extra spaces beside and bottom
Do you know we will see our dead loved ones again? John 5:25
Well its looking better right? Now the report size is 3 x 8 and it is shown on 8.5 x 11 paper in the pdf. Correct?
Now you need to set the printer to the paper size you want. Is that the problem? I don't think you can set pdf to 3 x 8 but I could be wrong.
I ask once again, how are you setting the paper size? Its not happening in the code you show.
Look at the examples on this thread.
You set the paper type for the printer like this:
printdoc.DefaultPageSettings.PaperSource = _
printdoc.PrinterSettings.PaperSources.Item(papertype)
where paper type is the index for the paper size on that printer. It changes by the selected printer. See the example on the other thread. You need to set it to your 3 x 8 paper. I don't think you can for a pdf but I am not sure.
Saturday, December 19, 2015 7:45 PM
Here is an example that shows how to set the paper size. You will need to work it into what you are doing. I set the size to iso b6 and got a small size in the pdf made with bullzip pdf printer.
You will have to decide how to set the paper or let the user do it. You can also set the default printer to the paper size you want and on and on...
Option Strict On
Imports System.Drawing.Printing
Public Class Form7
Public WithEvents printdoc As PrintDocument = New PrintDocument
Public PrintPreviewDialog1 As New PrintPreviewDialog
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex <> -1 Then
printdoc.DefaultPageSettings.PaperSize = _
printdoc.PrinterSettings.PaperSizes.Item(ComboBox1.SelectedIndex)
PrintPreviewDialog1.ShowDialog()
Else
MsgBox("Please select a Paper Source.")
End If
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintPreviewDialog1.Document = printdoc
Dim pkSize As PaperSize
For i = 0 To printdoc.PrinterSettings.PaperSizes.Count - 1
pkSize = printdoc.PrinterSettings.PaperSizes.Item(i)
ComboBox1.Items.Add(pkSize)
Next
End Sub
Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
'this is called when printdoc.Print() is exectued
Try
Dim thislabel As String
Dim printFont As New Font("Arial", 11)
Dim dy As Integer = CInt(printFont.GetHeight(e.Graphics))
thislabel = "Customer Name"
e.Graphics.DrawString(thislabel, printFont, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y + dy, New StringFormat())
thislabel = "Customer Purchase"
e.Graphics.DrawString(thislabel, printFont, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y + dy * 3, New StringFormat())
Catch ex As Exception
MsgBox("Problem Printing Page" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
End Try
End Sub
End Class
Saturday, December 19, 2015 7:52 PM
Well its looking better right? Now the report size is 3 x 8 and it is shown on 8.5 x 11 paper in the pdf. Correct?
Now you need to set the printer to the paper size you want. Is that the problem? I don't think you can set pdf to 3 x 8 but I could be wrong.
I ask once again, how are you setting the paper size? Its not happening in the code you show.
Look at the examples on this thread.
You set the paper type for the printer like this:
printdoc.DefaultPageSettings.PaperSource = _
printdoc.PrinterSettings.PaperSources.Item(papertype)where paper type is the index for the paper size on that printer. It changes by the selected printer. See the example on the other thread. You need to set it to your 3 x 8 paper. I don't think you can for a pdf but I am not sure.
see how i am setting the paper size
Dim pprSize As PaperSize = New PaperSize("Receipt Paper", 300, 800)
pprSize.RawKind = PaperKind.Custom
Dim printDoc As New PrintDocument()
printDoc.PrinterSettings.PrinterName = _printer
printDoc.DocumentName = "POS Receipt"
printDoc.DefaultPageSettings.PaperSize = pprSize
printDoc.PrinterSettings.DefaultPageSettings.PaperSize = printDoc.DefaultPageSettings.Landscape = False
printDoc.DefaultPageSettings.Margins = New Margins(1, 1, 1, 1)
Do you know we will see our dead loved ones again? John 5:25
Saturday, December 19, 2015 8:54 PM
Well its looking better right? Now the report size is 3 x 8 and it is shown on 8.5 x 11 paper in the pdf. Correct?
Now you need to set the printer to the paper size you want. Is that the problem? I don't think you can set pdf to 3 x 8 but I could be wrong.
I ask once again, how are you setting the paper size? Its not happening in the code you show.
Look at the examples on this thread.
You set the paper type for the printer like this:
printdoc.DefaultPageSettings.PaperSource = _
printdoc.PrinterSettings.PaperSources.Item(papertype)where paper type is the index for the paper size on that printer. It changes by the selected printer. See the example on the other thread. You need to set it to your 3 x 8 paper. I don't think you can for a pdf but I am not sure.
see how i am setting the paper size
Dim pprSize As PaperSize = New PaperSize("Receipt Paper", 300, 800) pprSize.RawKind = PaperKind.Custom Dim printDoc As New PrintDocument() printDoc.PrinterSettings.PrinterName = _printer printDoc.DocumentName = "POS Receipt" printDoc.DefaultPageSettings.PaperSize = pprSize printDoc.PrinterSettings.DefaultPageSettings.PaperSize = printDoc.DefaultPageSettings.Landscape = False printDoc.DefaultPageSettings.Margins = New Margins(1, 1, 1, 1)
Do you know we will see our dead loved ones again? John 5:25
Ok I see. I cant get it to change the size using that code for custom size either.
I will see if I can find something I kind of remember this before with a custom set size. Maybe you have to set the papersource too or something...
Just to be clear, do you have paper that size you can already print on? Can you print on this paper with other code if so how does it select the paper? Or do you just make a pdf? If so how do you print the pdf?
Saturday, December 19, 2015 11:47 PM
Well here is one way to set a custom paper size. I am not sure it works for exactly what you need. But I made this pdf that appears to be 300 x 800. Click the example preview button to print (or you can leave the preview out with printdoc.print). You can work this into your code I think?
Option Strict On
Imports System.Drawing.Printing
Public Class Form7
Public WithEvents printdoc As PrintDocument = New PrintDocument
Public PrintPreviewDialog1 As New PrintPreviewDialog
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
printdoc.DefaultPageSettings.PaperSize = New PaperSize("Custom", 300, 800)
PrintPreviewDialog1.Document = printdoc
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
e.Graphics.DrawRectangle(Pens.Black, 1, 1, 300, 800)
e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString, New Font("arial", 12), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
End Sub
End Class
Sunday, December 20, 2015 3:54 AM
see how i am setting the paper size
Dim pprSize As PaperSize = New PaperSize("Receipt Paper", 300, 800) pprSize.RawKind = PaperKind.Custom Dim printDoc As New PrintDocument() printDoc.PrinterSettings.PrinterName = _printer printDoc.DocumentName = "POS Receipt" printDoc.DefaultPageSettings.PaperSize = pprSize printDoc.PrinterSettings.DefaultPageSettings.PaperSize = printDoc.DefaultPageSettings.Landscape = False printDoc.DefaultPageSettings.Margins = New Margins(1, 1, 1, 1)
Do you know we will see our dead loved ones again? John 5:25
As per this thread, try changing this:
printDoc.DefaultPageSettings.PaperSize = pprSize
to this
printDoc.DefaultPageSettings.PaperSize = CType(pprSize, PaperSize)
in your code.
Monday, December 21, 2015 6:00 AM
As per this thread, try changing this:
printDoc.DefaultPageSettings.PaperSize = pprSize
to this
printDoc.DefaultPageSettings.PaperSize = CType(pprSize, PaperSize)
in your code.
I need to print without showing the print preview dialog.
My solution words fin if i show the preview dialog.
I tested you solution without showing the printpreview dialog, here is what i got
You can see the 300 x 800 margin, being printed on big page
I need it to be rendered like this
I will try printing on a receipt printer to see if it doesn't complain about paper size too big
Do you know we will see our dead loved ones again? John 5:25
Monday, December 21, 2015 1:51 PM
Seems to work for me? I am using bullzip to make the pdf. What pdf converter do you use?
Public Class PrintingCustomSize
Private WithEvents Button1 As New Button With {.Parent = Me, .Location = New Point(100, 100), .Text = "Print"}
'how to set custom paper size with code
Public WithEvents printdoc As PrintDocument = New PrintDocument
Public PrintPreviewDialog1 As New PrintPreviewDialog
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = printdoc
'two ways to set size first just new papersize
'printdoc.DefaultPageSettings.PaperSize = New PaperSize("Receipt Paper", 300, 800)
'printdoc.Print()
'second define a size
Dim pprSize As PaperSize = New PaperSize("Receipt Paper", 300, 800)
pprSize.RawKind = PaperKind.Custom
'printDoc.PrinterSettings.PrinterName = _printer
printdoc.DocumentName = "POS Receipt"
printdoc.DefaultPageSettings.PaperSize = CType(pprSize, PaperSize)
printdoc.Print()
End Sub
Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
e.Graphics.DrawRectangle(Pens.Black, 1, 1, 300, 800)
e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString, New Font("arial", 12), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
End Sub
End Class
Monday, December 21, 2015 2:01 PM
As per this thread, try changing this:
printDoc.DefaultPageSettings.PaperSize = pprSize
to this
printDoc.DefaultPageSettings.PaperSize = CType(pprSize, PaperSize)
in your code.
I need to print without showing the print preview dialog.
My solution words fin if i show the preview dialog.
I tested you solution without showing the printpreview dialog, here is what i got
You can see the 300 x 800 margin, being printed on big page
I need it to be rendered like this
I will try printing on a receipt printer to see if it doesn't complain about paper size too big
Do you know we will see our dead loved ones again? John 5:25
How did you make this image? Is that your pdf?
How are you setting the printer? I have set my bullzip pdf printer as the default in windows devices and printers first.
What pdf printer are you using?
PS as you can see in my last example I have used your code and added ctype. I have commented out this:
'printDoc.PrinterSettings.PrinterName = _printer
and have set the printer by setting winzip pdf converter as the default printer in windows printer control panel.
How are you setting the printer?