【发布时间】: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 文件并将它们连接起来。这种解决方法有缺点,例如关于文件大小。它对您有用,但可能比正确操作更复杂。它终于适用于原始海报。