简体   繁体   English

pdf / A合并itext 5 java

[英]pdf/A merge itext 5 java

I'm trying merge two PDFs with itext 5. When the process ends, one PDF is generated, but the foxit reader doesn't read the PDF. 我正在尝试将两个PDF与itext 5合并。当过程结束时,将生成一个PDF,但是foxit阅读器不会读取该PDF。 Invalid format This is my code 格式无效这是我的代码

    public static byte[] mergePdf(byte[][] streamOfPDFFiles) throws IOException, DocumentException {

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      float MARGIN_OF_ONE_CM = 28.8f;
      Document document = new Document(PageSize.A4, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM,MARGIN_OF_ONE_CM);
      document.addLanguage("pt-BR");
      List<PdfReader> readers = new ArrayList<PdfReader>();       
      int totalPages = 0;
      byte[][] pdfs = streamOfPDFFiles;
      try {

        // Create Readers for the pdfs.
        for (byte[] bs : pdfs) {
            PdfReader pdfReader = new PdfReader(bs);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
      // step 2
      PdfACopy copy = new PdfACopy(document, baos, PdfAConformanceLevel.PDF_A_1A);
      // step 3
      document.open();
      // step 4

      int n;
      // loop over the documents you want to concatenate
      for (int i = 0; i < readers.size(); i++) {              
          // loop over the pages in that document
          n = readers.get(i).getNumberOfPages();
          for (int page = 1; page <= n;) {
              copy.addDocument(readers.get(i));
              ++page;
          }
          copy.freeReader(readers.get(i));
          readers.get(i).close();
      }
      byte[] pdfRetorno = baos.toByteArray();
      return pdfRetorno;
}

Can anyone helps me? 谁能帮我? thanks in advance 提前致谢

Whenever you open something, you should eventually close it again, in particular as closing may involve more than freeing resources, it may also include adding the final piece to written data. 每当您打开某些内容时,您最终都应该再次将其关闭,特别是因为关闭可能不仅仅涉及释放资源,还可能包括将最后的内容添加到已写入的数据中。

This also is the case here, you open your Document document : 在这里,您也可以打开“ Document document

document.open();

but don't close it thereafter. 但此后不要关闭它。 Thus, the PDF written to your ByteArrayOutputStream baos is incomplete, at least the final PDF objects cross references table and the trailer are missing. 因此,写入您的ByteArrayOutputStream baos的PDF不完整,至少缺少最终的PDF对象交叉引用表和预告片。

Thus, close the document before working with the result in baos : 因此,在baos处理结果之前,请关闭document

for (int i = 0; i < readers.size(); i++) {
    [...]
}
document.close();
byte[] pdfRetorno = baos.toByteArray();
return pdfRetorno;

Here, the correct code: 在这里,正确的代码:

public static byte[] mergePdf(byte[][] streamOfPDFFiles) throws IOException, DocumentException {

    final float MARGIN_OF_ONE_CM = 28.8f;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Document document = new Document(PageSize.A4, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM,
            MARGIN_OF_ONE_CM);
    List<PdfReader> readers = new ArrayList<PdfReader>();

    byte[][] pdfs = streamOfPDFFiles;
    try {

        // Create Readers for the pdfs.
        for (byte[] bs : pdfs) {
            PdfReader pdfReader = new PdfReader(bs);
            readers.add(pdfReader);             
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    PdfACopy copy = new PdfACopy(document, baos, com.itextpdf.text.pdf.PdfAConformanceLevel.PDF_A_1A);

    copy.setTagged();
    document.open();
    copy.createXmpMetadata();
    copy.setLanguage("pt-br");                  

    int n;
    for (int i = 0; i < readers.size(); i++) {          
        n = readers.get(i).getNumberOfPages();
        for (int page = 1; page <= n;) {
            copy.addDocument(readers.get(i));
            ++page;
        }
        copy.freeReader(readers.get(i));            
    }

    document.close();
    byte[] pdfRetorno = baos.toByteArray();
    return pdfRetorno;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM