简体   繁体   English

首页> C#> itext7-如何添加文本到新页面?

[英]c# - itext7 - How to add text to new page?

I'm creating Pdfs using itext7. 我正在使用itext7创建Pdfs。 It allows me to add paragraphs to first page but I'm not sure how can I add content to second page. 它允许我将段落添加到第一页,但是我不确定如何将内容添加到第二页。 If I create Canvas after calling AddNewPage() then it works fine, but it doesn't work when I use a paragraph and I add it to the document. 如果我在调用AddNewPage()之后创建Canvas,那么它可以正常工作,但是当我使用一个段落并将其添加到文档中时,它不起作用。 Thanks for help. 感谢帮助。 In my example, firstPageText and secondPageText will be displayed on first page: 在我的示例中,firstPageText和secondPageText将显示在第一页上:

protected void CreatePdf(string filePath, string firstPageText, string secondPageText)
   {
       PdfWriter writer = new PdfWriter(filePath);
       PdfDocument pdfDocument = new PdfDocument(writer);
       Document doc = new Document(pdfDocument);

       doc.Add(new Paragraph(firstPageText));
       pdfDocument.AddNewPage();
       doc.Add(new Paragraph(secondPageText)); 

       doc.Close();
   }

This is explained in chapter 2 of the iText 7: Building Blocks . iText 7:Building Blocks的 第2章对此进行了解释。 Allow me to copy a snippet of that tutorial: 请允许我复制该教程的片段:

If we had used an AreaBreak of type NEXT_PAGE , a new page would have been started; 如果我们使用了AreaBreak类型的NEXT_PAGE ,那么将开始一个新页面; see figure 2.11. 见图2.11。

在此处输入图片说明

In the JekyllHydeV5 example, we changed a single line: JekyllHydeV5示例中,我们更改了一行:

 AreaBreak nextPage = new AreaBreak(AreaBreakType.NEXT_PAGE); 

Instead of skipping to the next column, iText now skips to the next page. 现在,iText不再跳到下一列,而是跳到下一页。

By default, the newly created page will have the same page size as the current page. 默认情况下,新创建的页面将具有与当前页面相同的页面大小。 If you want iText to create a page of another size, you can use the constructor that accepts a PageSize object as a parameter. 如果希望iText创建其他尺寸的页面,则可以使用接受PageSize对象作为参数的构造函数。 For instance: new AreaBreak(PageSize.A3) . 例如: new AreaBreak(PageSize.A3)

There's also an AreaBreak of type LAST_PAGE . 还有一个AreaBreak类型的LAST_PAGE This AreaBreakType is to be used when switching between different renderers. 在不同的渲染器之间切换时,将使用此AreaBreakType

It surprises me that you'd do this: 您会这​​样做使我感到惊讶:

doc.Add(new Paragraph(firstPageText));
pdfDocument.AddNewPage();
doc.Add(new Paragraph(secondPageText));

While the documented way is to do it like this: 虽然记录的方式是这样的:

doc.Add(new Paragraph(firstPageText));
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
doc.Add(new Paragraph(secondPageText));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM