我是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..
Run Code Online (Sandbox Code Playgroud)