使用C#使用PDFsharp创建多个页面 [英] Creating multiple pages with PDFsharp using C#

查看:137
本文介绍了使用C#使用PDFsharp创建多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDFsharp创建PDF页面.这对于只有一页的文档非常有效.在某些情况下,行将需要填满两页.每当行数等于20时,我想创建一个新页面并将剩余内容写入其中.

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.

此代码在第一页上写入内容,但是一旦行数等于20,它将继续在第一页上写入而不是去第二个.

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.

请问该如何解决?

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);
    }
}

推荐答案

我确定这是重复项.

您调用 AddPage()创建第二个页面,但是继续使用为第一页创建的 XGraphics 对象.您必须使用 AddPage()的返回值创建一个新的 XGraphics 对象.

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.

这个问题的重复:
https://stackoverflow.com/a/21143712/1015447

另一个小伙子试图创建一个新的XGraphics对象,但也没有使用AddPage()的返回值.

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);
}

这篇关于使用C#使用PDFsharp创建多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