Search code examples
vb.netwinformspdfreporting-servicesrdlc

ReportViewer.LocalReport.Render("PDF")


I am using Report Viewer for WinForms. The problem i am having is this: I have a form that contains a form which is used to view a local report which work fine, but when I try to render the same report as a PDF, it is cut-off, but in report viewer the same report renders a report on one page. When I render to PDF it cuts it off and the part of the report that was cut-off renders on a 2nd page. So in other words, part of the same report is on page 1, and 2nd half is on 2nd page in the PDF?

Code:

Private Function GetPDfReport() As String

    Dim parameters = Me.GetReportParms()

    Dim query = Me.GetReportQuery()

    Dim rView As Microsoft.Reporting.WinForms.ReportViewer = New Microsoft.Reporting.WinForms.ReportViewer
    rView.Dock = DockStyle.Fill
    rView.SetDisplayMode(DisplayMode.PrintLayout)

    Dim pnl As New Panel()
    pnl.Name = "pnlMain"
    pnl.Location = New System.Drawing.Point(0, 25)
    pnl.Size = New System.Drawing.Size(734, 478)

    pnl.Controls.Add(rView)

    Dim dbReader As New dbReader()
    Dim ds As DataSet = dbReader.DataSet(query)

    Dim rds As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables(0))

    rView.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
    rView.LocalReport.DataSources.Add(rds)
    rView.LocalReport.ReportEmbeddedResource = "EasyDose.rptIncident.rdlc"
    If Not IsNothing(parameters) Then
        Dim Bound0 As Integer = parameters.GetUpperBound(0)
        Dim Bound1 As Integer = parameters.GetUpperBound(1)
        For index = 0 To Bound0
            Dim rParameter As New ReportParameter(parameters(index, 0), parameters(index, 1))
            rView.LocalReport.SetParameters(rParameter)
        Next
    End If

    Dim ps As PageSettings = rView.GetPageSettings

    ps.Margins.Top = 0             ' 10mm approx
    ps.Margins.Right = 0
    ps.Margins.Bottom = 0
    ps.Margins.Left = 0
    ps.Landscape = False
    'ps.PaperSize = New PaperSize("LetterExtra", (9.275 * 100), (12 * 100)) ' Letter paper (8.5 in. by 11 in.) ' Letter extra paper (9.275 in. by 12 in.)
    ps.PaperSize = New PaperSize("A4", (8.27 * 100), (11.69 * 100))

    rView.RefreshReport()

    Dim exePath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)

    Dim dir As New DirectoryInfo(System.IO.Path.Combine(exePath, "tmpDir"))
    Dim file As New FileInfo(System.IO.Path.Combine( _
        dir.FullName, String.Format("Patient_Details_{0:yyyyMMdd_hhmmss}.pdf", DateTime.Now)))

    If Not dir.Exists Then
        dir.Create()
    End If

    Dim bytes As Byte() = rView.LocalReport.Render("PDF")

    Using fs As New System.IO.FileStream(file.FullName, System.IO.FileMode.Create)
        fs.Write(bytes, 0, bytes.Length)
        fs.Close()
    End Using

    Return file.FullName
End Function

Solution

  • are you seeing the local report in the embedded ReportViewer using the "Print Layout" option activated? That should show exactly the same output as your printed result.

    If you have problems in the PDF is probably caused by the design of the report itself. Check the font, the page size and orientation, the margins, the page breaks.