Cannot programmatically set papersize in reportviewer

nsteele84 used Ask the Experts™

I have an rdlc report in Legal papersize. I have set the report properties to Legal. The problem I am having is when I load it in to the reportviewer  and go to print the pagesize under page setup is always set to letter so it has to be changed manually. I need to be able to set the page settings in reportviewer programmatically so I can switch between letter and legal sizes via the code.  I have tried various things but nothing seems to affect the page setup dialog. What is the correct procedure to do this. I am coding in


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT is not possible , you have to change it before loading the rdlc file, this is a ver old problem still present today in report viewer, luckily for you someone has managed to do it using a public class (c#)
RDLC Set paper size

Here is untested converted code using telerik online converter (C#->VB), beware that it is untested so please check it thoroughly before trying to use it
Class SurroundingClass
    Private m_currentPageIndex As Integer
    Private m_streams As IList(Of Stream)

    Private Function LoadSalesData() As DataTable
        Dim dataSet As DataSet = New DataSet()
        Return dataSet.Tables(0)
    End Function

    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 FileStream(name & "." & fileNameExtension, FileMode.Create)
        Return stream
    End Function

    Private Sub Export(ByVal report As LocalReport, ByVal dPageWidth As Double, ByVal dPageHeigth As Double)
        dPageWidth = Math.Round(dPageWidth, 2)
        dPageHeigth = Math.Round(dPageHeigth, 2)
        Dim deviceInfo As String = "<DeviceInfo>" & "  <OutputFormat>EMF</OutputFormat>" & "  <PageWidth>{0}in</PageWidth>" & "  <PageHeight>{1}in</PageHeight>" & "  <MarginTop>0.25in</MarginTop>" & "  <MarginLeft>0.25in</MarginLeft>" & "  <MarginRight>0.25in</MarginRight>" & "  <MarginBottom>0.25in</MarginBottom>" & "</DeviceInfo>"
        deviceInfo = String.Format(deviceInfo, CDataMange.GetDoubleSQLPretty(dPageWidth), CDataMange.GetDoubleSQLPretty(dPageHeigth))
        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
    End Sub

    Public Sub Print(ByVal report As LocalReport)
        Dim dlg As PrintDialog = New PrintDialog()
        Dim res As DialogResult = dlg.ShowDialog()
        If res = DialogResult.Cancel Then Return
        Me.Export(report, dlg.PrinterSettings.DefaultPageSettings.PaperSize.Width / 100.0, dlg.PrinterSettings.DefaultPageSettings.PaperSize.Height / 100.0)
        Me.m_currentPageIndex = 0
        If m_streams Is Nothing OrElse m_streams.Count = 0 Then Return
        Dim printDoc As PrintDocument = New PrintDocument()
        printDoc.PrinterSettings = dlg.PrinterSettings

        If Not printDoc.PrinterSettings.IsValid Then
            CErrorHandling.WriteError("The printer settings are not valid! ", "CPinting.Print")
            MessageBox.Show("Error! ", "", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If

        printDoc.PrintPage += New PrintPageEventHandler(AddressOf PrintPage)
    End Sub

    Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim pageImage As Metafile = New Metafile(m_streams(m_currentPageIndex))
        ev.Graphics.DrawImage(pageImage, 0, 0)
        m_currentPageIndex += 1
        ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
    End Sub

    Public Sub Dispose()
        If m_streams IsNot Nothing Then

            For Each stream As Stream In m_streams
        End If
    End Sub
End Class

Open in new window



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial