Results 1 to 7 of 7

Thread: PDF add pages

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Posts
    23

    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:


    Code:
                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 by cypress; Jun 22nd, 2018 at 01:19 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: PDF add pages

    Inside the loop you use the variable yPoint to keep track of where to print the row of data... once the value of that variable reaches the height of the page (I assume pdfPage.Height.Point ), add a new page and reset the graph variable (as you do at the end of the lines that declare pdfPage and graph), add the header and footer, and reset the variable yPoint to its initial value (50+75+20)

    You probably want to create a sub to print the header (pass in parameters as apt), so that you don't need to repeat the code (and therefore have to remember to change both if you change either).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Posts
    23

    Re: PDF add pages

    Thanks for the quick response! I'm tracking so far on what you are telling me. The actually "add a new page" part is where my troubles lie. I've been able to add a new page but they continually show up blank. I know I'm missing something little but it's kicking my butt at the moment.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: PDF add pages

    Are you also resetting the graph variable?

    That is what you are drawing to, and is explicitly related to a particular page.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Posts
    23

    Re: PDF add pages

    Are you referring to the "Dim graph as Xgraphics = XGraphics.FromPdfPage(pdfPage)"? Or am I going to need to reset all the graph.DrawString code everytime a new page is reached?
    I apologize if this is a dumb question; I'm just not grasping this PDFSharp stuff.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: PDF add pages

    I think you should just need to do this:
    Code:
    pdfPage = pdf.AddPage
    graph = XGraphics.FromPdfPage(pdfPage)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Posts
    23

    Re: PDF add pages

    ok...thanks! That was the code I was initially using but was only getting blank pages after page 1. Appreciate the help!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width