[英]IndexOutOfBounds Exception when using PDFBox PDFPagePanel
我正在嘗試使用PDFBox的PDFPagePanel將PDF文件加載到基於Swing的eBook閱讀器應用程序中。
PDDocument pdfDoc = PDDocument.load(file);
List<PDPage> pages = (List<PDPage>) pdfDoc.getDocumentCatalog().getAllPages();
PDFPagePanel panel = new PDFPagePanel();
panel.setPage(pages.get(0));
pdfDoc.close();
JFrame frame = new JFrame();
frame.add(panel);
frame.setVisible(true);
從理論上講,這應該可行,因為PDFPagePanel只是一個JPanel,並且正在打開的文件似乎可以正確讀取,因為我可以使用文本剝離器提取文本並獲取元數據。 但是,每當我運行它時,都會出現以下IndexOutOfBounds異常:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.apache.pdfbox.io.RandomAccessBuffer.seek(RandomAccessBuffer.java:84)
at org.apache.pdfbox.io.RandomAccessFileInputStream.read(RandomAccessFileInputStream.java:96)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:161)
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:98)
at org.apache.pdfbox.cos.COSStream.doDecode(COSStream.java:295)
at org.apache.pdfbox.cos.COSStream.doDecode(COSStream.java:237)
at org.apache.pdfbox.cos.COSStream.getUnfilteredStream(COSStream.java:172)
at org.apache.pdfbox.pdfparser.PDFStreamParser.<init>(PDFStreamParser.java:108)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:251)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:235)
at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:215)
at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:125)
at org.apache.pdfbox.pdfviewer.PDFPagePanel.paint(PDFPagePanel.java:98)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
放入PDFPagePanel(pages.get(0))的頁面肯定存在。 如果我也嘗試在頁面上使用convertToImage(),則會遇到相同的錯誤。 任何幫助表示贊賞。
問題是, pdfDoc
是后立即關閉panel.setPage()
但是, PDFPagePanel
僅在顯示頁面PDFPagePanel
嘗試處理pdf流。 要克服此異常,請確保PDFPagePanel
仍在使用該文檔時該文檔可用。 您可能需要使該文檔成為成員變量,因為一旦文檔離開作用域並觸發其關閉,GC可能會收集該成員變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.