最近,我有一个任务whereinnbsp,我不得不通过合并数据和模板创建的PDF动态。我一直在使用大量的PDF库sincenbsp;我开始编程做这样的任务。前一个我很佩服很多曾钰成报告。但多一些时间研究后,我发现,与OpenOffice的抽奖iText是最简单的方法,我们可以生成PDF形式。
这种视觉教程首先解释如何创建PDF表单OpenOffice的抽奖:
{A2}
保存PDF文件夹你有你的程序或只需改变输入信号源的程序的路径。
{A3}
现在这里是Java代码,它可以转换PDF填写值使用iText到最终的PDF:/**
*
*/
package com.linkwithweb.reports;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* @author Ashwin Kumar
*
*/
public class ITextStamperSample {
/**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException,
DocumentException {
// createSamplePDF();
generateAshwinFriends();
}
/**
*
*/
private static void generateAshwinFriends() throws IOException,
FileNotFoundException, DocumentException {
PdfReader pdfTemplate = new PdfReader("mytemplate.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);
stamper.setFormFlattening(true);
stamper.getAcroFields().setField("name", "Ashwin Kumar");
stamper.getAcroFields().setField("id", "1\n2\n3\n");
stamper.getAcroFields().setField("friendname",
"kumar\nsirisha\nsuresh\n");
stamper.getAcroFields().setField("relation", "self\nwife\nfriend\n");
stamper.close();
pdfTemplate.close();
}
}
下面是一个示例的pom.xml:{C}
创建一个PDF文件中的占位符填充数据名称test.pdf:
{A4}
{A5} {S14}| AshwinRayaprolu