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

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

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

问题

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

  1. public String mergePdfs(ArrayList<InputStream> files) {
  2. File pdfMerged = new File("merged.pdf");
  3. PdfDocument pdf = new PdfDocument(new PdfWriter(pdfMerged));
  4. PdfMerger merger = new PdfMerger(pdf);
  5. for (InputStream in : files) {
  6. // 从每个文档添加页面
  7. PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
  8. merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
  9. sourcePdf.close();
  10. }
  11. pdf.close();
  12. }

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

  1. 2020-08-28 18:05:59,935 ERROR [kernel.pdf.PdfReader] [http-nio-8080-exec-9] 读取交叉引用表时发生错误。将重新构建交叉引用表。
  2. com.itextpdf.io.IOException: 在文件指针 1,051,972 处发生错误。
  3. at com.itextpdf.io.source.PdfTokenizer.throwError(PdfTokenizer.java:639)
  4. at com.itextpdf.kernel.pdf.PdfReader.readXrefSection(PdfReader.java:839)
  5. at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:777)
  6. at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:532)
  7. at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1638)
  8. at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:231)
  9. ...
  10. ...
  11. ...
  12. 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

  1. File pdfMerged = new File("merged.pdf");
  2. PdfDocument pdf = new PdfDocument(new PdfWriter(pdfMerged));
  3. PdfMerger merger = new PdfMerger(pdf);
  4. for (InputStream in : files) {
  5. //Add pages from the each document
  6. PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
  7. merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
  8. sourcePdf.close();
  9. }
  10. pdf.close();
  11. }

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

  1. 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.
  2. com.itextpdf.io.IOException: Error at file pointer 1,051,972.
  3. at com.itextpdf.io.source.PdfTokenizer.throwError(PdfTokenizer.java:639)
  4. at com.itextpdf.kernel.pdf.PdfReader.readXrefSection(PdfReader.java:839)
  5. at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:777)
  6. at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:532)
  7. at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1638)
  8. at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:231)
  9. ...
  10. ...
  11. ...
  12. 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 语句,在我的情况下解决了这个问题。

  1. for (InputStream in : files) {
  2. // 从每个文档中添加页面
  3. PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
  4. try {
  5. merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
  6. } catch (Exception e) {
  7. // 记录错误并继续
  8. }
  9. sourcePdf.close();
  10. }
英文:

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.

  1. for (InputStream in : files) {
  2. //Add pages from the each document
  3. PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
  4. try{
  5. merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
  6. } catch (Exception e) {
  7. //log an error and continue
  8. }
  9. sourcePdf.close();
  10. }

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:

确定