简体   繁体   English

使用iText和Java创建PDF的定制模板

[英]Customised template to create a pdf using iText and java

I am new to iText. 我是iText的新手。 I want to create several reports in the form of PDF using iText(Java). 我想使用iText(Java)以PDF格式创建多个报告。 Each and every report will be in a different design . 每份报告将采用不同的设计。 Is there any way to create a template manually? 有什么方法可以手动创建模板吗? I am using the database data to create a pdf. 我正在使用数据库数据创建pdf。 Is there any feature in iText which allows me to do it. iText中是否有允许我执行的功能。 Thanks in advance. 提前致谢。

If as template you intend using another pdf and have it as background you do something like this. 如果您打算使用另一个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.. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM