如何使用PDFBox的PDFPagePanel查看PDF文档 [英] How to view a PDF document using PDFBox's PDFPagePanel

查看:191
本文介绍了如何使用PDFBox的PDFPagePanel查看PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

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

The following code creates a PDPage object named 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);

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

From here I would like to create a PDFPagePanel and 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.

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的方法?

I found one 'solution' which suggest converting the PDF to an image and 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?

推荐答案

当我注释掉inputPDF.close时打电话,它运作正常。如果在完成显示pdf后移动近距离怎么办?像这样...

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...

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 。然后,我能够使用其他按钮自定义面板以更改页面,更改文档,缩放或调整图像大小等。

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的PDFPagePanel查看PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