Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 使用C使用PDFsharp创建多个页面#_C# 4.0_Pdfsharp - Fatal编程技术网

C# 4.0 使用C使用PDFsharp创建多个页面#

C# 4.0 使用C使用PDFsharp创建多个页面#,c#-4.0,pdfsharp,C# 4.0,Pdfsharp,我正在使用PDFsharp创建PDF页面。这对于只有一页的文档非常有效。 在这种情况下,行需要填满两页。只要行数等于20, 我想创建一个新页面,并将剩余内容写入其中 此代码将内容写入第一页,但一旦行数等于20,它将继续写入第一页 而不是去第二个 请问我该怎么修 PdfDocument document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; // Create an empty page PdfPa

我正在使用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);
    }
}

我肯定这是一个复制品

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

此问题的副本:

另一个chap试图创建新的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);
}

这是一个有点陈旧的主题或线索,但添加了一些输入来澄清

user2320476是错误的。您能够(并且允许)使用XGraphics.FromPdfPage(第页);一页两次

只要确保你处理掉第一个,你就准备好了

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


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

实际上它不是重复的。我能够创建第二个页面,但将内容写入其中是个问题。我仍然认为它是重复的。不可能使用XGraphics gfx=XGraphics.FromPdfPage(第页);一页两次。这是一个错误。这是不正确的。请确保使用
page=document.AddPage()
,您将拥有一个新页面,并且可以为新页面创建
XGraphics
对象。你不能在第一页创建两个
XGraphics
对象,如果你想在第二页上绘制,你不应该尝试。Hello的可能副本大家看这个问题。我无法在Pdf文档中添加和写入第二页。我最后做的是编写两个独立的PDF,然后将它们连接起来。我不喜欢这样做…但它工作了。在一个PDF文件中创建多个页面没有问题。不建议仅使用一个页面创建多个PDF文件并将其连接起来。此解决方案有缺点,例如在文件大小方面。这对你来说很有效,但可能比做正确的事情更复杂。它最终为原始海报工作。
if (gfx == null)
gfx.Dispose();
XGraphics gfx = XGraphics.FromPdfPage(page);