gpt4 book ai didi

java - 使用 Apache PDFBox 阅读 pdf

转载 作者:行者123 更新时间:2023-12-01 10:44:16 31 4
gpt4 key购买 nike

我有这些代码行,我一直在尝试使用 Apache pdfBox 来读取 pdf 文件。

    private void readPdf(){
try {
File PDF_Path = new File("/home/olyjosh/Downloads/my project.pdf");
PDDocument inputPDF = PDDocument.load(PDF_Path);
List<PDPage> allPages = inputPDF.getDocumentCatalog().getAllPages();
PDPage testPage = (PDPage) allPages.get(5);
System.out.println("Number of pages "+allPages.size());

PDFPagePanel pdfPanel = new PDFPagePanel();
jPanel1.add(pdfPanel);
pdfPanel.setPage(testPage);

// this.revalidate();
inputPDF.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

我希望这个 pdf 显示在像 jPanel 这样的 swing 组件上,但这只会显示包含 pdf 文件的预期内容的面板。但是,我能够使用

将pdf显示为图像
convertToImage = testPage.convertToImage();

请问我该如何解决这个问题或者我做错了什么。

最佳答案

Apache PDF-Box 有一个mailing list我可以问同样的问题,这就是我得到的答复

This was removed in 2.0 because it made trouble. Obviously, it doesn't work for 1.8 either, at least for you, so why bother?

There are two ways to display, either get a BufferedImage (renderImage / renderImageWithDPI) and display that somehow (see in PDFDebugger how to do it), or renderPageToGraphics which renders to a graphics device object.

If you really want to get the source code of the deleted PDFReader application (which includes PDFPagePanel), use svn to get revision 1702125 or earlier, that should have it. But if it didn't work for you in 1.8, it won't work for you now.

The point is that swing display of PDF pages isn't part of the API, it's part of some tool (now: in PDFDebugger, previously: in PDFReader)

You need to have some understanding of awt / swing. If you don't, learn it, or hire somebody. (That's what we did, and the best is: google paid it, as part of the google summer of code)

Tilman

关于java - 使用 Apache PDFBox 阅读 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276406/

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