Daniyar Daniyar - 28 days ago 10
Java Question

Show all pages of a PDF using Apache PDFBox

I'm new in Apache PDFBox, I used org.apache.pdfbox.PDFReader to open PDF and etc,
but now I need to show in JFrame PDF documents all pages in one JPanel, is it possible?
like this

Answer

As described before I needed to show all pages of PDF document in a jframe, for it I have used org.apache.pdfbox.PDFReader as follow:

import java.awt.*;
import java.lang.reflect.*;
import java.util.List;

import javax.swing.*;

import org.apache.pdfbox.PDFReader;
import org.apache.pdfbox.pdfviewer.PageWrapper;
import org.apache.pdfbox.pdmodel.PDPage;

public class CustomPDFReader extends PDFReader {
/**
 * 
 */
private static final long serialVersionUID = 678451510308887925L;

public CustomPDFReader() {
    super();
}

public void setCurrentFile(String file) {
    try {
        Method m = getClass().getSuperclass().getDeclaredMethod("openPDFFile", 
                new Class<?>[]{String.class, String.class});
        m.setAccessible(true);
        m.invoke(this, file, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String []args) {
    CustomPDFReader reader = new CustomPDFReader();
    // remove menubar
    JMenuBar menu = reader.getJMenuBar();
    menu.setVisible(false);

    JPanel header = new JPanel(new BorderLayout());

    JPanel firstLine = new JPanel();

    JLabel label = new JLabel("Label");
    firstLine.add(label);

    JPanel secondLine = new JPanel();

    JTextField text = new JTextField();
    text.setPreferredSize(new Dimension(100, 30));
    secondLine.add(text);

    JButton button = new JButton("Button");
    button.setPreferredSize(new Dimension(100, 30));
    secondLine.add(button);

    header.add(firstLine, java.awt.BorderLayout.NORTH);
    header.add(secondLine, java.awt.BorderLayout.SOUTH);

    reader.getContentPane().add(header, java.awt.BorderLayout.NORTH);

    // set default opened file
    reader.setCurrentFile("test.pdf");
    reader.showAllPages();
    reader.setVisible(true);
}

private void showAllPages() {
    try {
        Field pages = getClass().getSuperclass().getDeclaredField("pages");
        pages.setAccessible(true);
        List<PDPage> pagesList = (List<PDPage>) pages.get(this);

        Field documentPanel = getClass().getSuperclass().getDeclaredField("documentPanel");
        documentPanel.setAccessible(true);
        JPanel panel = (JPanel) documentPanel.get(this);
        panel.remove(0);
        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();
    }
}

}

PDFBox version is 1.8.10. Sample project you can find there