我照网上的写法找了段代码,读取模板也读取到了,但是写入就没成功,方便的话,前辈们帮看一下
public static void exPdf() {
// 模板路径
String templatePath = "D:/法院公告.pdf";
// 生成的新文件路径
String newPDFPath = "D:/ceshi.pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
String[] str = { "123456789", "TOP__ONE", "男", "1991-01-01", "130222111133338888"};
int i = 0;
java.util.Iterator<String> it = form.getFields().keySet().iterator();
while (it.hasNext()) {
String name = it.next().toString();
System.out.println(name);
System.out.println("数据");
form.setField(name, "sb");
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}