How to view a PDF using PDFBox PDFPagePanel

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

So it seems like using PDFBox my options are to either create PDPage list objects or PDDocument objects, but I went with the PDPage list (as opposed to using Splitter()for PDDocument objects)

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

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

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?

+4
1

inputPDF.close, . , , pdf? - ...

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, , JPanel. , , "" ..

+3

Source: https://habr.com/ru/post/1526252/


All Articles