| ||||||||||||||||
A PDF document is commonly opened in Acrobat Reader to edit,
view, and print. But when you need to open a PDF in a Java
application, Acrobat Reader is not accessible. Fortunately, Adobe
provides a viewer API, the Acrobat Viewer JavaBean, to view and
print PDF documents from Java applications, JavaBeans, or Java
applets. The Acrobat classes are provided in the
com.adobe.acrobat package of the Acrobat Viewer
distribution. In this tutorial, we shall integrate the Acrobat
Viewer in a Java application. We'll open an example PDF document in
the application with the Acrobat Viewer, and discuss the different
features of the Acrobat Viewer.
This tutorial has the following sections:
The Acrobat Viewer JavaBean API is available from the
Acrobat Viewer JavaBean [17] page. Download the bean.zip
file. The Acrobat Viewer is available for Windows, Macintosh,
Solaris, UNIX, and OS/2 platforms. For the purposes of this
tutorial, we'll consider the Acrobat Viewer to be installed on the
Windows platform. For installation on other platforms, refer to the
installation notes for the specific platform. Extract the
bean.zip file to an installation directory. The
Acrobat Viewer for JavaBean API classes are included in the
%AcrobatViewer%/acrobat.jar file, where the
%AcrobatViewer% environment variable represents the
directory in which the Acrobat Viewer JavaBean distribution is
installed. Add %AcrobatViewer%/acrobat.jar and
%AcrobatViewer%/MRJToolkitStubs.zip to the
CLASSPATH environment variable.
A Java application with the Acrobat Viewer API may be developed in an IDE such as Eclipse, JDeveloper, or NetBeans, or as a command-line application. In this tutorial, a sample Acrobat Viewer Java application is developed as a command-line application. JRE 1.1.8 or later is recommended for the Acrobat Viewer.
Having installed the Acrobat Viewer API, we shall open an example PDF document in Acrobat Viewer. The Adobe Document Services PDF [18] document is used as the example document. Store the example PDF document in the C:/Adobe directory.
The Acrobat Viewer for JavaBean API provides methods for
displaying a PDF document from a Java application. First, import
the com.adobe.acrobat package. Also import the
java.awt and java.io packages, which will
be used for creating a GUI and loading the PDF file,
respectively.
import com.adobe.acrobat.*;
import java.awt.*;
import java.io.*;
Create a Frame object, to which the Acrobat Viewer
will be added, and set the layout of the frame. Specify
BorderLayout as the layout of the frame.
Frame frame = new Frame("PDF Viewer");
frame.setLayout(new BorderLayout());
Create a Viewer object, a subclass of
java.awt.Component, to display the PDF document.
Viewer viewer = new Viewer();
Add the Viewer object to the frame. Add the
Viewer to the center of the frame.
frame.add(viewer, BorderLayout.CENTER);
Open a FileInputStream to the PDF document that is
to be displayed in the Acrobat Viewer.
InputStream input =
new FileInputStream (new File(
"C:/Adobe/95004509_AcroDS_SB_UE.pdf"));
Set the FileInputStream as the input to the Acrobat
Viewer.
viewer.setDocumentInputStream(input);
A PDF document may also be input with the
setDocumentURL(java.lang.String docURL) method.
Acrobat Viewer has several properties to specify the display characteristics of a PDF document. The viewer properties are of two types: static and dynamic. Static properties are set before a viewer is displayed and may not be modified after the viewer is displayed. Dynamic properties may be set and modified after a viewer is displayed. Some of the properties that may be set for the Acrobat Viewer are listed in the following table.
| Property | Description | Type |
Default_Page_Layout |
The page layout of the PDF document. Values that may specified
are: SinglePage, OneColumn, TwoColumn (same as
TwoColumnRight), TwoColumnLeft, and TwoColumnRight. |
Static |
Default_Zoom_Type |
The zoom type of the PDF document. Values that may be
specified are: FixedZoom, FitPage, FitVisible, FitWidth,
FitVisibleWidth, FitHeight, and FitVisibleHeight. |
Static |
Default_Magnification |
The percent of magnification, with values in the range of 25-800. | Static |
Max_Magnification |
The maximum magnification, with values in the range of 25-800. | Static |
Page_Units |
Page units may be set to Points, Millimeters, or Inches. |
Dynamic |
Display_Large |
Boolean value. Set to display large images in a PDF document. | Dynamic |
Server_Printers |
String value specifying the relative path to get a list of printers. | Dynamic |
Server_Print |
String value specifying the relative path to print a document. | Dynamic |
To set the page layout to SinglePage, the zoom type
to FitPage, and the magnification to 100 percent, you just make a
series of calls to setProperty():
viewer.setProperty("Default_Page_Layout",
"SinglePage");
viewer.setProperty("Default_Zoom_Type",
"FitPage");
viewer.setProperty("Default_Magnification",
"100");
The number of pages in the PDF document and the current page are
obtained with the getPageCount() and
getCurrentPage() methods. As an example, print out the
number of pages in the example document and the current page
displayed.
System.out.println("Page Count: "+viewer.getPageCount());
System.out.println("Current Page: "+viewer.getCurrentPage());
Page 0 corresponds to the first page in the document.
You can set the zoom level of the current page with the
zoomTo() method. Set the zoom magnification to 100 percent by
specifying 1.0 as a double in the zoomTo() method.
viewer.zoomTo(1.0);
Create the layout of the viewer components and activate the viewer as follows:
viewer.activate();
Next, set the frame size in which the viewer is to be added, and display the frame.
frame.setSize(400, 500);
frame.pack();
frame.show();
You'll find the example application PDFViewer.java in the Resources [19] section. Run the Java application in a command-line window. The number of pages for the example PDF document is 3 and the current page is 0, the index of the first page of the PDF document. The example PDF document gets displayed in the Acrobat Viewer. As the zoom level is set to 1.0 in the Java application, the PDF document is displayed with 1.0 magnification. Figure 1 illustrates the PDF document in Acrobat Viewer.

