vb.net reportviewer render SSRS report print first page only

Tilhoo Prakash 0 Reputation points
2026-01-15T09:59:30.27+00:00

I have a windows form with a reportviewer component on it.
I have the following code in VB.Net to print a SSRS report directly to a printer but it prints only first page only.

    Try

        doc = New Printing.PrintDocument()

        AddHandler doc.PrintPage, AddressOf PrintPageHandler

        doc.DefaultPageSettings.Landscape = False

        Dim deviceInfo As String =

                            "<DeviceInfo>" &

            "<OutputFormat>emf</OutputFormat>" &

            "  <PageWidth>8.3in</PageWidth>" &

            "  <PageHeight>11.7in</PageHeight>" &

            "  <MarginTop>0.14in</MarginTop>" &

            "  <MarginLeft>0.22in</MarginLeft>" &

            "  <MarginRight>0.22in</MarginRight>" &

            "  <MarginBottom>0.14in</MarginBottom>" &

            "  <StartPage>0</StartPage>" &

            "  <EndPage>10</EndPage>" &

            "</DeviceInfo>"

        Dim warnings() As Microsoft.Reporting.WinForms.Warning

        Dim streamids() As String

        Dim mimeType As String, encoding As String, filenameExtension As String

        REM , path As String

        mimeType = "" : encoding = "" : filenameExtension = ""

        Dim data() As Byte

        data = ReportViewer1.ServerReport.Render("Image", deviceInfo, mimeType, encoding, filenameExtension, streamids, warnings)

        pages.Add(New Metafile(New MemoryStream(data)))

        For Each pageName As String In streamids

            data = ReportViewer1.ServerReport.RenderStream(“Image”, pageName, deviceInfo, mimeType, encoding)

            pages.Add(New Metafile(New MemoryStream(data)))

        Next

        doc.Print()

    Catch ex As Exception

Private Sub PrintPageHandler(ByVal sender As Object, ByVal e As PrintPageEventArgs)

    fnName = "PrintPageHandler"

    Try

        Dim page As Metafile = pages(pageIndex)

        pageIndex += 1

        Dim pWidth As Integer = 827

        Dim pHeight As Integer = 1100

        e.Graphics.DrawImage(page, 0, 0, pWidth, pHeight)

        e.HasMorePages = pageIndex < pages.Count

    Catch ex As Exception

This link https://stackoverflow.com/questions/1077266/ssrs-2008-export-report-to-image-only-exports-first-page refers to same problem but a potential solution is given in C# with partial code.

Grateful if the complete solution be given on VB.net preferably or C# please.

Thank you.





