IndexOutOfBounds使用PDFBox PDFPagePanel时的异常

时间:2013-12-21 15:25:35

标签: java swing pdfbox indexoutofboundsexception

我正在尝试使用PDFBox的PDFPagePanel将PDF文件加载到基于Swing的电子书阅读器应用程序中。

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(),我会得到相同的错误。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

问题是pdfDocpanel.setPage()被立即关闭。但是,PDFPagePanel仅在显示页面时尝试处理pdf流。要克服此异常,请确保文档在PDFPagePanel仍在使用时可用。您可能需要使文档成为成员变量,因为GC可能会在文档离开作用域时收集它,这将触发其关闭。