按顺序合并多个pdf

嘿伙计们,对于长篇大论和糟糕的语言,如果有不必要的细节,

我使用 excel 文档从一个 pdf 模板创建了多个 1page pdf,

我现在有

这样的东西

tempfile0.pdf

tempfile1.pdf

tempfile2.pdf

...

我试图合并所有使用 itext5 在单个 pdf 中创建文件,

但它表明结果 pdf

中的页面与第一页

tempfile1中的每个示例tempfile0.pdf 中 我想要的顺序不同 。

这里的 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();

    }


}


我的问题是如何在生成的 pdf 中保持文件的顺序相同


慕妹3146593
浏览 166回答 1
1回答

忽然笑

这是因为文件的命名。您的代码 new FileOutputStream(outputfolder + "\\" + "tempcontrat" + debut + "-" + i + "_.pdf") 将产生:tempcontrat0-0_.pdftempcontrat0-1_.pdf...tempcontrat0-10_.pdftempcontrat0-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()该阅读器。简而言之,它看起来像:// initializePdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);for (int i = debut; i < fin; i++) {&nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();&nbsp; &nbsp; // fill in the form here&nbsp; &nbsp; stamper.close();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; PdfReader reader = new PdfReader(out.toByteArray());&nbsp; &nbsp; reader.consolidateNamedDestinations();&nbsp; &nbsp; PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, 1);&nbsp; &nbsp; pdfSmartCopy.addPage(pdfImportedPage);&nbsp; &nbsp; // other actions ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java