使用 Apache PDF Box 阅读 pdf

Reading pdf with Apache PDF Box

我有这些代码行,我一直在尝试使用它们来使用 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