1 回答
TA贡献1806条经验 获得超5个赞
这是因为文件的命名。您的代码 new FileOutputStream(outputfolder + "\\" + "tempcontrat" + debut + "-" + i + "_.pdf") 将产生:
tempcontrat0-0_.pdf
tempcontrat0-1_.pdf
...
tempcontrat0-10_.pdf
tempcontrat0-11_.pdf
...
tempcontrat0-1000_.pdf
其中tempcontrat0-1000_.pdf将放置在tempcontrat0-11_.pdf之前,因为您在合并之前按字母顺序对其进行排序。
最好0使用leftPad()方法org.apache.commons.lang.StringUtils或将填充文件编号与字符一起保留,java.text.DecimalFormat并使其像这样tempcontrat0-000000.pdf , tempcontrat0-000001.pdf , ... tempcontrat0-9999999.pdf。
而且您还可以做得更简单,跳过写入文件,然后从文件步骤中读取并在填写表格后立即合并文档,这样会更快。但这取决于您要合并的文档数量和大小以及您有多少内存。
因此,您可以将填充的文档保存到该流中的字节中,ByteArrayOutputStream并在stamper.close()创建新PdfReader的字节之后调用pdfSmartCopy.getImportedPage()该阅读器。简而言之,它看起来像:
// initialize
PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
for (int i = debut; i < fin; i++) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// fill in the form here
stamper.close();
PdfReader reader = new PdfReader(out.toByteArray());
reader.consolidateNamedDestinations();
PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, 1);
pdfSmartCopy.addPage(pdfImportedPage);
// other actions ...
}
添加回答
举报