gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:08 24 4
gpt4 key购买 nike

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

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

下面的代码创建了一个名为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);

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

我找到了一个建议 converting the PDF to an image 的“解决方案”并将其显示为缓冲图像,虽然这可行,但似乎不是执行此操作的正确方法。我尝试使用 PDFBox 的 PDFPagePanel 作为显示 PDF 的方式是否不正确?

最佳答案

当我注释掉 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();
}
}

});

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

关于java - 如何使用 PDFBox 的 PDFPagePanel 查看 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com