Cannot programmatically set papersize in reportviewer

Hi,

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 vb.net.

Thanks

Nick
nsteele84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aranaCommented:
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()
        dataSet.ReadXml("data.xml")
        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)
        m_streams.Add(stream)
        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
        Next
    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])
            Return
        End If

        printDoc.PrintPage += New PrintPageEventHandler(AddressOf PrintPage)
        printDoc.Print()
    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
                stream.Close()
            Next
        End If
    End Sub
End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nsteele84Author Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.