Displaying PDF Documents

Displaying PDF Documents

Need the ability to view a PDF document within a Javaapplet? Daniel F. Savarese, answered this question in the March 2000 issue of Java Pro. His solution involves using Adobe’s Acrobat for Java, which is available from Adobe.

My solution involves using the showDocument() method of the public interface AppletContext. Note that there are some necessary assumptions for this solution to work properly:

  • Need a browser that has a plug-in for Adobe Reader.
  • Appletviewer cannot be used, since showDocument() is ignored

Here’s the code:

 package play.pdf;import java.applet.*;import java.awt.*;import;/** * Demonstrate how to view a PDF document from a Java applet. * Makes use of showDocument(), which is described in AppletContext. * 

* Assumptions: *

  1. The applet must be run from a browser, such as Internet Explorer or Netscape Communicator.
  2. *
  3. The browser must have an Adobe Reader plug-in.
  4. *
  5. The technique does not work using appletviewer.
  6. *

* This type was created in VisualAge. * * @author Gerald Hurdle * @version 1.0 */public class PDFViewer extends Applet { Font font = new Font("Dialog", Font.BOLD, 24); String str = "PDF Viewer"; int xPos = 5;/** * Returns information about this applet. * @return a string of information about this applet */public String getAppletInfo() { return "PDFViewer " + " " + "This type was created in VisualAge. " + "";}/** * Draws the text on the drawing area. * @param g the specified Graphics window */public void paint(Graphics g) { g.setFont(font); g.setColor(; g.drawString(str, xPos, 50);}/** * Called to start the applet. You never need to call this method * directly, it is called when the applet's document is visited. * @see #init * @see #stop * @see #destroy */public void start() { super.start(); // insert any code to be run when the applet starts here try { URL url = new URL( "" ); this.getAppletContext().showDocument( url, "_blank" ); } catch (Exception e) { System.err.println( "Error: Could not display PDF document!" ); }}

See also  How AI Art Generators Can Save Time and Money for Startups

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist