2015-10-14 173 views
1

我是iText的新手。我想用iText(Java)以PDF格式创建几个报告。每一份报告都将采用不同的设计。有没有办法手动创建模板?我正在使用数据库数据来创建PDF。 iText有什么功能可以让我做到这一点。提前致谢。使用iText和java创建PDF的自定义模板

+0

考虑使用jasper报告,然后导出到pdf。这个报告系统有一个图形界面iReport –

+0

我的要求是使用iText。 –

回答

3

如果您打算使用另一个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.. 
+0

我有疑问..template.pdf会让我的设计成为礼仪吗? –

+0

正确的,它将成为你创建PDF的背景,有metods以及提取另一个pdf的一部分,并使用这个作为模板作为组件(例如reapeating部分),在您创建的pdf –

+0

签出这个例子http ://itextpdf.com/examples/iia.php?id=73 –