I need to be able to print a report from report server on my own terms, so I have written some code to retrieve the report rendered as EMF and preview it, and then print it. The problem is that the PrintPage event of the printdocument fires too many times and causes an exception in the collection I am using to store the streams that each page is in.
When I use an if-conditional to keep the code from executing, I only get the second and third pages and not the first. If I do some fiddling in the debugger I can get it to print the first page if I remove the second and third page from the collection and only let it hit the first. (Does that make sense?)
Maybe somebody can take a look at my code and tell me if anything is wrong. Thanks
this is the gist, let me know if you need more.
Thanks, again
When I use an if-conditional to keep the code from executing, I only get the second and third pages and not the first. If I do some fiddling in the debugger I can get it to print the first page if I remove the second and third page from the collection and only let it hit the first. (Does that make sense?)
Maybe somebody can take a look at my code and tell me if anything is wrong. Thanks
Code:
Dim WithEvents reportDoc As Printing.PrintDocument
Dim printView As PrintPreviewDialog
Dim reportStreams As Generic.List(Of IO.MemoryStream)
Dim currentPage As Integer = 0
Dim streamIDs As String()
Private Sub RetrieveReport()
reportStreams = New Generic.List(Of IO.MemoryStream)
Dim devInfo As String
devInfo = _
"<DeviceInfo>" & _
" <OutputFormat>EMF</OutputFormat>" & _
" <StartPage>0</StartPage>" & _
" <PageWidth>8.5in</PageWidth>" & _
" <PageHeight>11in</PageHeight>" & _
" <MarginTop>0.5in</MarginTop>" & _
" <MarginLeft>0.5in</MarginLeft>" & _
" <MarginRight>0.5in</MarginRight>" & _
" <MarginBottom>0.5in</MarginBottom>" & _
"</DeviceInfo>"
'render the report so that I know how many pages
RV1.ServerReport.Render("Image", devInfo, "", "", "", streamIDs, Nothing)
Dim bt As Byte()
Dim pageCount As Integer = streamIDs.Length + 1
Dim count As Integer
For count = 1 To pageCount Step 1
devInfo = _
"<DeviceInfo>" & _
" <OutputFormat>EMF</OutputFormat>" & _
" <StartPage>" & count & "</StartPage>" & _
" <PageWidth>8.5in</PageWidth>" & _
" <PageHeight>11in</PageHeight>" & _
" <MarginTop>0.5in</MarginTop>" & _
" <MarginLeft>0.5in</MarginLeft>" & _
" <MarginRight>0.5in</MarginRight>" & _
" <MarginBottom>0.5in</MarginBottom>" & _
"</DeviceInfo>"
reportStreams.Add(New IO.MemoryStream(RV1.ServerReport.Render("Image", devInfo, "", "", "", Nothing, Nothing)))
Next
For Each stream As IO.MemoryStream In reportStreams
stream.Position = 0
Next
reportDoc = New Printing.PrintDocument
AddHandler reportDoc.PrintPage, AddressOf reportDoc_PrintPage
Dim reportpreview As PrintPreviewDialog = New PrintPreviewDialog
Dim settings As New Printing.PrinterSettings
settings.MaximumPage = pageCount
settings.MinimumPage = 1
settings.FromPage = 1
settings.ToPage = pageCount
settings.PrintRange = Printing.PrintRange.SomePages
reportDoc.PrinterSettings = settings
reportDoc.OriginAtMargins = False
reportpreview.Document = reportDoc
reportpreview.UseAntiAlias = True
reportpreview.ShowDialog(Me)
End Sub
Private Sub reportDoc_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles reportDoc.PrintPage
Dim pageImage As New System.Drawing.Imaging.Metafile(reportStreams(currentPage))
e.Graphics.DrawImage(pageImage, e.PageBounds)
currentPage += 1
e.HasMorePages = (currentPage < reportStreams.Count)
End Sub
this is the gist, let me know if you need more.
Thanks, again