簡體   English   中英

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

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

我是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.. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM