Reputation: 49
I am new to iText. I want to create several reports in the form of PDF using iText(Java). 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. Is there any feature in iText which allows me to do it. Thanks in advance.
Upvotes: 1
Views: 17828
Reputation: 21710
If as template you intend using another pdf and have it as background you do something like this.
//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..
Upvotes: 5