按顺序合并多个pdf

问题描述 投票:0回答:1

嘿家伙很抱歉长篇文章和糟糕的语言,如果有不必要的细节 我使用excel文档从一个pdf模板创建了多个1page pdf 我现在有了 这样的事情 tempfile0.pdf tempfile1.pdf tempfile2.pdf ... 我试图使用itext5合并一个单独的PDF文件中的所有文件 但是它表明结果pdf中的页面不符合我想要的顺序 第一页中的tempfile0.pdf tempfile1。在2000页面中 这是我正在使用的代码。 我使用的程序是: 1从hashmap填充from 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中保持文件的顺序相同

java pdf itext
1个回答
2
投票

这是因为文件的命名。你的代码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之前,因为您在合并之前按字母顺序对其进行排序。

使用0leftPad()org.apache.commons.lang.StringUtils方法使用java.text.DecimalFormat字符留下pad文件编号会更好,并且让它像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 ...
}
© www.soinside.com 2019 - 2024. All rights reserved.