如何使用PDFBox的PDFPagePanel查看PDF文档

我似乎无法弄清楚如何使用PDFBox及其PDFPagePanel组件查看PDF页面。

因此,似乎使用PDFBox我的选项是创建一个PDPage对象列表或PDDocument对象,我已经使用PDPage列表(而不是使用Splitter()用于PDDocument对象)

以下代码创建名为testPage的PDPage对象

 File PDF_Path = new File("C:\\PDF.PDF"); PDDocument inputPDF = PDDocument.load(PDF_Path); List allPages = inputPDF.getDocumentCatalog().getAllPages(); inputPDF.close(); PDPage testPage = (PDPage)allPages.get(0); 

从这里我想创建一个PDFPagePanel并使用其setPage()方法将PDPage放入组件中。 从这里我想将组件添加到JFrame。 当我这样做时,我只看到空白。

 JFrame testFrame = new JFrame(); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); PDFPagePanel pdfPanel = new PDFPagePanel(); pdfPanel.setPage(testPage); testFrame.add(pdfPanel); testFrame.setBounds(40, 40, pdfPanel.getWidth(), pdfPanel.getHeight()); testFrame.setVisible(true); 

我找到了一个“解决方案”,建议将PDF转换为图像并将其显示为缓冲图像,虽然这样做但似乎不是正确的方法。 我是否错误地尝试使用PDFBox的PDFPagePanel作为显示PDF的方法?

当我注释掉inputPDF.close调用时,它可以正常工作。 如果在完成显示pdf后移动近距离怎么办? 这样的东西……

 testFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); testFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { try { inputPDF.close(); testFrame.setVisible(false); } catch (IOException e1) { // TODO: implement error handling e1.printStackTrace(); } } }); 

为了记录,我还将PDFBox查看器实现为包装在JPanel中的Component中的BufferedImage。 然后,我能够使用其他按钮自定义面板以更改页面,更改文档,“缩放”或调整图像大小等。