2

I'm using apache pdfbox in my javaFx application where im reading a pdf document now i want to display a org.apache.pdfbox.pdmodel.PDDocument inside a pane in my FXML. So far i tried with org.apache.pdfbox.PDFReader but it is using it's own Jframe. I want to show it inside a pane.

Here what i have done so far

public class CustomPDFReader extends PDFReader {
    public CustomPDFReader(BillModel bm) {
        super();
        showAllPages(bm.getAllPages());
        setVisible(true);
    }

    private void showAllPages(List<PDPage> pagesList) {
        try {
            Field documentPanel = getClass().getSuperclass().getDeclaredField("documentPanel");
            documentPanel.setAccessible(true);
            JPanel panel = (JPanel) documentPanel.get(this);
            GridLayout layout = new GridLayout(0, 1);
            panel.setLayout(layout);
            for(PDPage page : pagesList) {
                PageWrapper wrapper = new PageWrapper(this);
                wrapper.displayPage(page);
                panel.add(wrapper.getPanel());
            }
            pack();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
Arpan
  • 575
  • 1
  • 9
  • 27
  • 1
    Creating a image from the page would be the simplest option. See https://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images . You could also use `PDFRenderer` to output the result via `Graphics2D` which you can extend to create the output via other node types btw. – fabian Jul 31 '18 at 12:17
  • Yeah i thought about is as well but i was wondering if there is any other way apart from showing the pdf as image. – Arpan Jul 31 '18 at 12:25
  • `Graphics2D` receives drawing operations of different kinds. You should be able to produce a scene based on these operations. This will require a lot of work though. I'm pretty sure there is no build in support for javafx in pdfbox at this moment so these 2 options are probably the best ones you'll get, but someone else may prove me wrong... – fabian Jul 31 '18 at 12:29

0 Answers0