Question PDF add pages

cypress1976

Member
Joined
Aug 18, 2009
Messages
7
Programming Experience
3-5
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:


VB.NET:
            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
            con.Open()

Try
                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
                adapter1.Fill(dt1)

                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)
                        Else
                            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)

                    Next

                    Dim pdfFilename As String = "Expiring Certifications.pdf"
                    pdf.Save(pdfFilename)
                    Process.Start(pdfFilename)
                Else
                    MsgBox("No Certifications available")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            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:
Logically yPoint should be checked against pdfPage.Height in loop, when you reach end you would likely use pdf.AddPage and initialize graph again.
 
Back
Top