This has already been answered. You need to include commons-log.

Tilman

Am 20.05.2019 um 08:15 schrieb gaurav mundhada:
I am trying to use pdfbox for my java program while using it i was facing
error.
I was supposed to open the pdfbox in jpanel in java swing but i was getting
as output was only a screen and on my netbean output console.
do you have any example or document regarding how to use the pdfbox.


HERE is code :

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("110004336_06.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();
     }
}
}


OUTPUT :

1

2

java.lang.reflect.InvocationTargetException

3

             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

4

             at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

             at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

             at java.lang.reflect.Method.invoke(Method.java:498)

             at SimpleApplication.setCurrentFile(SimpleApplication.java:34)

             at SimpleApplication.main(SimpleApplication.java:71)

Caused by: java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory

             at
org.apache.pdfbox.pdmodel.PDDocument.<clinit>(PDDocument.java:97)

             at org.apache.pdfbox.PDFReader.parseDocument(PDFReader.java:393)

             at org.apache.pdfbox.PDFReader.openPDFFile(PDFReader.java:330)

             ... 6 more

Caused by: java.lang.ClassNotFoundException:
org.apache.commons.logging.LogFactory

             at java.net.URLClassLoader.findClass(URLClassLoader.java:382)

             at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

             at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)

             at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

             ... 9 more

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0

             at java.awt.Container.remove(Container.java:1205)

             at SimpleApplication.showAllPages(SimpleApplication.java:88)

             at SimpleApplication.main(SimpleApplication.java:73)

BUILD SUCCESSFUL (total time: 5 seconds)



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to