我正在尝试使用 iText 7 使用以下代码创建 PDF 文档,并且我的 PDF 文档内容在生成时在同一页面中重叠。(即在第 1 页中)。
我看到了
document.newPage();
iText 7 中缺少方法。如何在 iText 7 中不使用 pdfDocumet.copyPages(...) 或 PDFmerger 的情况下向 PDF 文档添加页面。
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.addNewPage();
Document PageOnedocument = new Document(pdfDoc,PageSize.A4);
addPageOneContents(PageOnedocument);
pdfDoc.addNewPage();
Document PageTwodocument = new Document(pdfDoc,PageSize.A4);
addPageTwoContents(PageTwodocument);
pdfDoc.close();
PageOnedocument.close();
PageTwodocument.close();
在 iText 7 中,
newPage
方法已成为区域中断的特殊情况:
Document document = ...;
[....add some content...]
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
[...add some content on next page...]
如果您使用 C# 并重构引用 iText 5 的代码(就像我一样),请尝试将 iText 7 方法置于覆盖中。
public static class Itext7DocumentExtensions
{
public static void NewPage(this Document document)
{
document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
}
现在您可以拨打电话
document.NewPage();
IText 7.2.2中有一些在PDF中添加新页面的方法
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
public class AddPageToPDF {
public static void main(String[] args) {
// Path to the existing PDF file
String existingPdf = "path/to/existing.pdf";
// Path to the output PDF file with the new page
String outputPdf = "path/to/output.pdf";
try {
// Create a PdfWriter instance to write to the output PDF file
PdfWriter pdfWriter = new PdfWriter(outputPdf);
// Create a PdfDocument instance representing the existing PDF file
PdfDocument existingPdfDocument = new PdfDocument(new PdfReader(existingPdf), pdfWriter);
// Add a new page to the existing PDF document
existingPdfDocument.addNewPage();
// Close the PdfDocument instances to ensure changes are saved
existingPdfDocument.close();
pdfWriter.close();
System.out.println("New page added successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
PDF 示例:立即点击