How to display PDF file using PDFBox in JPanel?

I've already created a JForm in netbeans that can read PDF using PDFBox. But the problem is that I was using PDPage.convertToImage () method which is really very slow. Can anyone help me to display PDF with PDFBox in JPanel with faster speed?

The code I wrote is inside the ActionListener for the JButton.

File f = null;
ArrayList<JLabel> jl = new ArrayList<JLabel>();
BufferedImage bi = null;
JFileChooser fc = new JFileChooser();
int x=fc.showOpenDialog(null);
if(x==JFileChooser.APPROVE_OPTION)
{
    f=fc.getSelectedFile();
}
        PDDocument doc=null;
    try {
        doc = PDDocument.load(f);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "not done\n"+ex);


    }
    List pages = doc.getDocumentCatalog().getAllPages();
    Iterator itr = pages.iterator();
    int q=0;
    while(itr.hasNext())
    {
        PDPage page = (PDPage)itr.next();
        try
        {
            bi = page.convertToImage();
            q++;
            jl.add(new JLabel(new ImageIcon(bi)));
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }
    itr = jl.iterator();
    while(itr.hasNext())
    {
        viewPanel.setVisible(false);
        viewPanel.add((JLabel)itr.next());
        viewPanel.setVisible(true);
    }
    JOptionPane.showMessageDialog(null, "done");

      

+3


source to share


1 answer


NetBeans has several plugins for displaying PDF files

http://plugins.netbeans.org/plugin/5809/java-pdf-reader http://plugins.netbeans.org/plugin/11676/netbeans-pdfviewer http://plugins.netbeans.org/plugin/17/ pdf-viewer-javafx-converter-and-bookmarking-application



Have you tried any of them?

-2


source







All Articles