使用 iText 和 java 创建 pdf 的自定义模板

Customised template to create a pdf using iText and java

我是 iText 新手。我想使用 iText(Java) 创建多个 PDF 格式的报告。每一份报告都将采用不同的设计。有没有办法手动创建模板?我正在使用数据库数据创建 pdf。 iText 中是否有任何功能允许我这样做。提前致谢。

如果您打算使用另一个 pdf 作为模板并将其作为背景,您可以这样做。

//Starting a new pdf document
Document document = new Document();
ByteArrayOutputStream os = new ByteArrayOutputStream();

//This is your new pdf doc
PdfWriter writer = PdfWriter.getInstance(document, os);

document.open();
document.newPage();

//Get the file of you template, you should use try catch and then close it
//I simply to just show sistem
FileInputStream template = new FileInputStream("template.pdf");

//Load it into a reader
PdfReader reader = new PdfReader(template);

//Get the page of the template you like
PdfImportedPage page = writer.getImportedPage(reader, 1);

//Now you can add it to you report
PdfContentByte cb = writer.getDirectContent();
cb.addTemplate(page, 0, 0);

//Here goes code of other stuff that you add..