Developer technologies | .NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Adiba Khan 1,840 Reputation points Microsoft External Staff
    2026-01-15T11:17:46.1733333+00:00

    Thank you for reaching out. This is a known behavior when the SSRS report is rendered incorrectly or when page iteration is not handled properly in the PrintPage event.

    Root Cause

    The issue occurs because:

    • ServerReport.Render("Image", ...) returns only one EMF stream unless a stream callback is used.
    • PrintPage is fired once unless e.HasMorePages = True is correctly set.
    • pageIndex is not reset or incremented properly.
    • Page streams are not generated for all report pages.

    As a result, only the first page gets printed.

    Recommended Solution:

    Step 1: Declare global variables

    Private pages As New List(Of Metafile)
    Private pageIndex As Integar = 0
    

    Step 2: Render all pages using a stream callback

    This ensures every page of the report is rendered

    Private Function CreateStream( _
    	ByVal name As String, _
    	ByVal extension As String, _
    	ByVal encoding As System.Text.Encoding, _
    	ByVal mimetype As String, _
    	ByVal willSeek As Boolean) As IO.Stream
    
    	Dim stream As New IO.MemoryStream()
    	pages.Add(New Metafile(stream))
    	Return stream
    End function
    

    Step 3: Render report to EMF images

    Dim deviceInfo As String = 
    "<DeviceInfo>" & 
    " <OutputFormat>EMF</OutputFormat>" &
    " <PageWidth>8.3in</PageWidth>" & 
    " <PageHeight>11.7in</PageHeight>" & 
    " <MarginTop>0.14in</MarginTop>" & 
    " <MarginLeft>0.22in</MarginLeft>" & 
    " <MarginRight>0.22in</MarginRight>" & 
    " <MarginBottom>0.14in</MarginBottom>" & 
    "</DeviceInfo>"
    
    pages.Clear()
    
    ReportViewer1.ServerReport.Rendor(
    	"Image",
    	deviceInfo,
    	AddressOf CreateStream,
    	Nothing
    )
    For each page As Metafile in pages
    	page.GetHenhmetafile()
    Next
    
    

    Step 4: Print using PrintDocument

    pageIndex = 0
    Dim doc As New Printing.PrintDocument()
    AddHandler doc.printPage, AddressOf PrintPageHandler
    doc.Print()
    
    

    Step 5: Correct PrintPageHandler(Critical Fix)

     Private Sub PrintPageHandler(
    	ByVal sender As Object,
    	ByVal e As Printing.PrintPageEventArgs)
    
     Dim page As Metafile = pages(pageIndex)
            e.Graphics.DrawImage(
    			page,
    			e.PageBounds
    		)
    		pageIndex +=1
    		e.HasMorePages = (pageIndex < pages.Count)
    End Sub
    

    This ensures all pages are printed, not just the first one.

    Why This works

    • SSRS generates one EMF stream per page.
    • CreateStream captures all pages
    • HasMorePages correctly tells the printer to continue
    • PageIndex ensures sequential printing

    Please let us know if you require any further assistance we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer".


  2. Adiba Khan 1,840 Reputation points Microsoft External Staff
    2026-01-19T10:32:42.21+00:00

    Thank you for detailed follow up.

    Root Cause

    In serverReport, these are the only valid signatures:
    Valid Render overload

    ServerReport.Render(
    	format As String,
    	deviceInfo As String,
    	ByRef mimeType As String,
    	ByRef encoding As String,
    	ByRef fileNameExtension As String,
    	ByRef streams As String(),
    	ByRef warnings As Warning()
    ) As Byte()
    

    Valid RenderStream overload

    ServerReport.RenderStream
    (	format As String,
    	streamId As String,
    	deviceInfo As String,
    	ByRef mimeType As String,
    	ByRef encoding As String
    ) As Byte()
    

    CreateStream is NOT supported

    Missing fileNameExtension causes BC30455

    This is by design for SSRS

    Solution

    Step1: Global variables

    Private pages As New List(Of Metafile) 
    Private pageIndex As Integar = 0
    

    Step 2: DeviceInfo (IMPORTANT)

    Dim deviceInfo As String =  
    "<DeviceInfo>" &  
    " <OutputFormat>EMF</OutputFormat>" & 
    " <PageWidth>8.3in</PageWidth>" &  
    " <PageHeight>11.7in</PageHeight>" &  
    " <MarginTop>0.14in</MarginTop>" &  
    " <MarginLeft>0.22in</MarginLeft>" &  
    " <MarginRight>0.22in</MarginRight>" & 
    " <MarginBottom>0.14in</MarginBottom>" &  
    "</DeviceInfo>"
    

    Step 3: Render first page +get stream IDs

    Dim mimeType As String = "" 
    Dim encoding As String = ""
    Dim fileExt As String = ""
    Dim warnings() As Microsoft.Reporting.WinForms.Warning
    Dim streamIds() As String = Nothing
    
    pages.Clear()
    
    Dim firstPage As Byte() =
    ReportViewer1.ServerReport.Rendor( 		
    "Image", 		
    deviceInfo, 		
    mimeType, 		
    encoding, 		
    extension, 		
    streamIds, 		
    warnings ) 
    pages.Add(New Metafile(New IO.MemoryStream(firstPage)))
    	
    

    Step 4: Render remaining pages using RenderStream

    If streamIds IsNot Nothing Then
    	For Each streamId As String In streamIds
    		Dim pageBytes As Byte() =
    			ReportViewer1.ServerReport.RenderStream(
    					"Image",
    			 		deviceInfo,	
     			 		mimeType,
    			 		encoding, 		
    					) 		
    		pages.Add(New Metafile(New IO.MemoryStream(pageBytes))) 
    	Next
    End if
    		
    

    Step 5: Print all pages

    pageIndex = 0
    Dim doc As New Printing.PrintDocument()
    AddHandler doc.printPage, AddressOf PrintPageHandler
    doc.Print()
    

    Step 6: PrintPage handler (Do Not Modify)

    Private Sub PrintPageHandler(
    	sender As Object,
    	e As Printing.PrintPageEventArgs)
     Dim page As Metafile = pages(pageIndex)
            e.Graphics.DrawImage(
    			page,
    			e.PageBounds
    		)
    		pageIndex +=1
    		e.HasMorePages = (pageIndex < pages.Count)
    End Sub
    

    Please let us know if you require any further assistance we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer".


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.