2015-10-14 122 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 –