Java 如何使用PDFBox';s PDFPagePanel

Java 如何使用PDFBox';s PDFPagePanel,java,swing,pdf,Java,Swing,Pdf,我似乎不知道如何使用PDFBox及其PDFPagePanel组件查看PDF页面 因此,似乎使用PDFBox我的选项是创建PDPage对象或PDDocument对象的列表,我选择了PDPage列表(而不是对PDDocument对象使用Splitter()) 下面的代码创建了一个名为testPage File PDF_Path = new File("C:\\PDF.PDF"); PDDocument inputPDF = PDDocument.load(PDF_Path); List<PDP

我似乎不知道如何使用PDFBox及其PDFPagePanel组件查看PDF页面

因此,似乎使用PDFBox我的选项是创建PDPage对象或PDDocument对象的列表,我选择了PDPage列表(而不是对PDDocument对象使用
Splitter()

下面的代码创建了一个名为testPage

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

我找到了一个“解决方案”,建议将其显示为一个缓冲图像,虽然这样做有效,但似乎不是正确的方法。我试图使用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查看器,它是一个BuffereImage,封装在一个JPanel中的组件中。然后,我可以使用其他按钮自定义面板,以更改页面、更改文档、“缩放”或调整图像大小等。

您是否尝试过不这么早关闭PDDocument(inputPDF.close())?我在完成后关闭PDDocument,而不是在使用PDPage时。是的,我也尝试过,谢谢你的建议。我真的不知道我为什么把那行放在那里,当我复制一些代码时,我把它放在了底部。现在,我只是向PDFBox邮件列表发送了一封电子邮件,并将研究其他可用选项。从外观上看,我不应该对使用其他工具(如iText或IcePDF)有任何问题,因为所有的源代码都被推送到GitHub上的公共repo,但我从未参加过任何关于许可和开放源代码的课程。感谢您的建议,这非常有效。我已经实现了ICEpdf,但它比我需要的程序多了很多。你能为上面提到的定制面板发布一个示例代码吗。我试图在java应用程序中查看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();
    }
}

});