为了账号安全,请及时绑定邮箱和手机立即绑定

按顺序合并多个pdf

按顺序合并多个pdf

慕妹3146593 2021-09-03 13:28:11
嘿伙计们,对于长篇大论和糟糕的语言,如果有不必要的细节,我使用 excel 文档从一个 pdf 模板创建了多个 1page pdf,我现在有这样的东西tempfile0.pdftempfile1.pdftempfile2.pdf...我试图合并所有使用 itext5 在单个 pdf 中创建文件,但它表明结果 pdf中的页面与第一页tempfile1中的每个示例tempfile0.pdf 中 我想要的顺序不同 。这里的 2000 页 是我使用的代码。我使用的程序是:1 从哈希图中填充一个2 将填充的表单保存为一个 pdf3 将所有文件合并为一个 pdfpublic 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();    }}我的问题是如何在生成的 pdf 中保持文件的顺序相同
查看完整描述

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 ...

}


查看完整回答
反对 回复 2021-09-03
  • 1 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信