Results 1 to 2 of 2

Thread: PDF add pages

  1. #1
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Aug 2009

    PDF add pages

    I have code which currently will load a PDF page with data from my query but when I get to the end of the PDF page, my data keeps writing but goes nowhere.

    How can I tell my code when it gets to the end of the page, to continue writing the rest of the data on the next page and so on and so on until all data is in the PDF file across multiple pages (if needed)....all while keeping the same overall header and column headers on each page?

    Here is the code thus far:

                Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Certifications.accdb;Persist Security Info=True"
                Dim con As New OleDb.OleDbConnection(strConnection)
                'Open connection to db
                    Dim yPoint As Integer
                    Dim Certification As String
                    Dim Officer As String
                    Dim CertDate As String
                    Dim ExpDate As String
                    Dim dt1 As New DataTable
                    Dim adapter1 As New OleDb.OleDbDataAdapter
                    Dim command1 As New OleDb.OleDbCommand(strAll, con)
                    adapter1.SelectCommand = command1
                    Dim pdf As PdfDocument = New PdfDocument
                    pdf.Info.Title = "Expiring Certifications Report"
                    Dim pdfPage As PdfPage = pdf.AddPage
                    Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
                    Dim font As XFont = New XFont("Verdana", 12, XFontStyle.Regular)
                    Dim fontHeader As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
                    Dim fontColumn As XFont = New XFont("Verdana", 14, XFontStyle.Underline)
                    Dim pen As XPen = New XPen(XColor.FromKnownColor(XKnownColor.Blue))
                    yPoint = 50
                    yPoint = yPoint + 75
                    graph.DrawString("Expiring Certifications", fontHeader, XBrushes.Black,
                            New XRect(25, 25, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    graph.DrawString("Officer", fontColumn, XBrushes.Black,
                            New XRect(5, 100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    graph.DrawString("Certification", fontColumn, XBrushes.Black,
                            New XRect(150, 100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    graph.DrawString("Certified On:", fontColumn, XBrushes.Black,
                            New XRect(425, 100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    graph.DrawString("Expires On:", fontColumn, XBrushes.Black,
                            New XRect(525, 100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    graph.DrawLine(pen, 100, 500, 100, 500)
                    If dt1.Rows.Count > 0 Then
                        For i As Integer = 0 To dt1.Rows.Count - 1
                            Officer = dt1.Rows(i).Item(0)
                            Certification = dt1.Rows(i).Item(1)
                            CertDate = dt1.Rows(i).Item(2)
                            ExpDate = dt1.Rows(i).Item(3)
                            yPoint = yPoint + 20
                            graph.DrawString(Officer, font, XBrushes.Red,
                                New XRect(5, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                            If Certification.Length > 40 Then
                                Dim strA As String = Certification.Substring(0, Certification.Substring(0, 40).LastIndexOf(" "))
                                Dim strB As String = Certification.Substring(strA.Length + 1)
                                graph.DrawString(strA, font, XBrushes.Red,
                                New XRect(170, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                                yPoint = yPoint + 20
                                graph.DrawString(strB, font, XBrushes.Red,
                                New XRect(170, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                                graph.DrawString(Certification, font, XBrushes.Red,
                                New XRect(170, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                            End If
                            graph.DrawString(CertDate, font, XBrushes.Red,
                            New XRect(425, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                            graph.DrawString(ExpDate, font, XBrushes.Red,
                            New XRect(525, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                        Dim pdfFilename As String = "Expiring Certifications.pdf"
                        MsgBox("No Certifications available")
                    End If
                Catch ex As Exception
                End Try
    Currently the above code will write to a new pdf file and save it as I want. But when my data becomes greater than the page length, there is not second, third, fourth, etc pages. My data just continues to write to nowhere at the end of the page. I would like to loop through the data and as I reach the end of the page, a new page start up with the same header and column headers.
    Last edited by cypress1976; 06-22-2018 at 3:21 PM.

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Logically yPoint should be checked against pdfPage.Height in loop, when you reach end you would likely use pdf.AddPage and initialize graph again.
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

Similar Threads

  1. Microsoft report viewer displays blank pages between printed pages
    By emaduddeen in forum Reporting / Printing
    Replies: 0
    Last Post: 06-25-2010, 9:00 AM
  2. problem opening pdf files from aspx pages
    By Heppen in forum VB.NET General Discussion
    Replies: 1
    Last Post: 05-04-2006, 7:30 PM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts