View a PDF file with the Acrobat viewer JavaBean API

Fortunately, Acrobat has developed a API to view a PDF document in Java. Acrobat viewer allows you to open and print PDF files from a Java application, JavaBean or Java applets. In this tutorial, we're going to integrate Acrobat viewer into our application made with JFrame.

The api  Acrobat viewer is available in  http://www.java2s.com/Code/JarDownload/acrobat/acrobat-1.1.jar.zip. Download it and extract the Acrobat.jar file into your project, then import the library into your Eclipse IDE or Netbeans.

Now, how do you open a PDF file? The Java Beans API provides simple-to-follow and easy-to-view steps to view a PDF document in the Java application. Here are the steps for creating the PDF reader:
  1. Create the JFrame whose Acrobat viewer will be added;
  2. Create Viewer object(a class of  java.awt.Component)  to view the PDF and add it to JFrame:
    • Viewer viewer = new Viewer();
    • frame.add(viewer, BorderLayout.CENTER);
    1. Select Document
      • FileInputStream fis = new FileInputStream(filename);
      1. Define FileInputStream  as an argument in viewer:
        • viewer.setDocumentInputStream(fis);
        1. Create the layout that makes the document appear:
          • viewer.activate();
        The file path can also be indicated by the method:

        setDocumentURL(java.lang.String url)
        Example:

        import java.awt.BorderLayout; 
        import java.io.FileInputStream;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import com.adobe.acrobat.Viewer;
        /*
        * www.codeurjava.com
        */
        public class readerPDF extends JPanel{

        private Viewer viewer;

        public readerPDF(String, filename) throws Exception{
        this.setLayout(new BorderLayout());

        //create the viewer that will be used to display the content of the pdf
        viewer = new Viewer();
        this.add(viewer, BorderLayout.CENTER);
        FileInputStream fis = new FileInputStream(filename);
        viewer.setDocumentInputStream(fis);
        viewer.activate();
        }

        public static void main(String[] args) throws Exception {

        String filename = "delphi.pdf";
        readerPDF reader = new readerPDF(filename);
        //create the JFrame
        JFrame f = new JFrame("PDF Reader");
        f.setSize(1024,768);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.getContentPane().add(drive);
        }
        }
        View a PDF file in java with acrobat viewer

        Retrieve page count and current page

        Page number and current page are obtained with the following methods: getPageCount() and  getCurrentPage(). For example, we want to display the total number of pages and the number of the current page:

        System.out.println("Number of pages: "+viewer.getPageCount()); 
        System.out.println("Current page: "+viewer.getCurrentPage());
        Page Count: 50
        Current Page: 0

        Change Zoom

        To adjust the zoom level of the current page, use the zoomTo(double). The 100% zoom corresponds to 1.0 in the method zoomTo().

        viewer.zoomTo(0.5); 

        Get PDF Title in Java

        The title is obtained with the getTitle() 

        PDFBookmark bookmark=viewer.getRootBookmark(); 
        String title=bookmark.getTitle();
        Reference