Figure 1. PDF Document in Acrobat Viewer
The Acrobat Viewer provides some viewer commands to edit the
document and to modify the view characteristics of the document.
The viewer commands are specified in the ViewerCommand
interface, which is implemented by the Viewer class.
To run a viewer command, invoke the
execMenuItem(java.lang.String viewerCommand) method of
the Viewer class. For example, to run the
ZoomTo_K command:
Viewer viewer=new Viewer();
viewer.execMenuItem(ViewerCommand.ZoomTo_K);
Some of the commonly used viewer commands are listed in the following table:
| Viewer Command | Description |
FitPage_K |
Displays document to fit page. |
FitHeight_K |
Displays document to fit height. |
FitWidth_K |
Displays document to fit width. |
OpenURL_K |
Displays the URL selection dialog. |
Open_K |
Displays the file selection dialog. |
PageOnly_K |
Displays the page without the bookmarks. |
Print_K |
Displays the Print Document dialog. |
PrintSetup_K |
Displays the Print Document dialog. |
ShowBookmarks_K |
Displays the bookmarks. |
TwoColumn_K |
Displays the document in two-column mode. |
ZoomTo_K |
Displays the Zoom To dialog. |
The Acrobat Viewer may be displayed with some of the toolbar buttons removed. For example, you can remove the Open and Open URL buttons in the Acrobat Viewer with:
String[] dis = {ViewerCommand.Open_K, ViewerCommand.OpenURL_K};
Viewer viewer = new Viewer(dis);
The Acrobat Viewer has a toolbar to navigate through the PDF document with Next Page, Last Page, Previous Page, and First Page buttons. A PDF document may be zoomed in or zoomed out with the Zoom In or Zoom Out buttons. The Open and Open URL buttons allow you to open a different PDF document, which replaces the document currently displayed the Java AWT Component.
The Acrobat Viewer GUI functionality may also be accessed with
the com.adobe.acrobat package API from a Java
application. The different viewer commands are invoked with the
execMenuItem(java.lang.String viewerCommand) method of
the Viewer class. The viewer commands corresponding to
some of the Acrobat Viewer buttons are listed in the following
table.
| Acrobat Viewer Button | Viewer Command |
| Next Page | ViewerCommand.NextPage_K |
| Last Page | ViewerCommand.LastPage_K |
| Previous Page | ViewerCommand.PrevPage_K |
| First Page | ViewerCommand.FirstPage_K |
| Zoom In, Zoom Out | ViewerCommand.ZoomTo_K |
| Open | ViewerCommand.Open_K |
| Find | ViewerCommand.Find_K |
In the Viewer Component, you access the File, Edit, View, Tools, and Help menus by right-clicking in the document. This displays the pop-up menu seen in Figure 2.

