简体   繁体   English

使用iText 2.1.7合并大型PDF

[英]Using iText 2.1.7 to merge large PDFs

I am using an older version of iText (2.1.7) to merge PDFs. 我使用较旧版本的iText(2.1.7)来合并PDF。 Because that is the last version under the MPL available to me. 因为这是MPL下的最后一个版本。 I cannot change this. 我无法改变这一点。

Anyways. 无论如何。 I am trying to merge multiple PDFs. 我想合并多个PDF。 Everything seems to work ok, but when I go over about 1500 pages, then the generated PDF fails to open (behaves as if it is corrupted) 一切似乎都运行正常,但是当我浏览大约1500页时,生成的PDF无法打开(表现得好像它已损坏)

This is how I am doing it: 这就是我这样做的方式:

private byte[] mergePDFs(List<byte[]> pdfBytesList) throws DocumentException, IOException {
    Document document = new Document();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PdfCopy copy = new PdfCopy(document, outputStream);
    document.open();

    for (byte[] pdfByteArray : pdfBytesList) {
        ByteArrayInputStream readerStream = new ByteArrayInputStream(pdfByteArray);
        PdfReader reader = new PdfReader(readerStream);

        for (int i = 0; i < reader.getNumberOfPages(); ) {
            copy.addPage(copy.getImportedPage(reader, ++i));
        }

        copy.freeReader(reader);
        reader.close();
    }

    document.close();

    return outputStream.toByteArray();
}

Is this the correct approach? 这是正确的方法吗? Is there anything about this that would hint at breaking when going over a certain amount of pages? 有什么关于这个会在超过一定数量的页面时暗示破坏吗? There are no exceptions thrown or anything. 抛出没有异常或任何东西。

对于任何好奇的人来说,这个问题与iText无关,而是负责从iText返回响应的代码。

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

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