【问题标题】:Creating multiple pages with PDFsharp using C#使用 C# 使用 PDFsharp 创建多个页面
【发布时间】:2015-09-09 19:50:25
【问题描述】:

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

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

请问我该如何解决这个问题?

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

【问题讨论】:

  • 大家好,关注这个问题。我无法在 Pdf 文档中添加和写入第二页。我最终做的是编写两个单独的 pdf,然后将它们连接起来。我不喜欢那样做……但它奏效了。
  • 在一个PDF文件中创建多个页面是没有问题的。不建议仅使用一个页面创建多个 PDF 文件并将它们连接起来。这种解决方法有缺点,例如关于文件大小。它对您有用,但可能比正确操作更复杂。它终于适用于原始海报。

标签: c#-4.0 pdfsharp


【解决方案1】:

我确定这是重复的。

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

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

另一位尝试创建一个新的 XGraphics 对象,但也没有使用 AddPage() 的返回值。

更新:未经测试的代码 - 我希望它可以编译。

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

【讨论】:

  • 其实不是重复的。我可以创建第二个页面,但向其中写入内容是个问题。
  • 我仍然认为它是重复的。
  • 无法使用 XGraphics gfx = XGraphics.FromPdfPage(page);一页两次。它正在引发错误。这是不正确的
  • 确保使用page = document.AddPage(),您将拥有一个新页面,并且可以为新页面创建XGraphics 对象。您不能为第一页创建两个 XGraphics 对象 - 如果您想在第二页上绘图,则不应尝试这样做。
【解决方案2】:

这是一个有些陈旧的话题或线程,但添加了一些输入来澄清。

user2320476 是错误的。您可以(并且允许)使用 XGraphics.FromPdfPage(page);一页两次。

只要确保你处理掉第一个就可以了。

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

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

他/她可能指的是页面不允许有多个活动的 XGraphics 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多