Figure 2. PDF document menu
The File and Edit menu items may also be invoked with a viewer command in a Java application. Some of the viewer commands corresponding to the File and Edit menu items are listed in the following table.
| Acrobat Viewer Menu Item | Viewer Command |
| Close | ViewerCommand.Close_K |
| Copy | ViewerCommand.EditCopy_K |
| Select All | ViewerCommand.EditSelectAll_K |
To display the PDF document at its actual size, select the
Actual Size button, as illustrated in Figure 3. The Actual Size
button may also be invoked with the viewer command
ViewerCommand.ActualSize_K.

Figure 3. Displaying document at actual size
To fit the PDF document in the AWT Component's
display space, select Fit Page, as illustrated in Figure 4. The Fit
Page button may also be invoked with the viewer command
ViewerCommand.FitPage_K.

Figure 4. Displaying document to fit page
To fit the PDF document to the width of the component, select
the Fit Width button, as illustrated in Figure 5. The Fit Width
button may also be invoked with the viewer command
ViewerCommand.FitWidth_K.

Figure 5. Displaying document to fit page width
To open another PDF document, select the Open URL button, as shown
in Figure 6. The Open URL button may also be invoked with the
viewer command ViewerCommand.OpenURL_K.

Figure 6. Opening a document from a URL
In the Open URL frame, specify the URL of the PDF document, as
illustrated in Figure 7. The input field for the Open URL frame
does not accept a ftp-style URL with an escape
sequence, such as ftp://host/my%20file.pdf. On the other
hand, escapes are OK in http-style URLs.

Figure 7. Open URL
A PDF document page may be bookmarked in the Acrobat Viewer. The Acrobat Viewer bookmark frame displays the bookmarks to a PDF document with links to the different document pages. To bookmark a page, select the Bookmark button, as illustrated in Figure 8.

Figure 8. Bookmarking a document
This adds a bookmark to the Acrobat Viewer, as shown in Figure 9.
You can obtain the root bookmark in the Acrobat Viewer frame from a
Java application with the getRootBookmark() method of
the Viewer class. The PDFBookmark class
represents a bookmark in a PDF document.
Viewer viewer;
PDFBookmark rootBookmark=viewer.getRootBookmark();
Obtain the different bookmarks in a PDF document that is displayed
in the Acrobat Viewer with the getBookmarks() method.
java.util.Vector pdfBookmarks=rootBookmark.getBookmarks();
The title of a bookmark can be obtained with the
getTitle() method.
PDFBookmark bookmark;
java.lang.String title=bookmark.getTitle();

Figure 9. PDF document with a bookmark
In the Acrobat Viewer, text may be highlighted with the Select Text button. Select the Select Text button and select the text to be highlighted, as illustrated in Figure 10.

Figure 10. Selecting text
A Java application may need to display PDF documents. The Adobe Acrobat Viewer for JavaBean API makes it feasible to display a PDF document from a Java application.
Links:
[1] http://www.java.net/author/deepak-vohra
[2] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#preliminary-setup
[3] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#opening-pdf-doc
[4] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#setting-layout
[5] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#setting-document-display
[6] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#setting-viewer-properties
[7] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#getting-current-page
[8] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#setting-zoom-level
[9] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#displaying-the-document
[10] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#using-viewer-commands
[11] http://www.java.net/%3Fpage%3D2%2523displaying
[12] http://www.java.net/%3Fpage%3D2%2523opening-a-document
[13] http://www.java.net/%3Fpage%3D2%2523bookmarking
[14] http://www.java.net/%3Fpage%3D2%2523selecting-text
[15] http://www.java.net/%3Fpage%3D2%2523conclusion
[16] http://www.java.net/%3Fpage%3D2%2523resources
[17] http://www.adobe.com/products/acrviewer/acrvdnld.html?name=Accept
[18] http://www.adobe.com/products/server/pdfs/95004509_AcroDS_SB_UE.pdf
[19] http://www.java.net/article/2005/10/18/accessing-pdf-document-acrobat-viewer-javabean#resources
[20] http://www.java.net/%3Ccs_comment
[21] http://www.java.net/today/2005/10/20/AcrobatViewerResources.zip