简体   繁体   English

在 iText7 上合并 PDF

[英]Issue merging PDFs on iText7

I have the below method that merges my PDF documents.我有以下方法可以合并我的 PDF 文档。 However, in some cases where the document has a watermark, the method throws me an error.但是,在文档有水印的某些情况下,该方法会引发错误。

public void doMergeUsingItext7(List<InputStream> list, OutputStream outputStream) throws SSException {
    try (com.itextpdf.kernel.pdf.PdfWriter writer = new com.itextpdf.kernel.pdf.PdfWriter(outputStream);) {
        writer.setSmartMode(Boolean.TRUE);
        try (com.itextpdf.kernel.pdf.PdfDocument pdfDoc = new com.itextpdf.kernel.pdf.PdfDocument(writer)) {
            pdfDoc.initializeOutlines();
            list.forEach((in) -> {
                try (com.itextpdf.kernel.pdf.PdfReader reader = new com.itextpdf.kernel.pdf.PdfReader(in);) {
                    reader.setUnethicalReading(Boolean.TRUE);
                    try (com.itextpdf.kernel.pdf.PdfDocument addedDoc = new com.itextpdf.kernel.pdf.PdfDocument(reader)) { //ERROR IS THROWN ON THIS LINE
                        addedDoc.copyPagesTo(1, addedDoc.getNumberOfPages(), pdfDoc);
                        logger.log(Level.INFO, "Successfully Added the Document to PDF");
                    } catch (Exception e) {
                        ExceptionUtils.printRootCauseStackTrace(e);
                    }
                } catch (IOException ex) {
                    ExceptionUtils.printRootCauseStackTrace(ex);
                } catch (Exception e) {
                    ExceptionUtils.printRootCauseStackTrace(e);
                }
            });
        }
    } catch (Exception ex) {
        throw new SSException(ex, "Print Version Failed");
    }
}

The following error is thrown when the document has a watermark...当文档有水印时会抛出以下错误...

com.itextpdf.kernel.PdfException: Illegal length value.
at com.itextpdf.kernel.pdf.PdfEncryption.readAndSetCryptoModeForStdHandler(PdfEncryption.java:523)
at com.itextpdf.kernel.pdf.PdfEncryption.<init>(PdfEncryption.java:229)
at com.itextpdf.kernel.pdf.PdfReader.readDecryptObj(PdfReader.java:1251)
at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:685)
at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1871)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:252)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:234)
at gov.ca.lc.util.PdfUtilFuntions.lambda$doMergeUsingItext7$0(PdfUtilFuntions.java:180)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at gov.ca.lc.util.PdfUtilFuntions.doMergeUsingItext7(PdfUtilFuntions.java:176)

I am not sure what exactly within the document is failing to merge.我不确定文档中究竟是什么未能合并。 Any help on fixing this issue is highly appreciated.非常感谢任何有关解决此问题的帮助。 Thanks.谢谢。

This is a bug in iText.这是 iText 中的错误。

The PDF in question is encrypted.有问题的 PDF 已加密。 Its encryption dictionary has a V value of 1 ("RC4 or AES algorithms with an encryption key length of 40 bits") and an R value of 3 ("for files encrypted with a V value of 2 or 3, or with any “Security handlers of revision 3 or greater” access permissions set to 0").其加密字典的V值为 1(“RC4 或 AES 算法,加密密钥长度为 40 位”)和R值为 3(“对于使用 V 值为 2 或 3 或任何“安全修订版 3 或更高版本的处理程序”访问权限设置为 0”)。

A Length value for the key length is specified only for V 2 or 3, for V 1 the key length is fixed to 40. Nonetheless, iText in case of R 3 requires a Length value.密钥长度的Length值仅针对V 2 或 3 指定,对于V 1,密钥长度固定为 40。 尽管如此,iText 在R 3 的情况下需要Length值。

As there is no Length value in the encryption dictionary of your document, iText fails reading that file.由于文档的加密字典中没有Length值,iText 无法读取该文件。


As a side note: 40 bit key length security nowadays is no security.附带说明:如今 40 位密钥长度的安全性是没有安全性的。 So most likely that issue did not pop up earlier because using V 1 security (or 40 bit key length security in general) is useless and, therefore, no use case taken seriously.所以很可能这个问题没有更早出现,因为使用V 1 安全性(或一般的 40 位密钥长度安全性)是无用的,因此没有认真对待任何用例。

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

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