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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21689529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:04:27  来源:igfitidea点击:

How to view a PDF document using PDFBox's PDFPagePanel

javaswingpdf

提问by Jens Bodal

I cannot seem to figure out how to view a PDF Page using PDFBox and its PDFPagePanel component.

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

So it seems that using PDFBox my options are to either create a List of PDPage objects or PDDocument objects, I've gone with the PDPage list (as opposed to using Splitter()for PDDocument objects)

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

The following code creates a PDPage object named testPage

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

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);

From here I would like to create a PDFPagePaneland use its setPage()method to place the PDPage into the component. From here I want to add the component to a JFrame. When I do this I just see whitespace.

从这里我想创建一个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);

I found one 'solution' which suggest converting the PDF to an imageand displaying it as a buffered image, and while this works it doesn't seem like the correct way to do this. Am I incorrect in trying to use PDFBox's PDFPagePanel as a means to displaying a PDF?

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

回答by kevin_s

When I comment out the inputPDF.close call, it works okay. What if you move the close to after you are finished displaying the pdf? Something like this...

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

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();
    }
}

});

For the record, I also implemented a PDFBox viewer as a BufferedImage wrapped in a Component wrapped in a JPanel. Then, I was able to customize the panel with additional buttons to change pages, change documents, "zoom" or resize the image, etc.

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