iText7 Error while reading cross reference table. table will be rebuilt. file position {0} cross reference entry in this xref subsection

huangapple go评论73阅读模式
英文:

iText7 Error while reading cross reference table. table will be rebuilt. file position {0} cross reference entry in this xref subsection

问题

我有一个简单的函数,它使用iText7从一个ArrayList中合并pdf文档。

public String mergePdfs(ArrayList<InputStream> files) {
    File pdfMerged = new File("merged.pdf");
    PdfDocument pdf = new PdfDocument(new PdfWriter(pdfMerged));
    PdfMerger merger = new PdfMerger(pdf);

    for (InputStream in : files) {
        // 从每个文档添加页面
        PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
        merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
        sourcePdf.close();
    }

    pdf.close();
}

虽然这在大多数情况下都能工作,但不幸的是,有一个pdf文档会引发以下错误。

2020-08-28 18:05:59,935  ERROR [kernel.pdf.PdfReader] [http-nio-8080-exec-9] 读取交叉引用表时发生错误。将重新构建交叉引用表。
com.itextpdf.io.IOException: 在文件指针 1,051,972 处发生错误。
at com.itextpdf.io.source.PdfTokenizer.throwError(PdfTokenizer.java:639)
at com.itextpdf.kernel.pdf.PdfReader.readXrefSection(PdfReader.java:839)
at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:777)
at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:532)
at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1638)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:231)
...
...
...
Caused by: com.itextpdf.io.IOException: 此 xref 子段中的文件位置 {0} 交叉引用条目。

在合并中使用的问题pdf在其他程序中可以正常打开。只有itext7在抱怨。

我无法分享有问题的pdf,但我可以说它使用的是PDF版本1.6。

这导致结果是一个空的pdf。这个错误是什么意思,我该如何修复它或绕过它?

英文:

I have a simple function that is merging together pdfs from an ArrayList using iText7

            File pdfMerged = new File("merged.pdf");
            PdfDocument pdf = new PdfDocument(new PdfWriter(pdfMerged));
            PdfMerger merger = new PdfMerger(pdf);


            for (InputStream in : files) {
            //Add pages from the each document
                PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
                merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
                
                sourcePdf.close();
            }

            pdf.close();
}

While this works most of the time, unfortunately one pdf is giving the following error.


2020-08-28 18:05:59,935  ERROR [kernel.pdf.PdfReader] [http-nio-8080-exec-9] Error occurred while reading cross reference table. Cross reference table will be rebuilt.
com.itextpdf.io.IOException: Error at file pointer 1,051,972.
at com.itextpdf.io.source.PdfTokenizer.throwError(PdfTokenizer.java:639)
at com.itextpdf.kernel.pdf.PdfReader.readXrefSection(PdfReader.java:839)
at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:777)
at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:532)
at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1638)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:231)
...
...
...
Caused by: com.itextpdf.io.IOException: file position {0} cross reference entry in this xref subsection.

The pdf used in the merge that's causing issues opens just fine in other programs. It is only itext7 that is complaining.

I can't share the problematic pdf, but I can say it's using PDF version 1.6.

This causes the result to be an empty pdf. What does this error mean and how can I fix it or work around it?

答案1

得分: 3

我发现合并实际上是在工作并且正在合并文件,但无论如何都会抛出异常。我猜想是为了通知开发人员或用户它正在进行修复操作?
如果你仔细阅读异常信息,会发现它说"交叉引用表将被重建"。所以 iText 正在进行某种自动修复操作。

这个异常触发了一个不同的 try catch 块,导致 PDF 最终没有被关闭。

奇怪的是,我在 iText 7 文档中找不到关于这个的任何说明。

无论如何,围绕合并函数加上一个简单的 try catch 语句,在我的情况下解决了这个问题。

for (InputStream in : files) {
    // 从每个文档中添加页面
    PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));

    try {
        merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
    } catch (Exception e) {
        // 记录错误并继续
    }
    sourcePdf.close();
}
英文:

I found out that merger is actually working and merging the files but is throwing the exception anyway. I guess to inform the developer or user it is doing a fix?
If you read the exception carefully it says the "Cross reference table will be rebuilt." So iText is doing some kind of automated fix.

This exception was triggering a different try catch block and the pdf was never being closed as a result.

Strangely I couldn't find any documentation on this in the iText 7 documentation.

At any rate, a simple try catch around the merge function fixed the issue in my case.

    for (InputStream in : files) {
    //Add pages from the each document
        PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));

        try{
            merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
        } catch (Exception e) {
          //log an error and continue
        }
        sourcePdf.close();
    }

huangapple
  • 本文由 发表于 2020年8月29日 09:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定