[英]merge multiple pdfs in order
嘿,很抱歉,您发布的帖子过长,语言不好,如果有不必要的细节,
我使用excel文档从一个pdf模板创建了多个1page pdf
我现在有了
像这样的东西
tempfile0.pdf
tempfile1.pdf
tempfile2.pdf
...
我试图使用itext5将所有文件合并为一个pdf
但它的结果是,结果pdf中的页面不符合我希望的示例顺序
第一页中的tempfile0.pdf
临时文件1。 int 2000页
这是即时通讯使用的代码。
我使用的程序是:
1从哈希图中填充
2将填写的表单另存为一个pdf
3将所有文件合并为一个pdf
public void fillPdfitext(int debut,int fin) throws IOException, DocumentException {
for (int i =debut; i < fin; i++) {
HashMap<String, String> currentData = dataextracted[i];
// textArea.appendText("\n"+pdfoutputname +" en cours de preparation\n ");
PdfReader reader = new PdfReader(this.sourcePdfTemplateFile.toURI().getPath());
String outputfolder = this.destinationOutputFolder.toURI().getPath();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputfolder+"\\"+"tempcontrat"+debut+"-" +i+ "_.pdf"));
// get the document catalog
AcroFields acroForm = stamper.getAcroFields();
// as there might not be an AcroForm entry a null check is necessary
if (acroForm != null) {
for (String key : currentData.keySet()) {
try {
String fieldvalue=currentData.get(key);
if (key=="ADRESSE1"){
fieldvalue = currentData.get("ADRESSE1")+" "+currentData.get("ADRESSE2") ;
acroForm.setField("ADRESSE", fieldvalue);
}
if (key == "IMEI"){
acroForm.setField("NUM_SERIE_PACK", fieldvalue);
}
acroForm.setField(key, fieldvalue);
// textArea.appendText(key + ": "+fieldvalue+"\t\t");
} catch (Exception e) {
// e.printStackTrace();
}
}
stamper.setFormFlattening(true);
}
stamper.close();
}
}
这是合并的代码
public void Merge() throws IOException, DocumentException
{
File[] documentPaths = Main.objetapp.destinationOutputFolder.listFiles((dir, name) -> name.matches( "tempcontrat.*\\.pdf" ));
Arrays.sort(documentPaths, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
byte[] mergedDocument;
try (ByteArrayOutputStream memoryStream = new ByteArrayOutputStream())
{
Document document = new Document();
PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
document.open();
for (File docPath : documentPaths)
{
PdfReader reader = new PdfReader(docPath.toURI().getPath());
try
{
reader.consolidateNamedDestinations();
PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, 1);
pdfSmartCopy.addPage(pdfImportedPage);
}
finally
{
pdfSmartCopy.freeReader(reader);
reader.close();
}
}
document.close();
mergedDocument = memoryStream.toByteArray();
}
FileOutputStream stream = new FileOutputStream(this.destinationOutputFolder.toURI().getPath()+"\\"+
this.sourceDataFile.getName().replaceFirst("[.][^.]+$", "")+".pdf");
try {
stream.write(mergedDocument);
} finally {
stream.close();
}
documentPaths=null;
Runtime r = Runtime.getRuntime();
r.gc();
}
我的问题是如何在生成的pdf中保持文件顺序相同
这是因为文件的命名。 您的代码new FileOutputStream(outputfolder + "\\\\" + "tempcontrat" + debut + "-" + i + "_.pdf")
将产生:
在tempcontrat0-11_.pdf之前将tempcontrat0-1000_.pdf放置的位置 ,因为在合并之前要按字母顺序对其进行排序。
最好使用org.apache.commons.lang.StringUtils
或java.text.DecimalFormat
leftPad()方法将填充字符保留为0
字符,并使其类似于tempcontrat0-000000.pdf , tempcontrat0-000001.pdf , ... tempcontrat0-9999999.pdf 。
而且,您也可以做得更简单,跳过表格中的写入,然后从文件中读取内容,并在表单填写后立即合并文档,这样会更快。 但这取决于要合并的文件数量和大小,以及您拥有的内存量。
因此,您可以将填充的文档保存到ByteArrayOutputStream
然后在stamper.close()
之后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 ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.