我正在使用 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 typeNEXT_PAGE
, a new page would have been started; see figure 2.11.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/