我的程序需要阅读pdf的第一页,并使用PDFBox 2.0.15进行阅读。它通常可以使用,但是对于某些文档(无法共享的来自扫描仪/打印机的扫描PDF),尽管索引循环未超过PdPageTree getCount()
,我还是遇到了上述异常:
PDPageTree allPages = pdf.getPages();
int pageLimit = allPages.getCount();
System.err.println("Page Tree has " + pageLimit + " pages"); // 1 page reported
for (int i = 0; i < pageLimit; i++) {
System.err.println("i = " + i); // prints 0
PDPage page = allPages.get(i); // => java.lang.IllegalStateException: Index not found: 1 (1-based page numbering as written in the javadoc but get() is 0-based)
…
}
堆栈跟踪为:
java.lang.IllegalStateException: Index not found: 1
at org.apache.pdfbox.pdmodel.PDPageTree.get(PDPageTree.java:298)
at org.apache.pdfbox.pdmodel.PDPageTree.get(PDPageTree.java:227)
at myProject.tools.analyzePDF(Tools.java:1159)
at java.base/java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
at java.base/java.util.HashMap$KeySpliterator.tryAdvance(HashMap.java:1624)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.MatchOps$MatchTask.doLeaf(MatchOps.java:306)
at java.base/java.util.stream.MatchOps$MatchTask.doLeaf(MatchOps.java:277)
at java.base/java.util.stream.AbstractShortCircuitTask.compute(AbstractShortCircuitTask.java:115)
at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:408)
at java.base/java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:736)
at java.base/java.util.stream.MatchOps$MatchOp.evaluateParallel(MatchOps.java:242)
at java.base/java.util.stream.MatchOps$MatchOp.evaluateParallel(MatchOps.java:196)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
at java.base/java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:528)
at myProject.State.StartState$1.run(StartState.java:135)
at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
at java.base/java.util.TimerThread.run(Timer.java:506)
尽管pageCount()是正确的,为什么我会收到此异常?
所涉及的pdf具有以下共同的元数据: