Creating multiple pages with PDFsharp using C#

二次信任 提交于 2019-12-12 07:04:19

问题


I am using PDFsharp to create a PDF page. This works very well for a document with just one page. There will be a situation where the rows will need to fill up two pages. Whenever the number of lines is equal to 20, I will like to create a new page and write the remaining content to it.

This code writes content on the first page but once the number of lines equals 20 it will continue to write on the first page and not go to the second.

How do I fix this please?

PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";

// Create an empty page
PdfPage page = document.AddPage();

//page.Width = 
//page.Height = 

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

// Create a font
XFont font = new XFont("Times New Roman", 8, XFontStyle.BoldItalic);

int headeroneX = 30;
int headerOney = 25;
Int32 countLines = 0;

foreach (var item in queryResult)
{
    if ((playerIndex % TotalNumberOfUsersInGrp) == 0)
    {
        gfx.DrawString("Group:" + groupindex, font, XBrushes.DarkRed, new XRect(headeroneX, headerOney, page.Width, page.Height), XStringFormats.TopLeft);
        groupindex++;           
        headerOney = headerOney + 12;
    }
    gfx.DrawString(item.FullName + ',' + item.Rating, font, XBrushes.Black, new XRect(headeroneX, headerOney, page.Width, page.Height), XStringFormats.TopLeft);
    playerIndex++;
    headerOney = headerOney + 12;
    countLines = countLines + 1;

    if (countLines == 20)
    {
        countLines = 1;
        headerOney = 25;
        document.AddPage();
        gfx.DrawString(item.FullName + ',' + item.Rating, font, XBrushes.Black, new XRect(headeroneX, headerOney, page.Width, page.Height), XStringFormats.TopLeft);
    }
}

回答1:


I'm sure this is a duplicate.

You call AddPage() to create the second page, but continue to use the XGraphics object you created for the first page. You have to use the return value of AddPage() to create a new XGraphics object.

Duplicate of this question:
https://stackoverflow.com/a/21143712/1015447

The other chap tried to create a new XGraphics object, but also did not use the return value of AddPage().

Update: Untested code - I hope it compiles.

if (countLines == 20)
{
    countLines = 1;
    headerOney = 25;
    // Wrong: document.AddPage();
    // Better:
    page = document.AddPage();
    // Also missing:
    gfx = XGraphics.FromPdfPage(page);
    gfx.DrawString(item.FullName + ',' + item.Rating, font, XBrushes.Black, new XRect(headeroneX, headerOney, page.Width, page.Height), XStringFormats.TopLeft);
}



回答2:


It's a somewhat aged topic or thread but adding some input to clarify.

user2320476 is wrong. You are able (and allowed) to use XGraphics.FromPdfPage(page); twice in a page.

Just make sure you dispose of the first one and you're all set.

Using (XGraphics gfx = XGraphics.FromPdfPage(page))
{ MakeItRain(); }

or

if (gfx == null)
gfx.Dispose();
XGraphics gfx = XGraphics.FromPdfPage(page);

What he/she probably refers to is that the page isn't allowed to have multiple active XGraphics objects.



来源:https://stackoverflow.com/questions/32488019/creating-multiple-pages-with-pdfsharp-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!