c# - itext7 - 如何向新页面添加文本?

标签 c# itext7

我正在使用 itext7 创建 Pdf。它允许我将段落添加到第一页,但我不确定如何将内容添加到第二页。如果我在调用 AddNewPage() 之后创建 Canvas 那么它工作正常,但是当我使用一个段落并将它添加到文档时它不起作用。感谢帮助。在我的示例中,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();
   }

最佳答案

这在 chapter 2 中有解释的 iText 7: Building Blocks .请允许我复制该教程的片段:

If we had used an AreaBreak of type NEXT_PAGE, a new page would have been started; see figure 2.11.

enter image description here

In the JekyllHydeV5 example, we changed a single line:

AreaBreak nextPage = new AreaBreak(AreaBreakType.NEXT_PAGE);

Instead of skipping to the next column, iText now skips to the next page.

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. For instance: new AreaBreak(PageSize.A3).

还有 LAST_PAGE 类型的 AreaBreak。此 AreaBreakType 在不同渲染器之间切换时使用。

我很惊讶你会这样做:

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

虽然记录的方法是这样做的:

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

关于c# - itext7 - 如何向新页面添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47327968/

相关文章:

c# - 使用指令组织

c# - 使用 LINQ ForEach() 异步等待

c# - 减少 LambdaExpression 签名

c# - VB.NET 开发人员是否不那么好奇? VB.NET 标准化

java - Xpath 在 XMLSpy 中显示正确的结果,但在 Java 中显示 null

c# - 同一个类实现,只是父级不同,如何处理?

java - iText 7 (Java) 当大表扩展到下一页时,它不会绘制底部边框

java - 当我们使用 ITextRenderer 从 thymeleaf HTML 模板生成 PDF 时,如何设置 PDF 页面大小 A4?

java - itext7 - 不剪切内容的最大字体大小

java - 使用 LocationTextLocationStrategy 的 iText "Coordinate outside allowed range"异常