PDFsharp - How to create a 2nd page

前端 未结 4 794
悲哀的现实
悲哀的现实 2020-12-06 13:58

I can\'t find documentation in PDFsharp to show how to add a 2nd page using C#!

As an example, over in VB6 I use a PDF creation method called mjwPDF. To indicate th

相关标签:
4条回答
  • 2020-12-06 14:08

    you can print in multiple pages by decreasing the size of "Ypoint" as shown below code.

    its given value is "yPoint = yPoint + 40"

    change it to "yPoint = yPoint + 8" i.e increment ypoint by +8 in every loop iteration,

    reply for any doubts. refer while loop below

            While dr.Read
    
                username = dr.Item(0)
                Action_Performed = dr.Item(1)
                datetim = dr.Item(2)
    
                graph.DrawString(username, font, XBrushes.Black, _
                   New XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
    
                graph.DrawString(Action_Performed, font, XBrushes.Black, _
                  New XRect(280, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
    
                graph.DrawString(datetim, font, XBrushes.Black, _
                   New XRect(420, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                yPoint = yPoint + 10
                counter += 1
                If counter = 30 Then
                    pdfPage = pdf.AddPage()
                    graph.Dispose()
                    graph = XGraphics.FromPdfPage(pdfPage)
                    font = New XFont("Verdana", 7, XFontStyle.Regular)
                    yPoint = 0
                    yPoint = yPoint + 100
                    counter = 0
                End If
            End While
            Dim pdfFilename As String = "dbtopdf.pdf"
            pdf.Save(pdfFilename)
            Process.Start(pdfFilename)
    

    regards, Shivanand

    0 讨论(0)
  • 2020-12-06 14:15

    Do not forget to update your "canvas" or "graphics" object variable to the new page:

    You may be doing something like this:

    // First time:
    this.page = document.AddPage();
    this.gfx = XGraphics.FromPdfPage(page);
    
    ...
    
    // next time (forgot to update graphics or canvas variable):
    this.page = document.AddPage();
    

    And, it should be something like:

    // First time:
    this.page = document.AddPage();
    this.gfx = XGraphics.FromPdfPage(page);
    
    ...
    
    // next time:
    this.page = document.AddPage();
    // update graphics object, also
    this.gfx = XGraphics.FromPdfPage(page);
    

    I also made that mistake.

    0 讨论(0)
  • 2020-12-06 14:17

    Before trying to create a new XGraphics object for the new page, dispose the old existing object:

    xgrObject.Dispose();
    
    0 讨论(0)
  • 2020-12-06 14:32

    You create a new page with

    document.AddPage();
    

    and then call

    XGraphics.FromPdfPage(page);
    

    to get a gfx object for the second page (probably this is the missing step).

    So repeat those steps from your code sample for every new page:

    // Create an empty page
    PdfPage page = document.AddPage();
    
    // Get an XGraphics object for drawing
    XGraphics gfx = XGraphics.FromPdfPage(page);
    

    Update: In your loop you do the following:

    document.AddPage();
    XGraphics.FromPdfPage(page);
    

    AddPage() returns a "handle" to the newly created page - which you throw in the bin. Then you call "XGraphics.FromPdfPage()" to create yet another gfx for the first page - which you also would throw in the bin, but you get an exception since there already is a gfx for the first page.

    A small change should do the trick:

    page = document.AddPage();
    gfx = XGraphics.FromPdfPage(page);
    

    See also: http://www.pdfsharp.net/wiki/PageSizes-sample.ashx

    "I never dreamed adding a second page could be so difficult! No luck with the suggested code."
    PDFsharp includes quite a few working samples to get people going. No luck with the suggested code since you ignored the important part - the values returned by the methods.

    0 讨论(0)
提交回复
热议问题