使用 iText 和 java 创建 pdf 的自定义模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33119166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customised template to create a pdf using iText and java
提问by Heerapreethi P
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.
我是 iText 的新手。我想使用 iText(Java) 以 PDF 的形式创建多个报告。每个报告都将采用不同的设计。有没有办法手动创建模板?我正在使用数据库数据创建 pdf。iText 中是否有任何功能允许我这样做。提前致谢。
回答by Petter Friberg
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..