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 java.net.URL;/** * 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(Color.black); 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( "http://www.somecomputer.com/MyPDFDocument.pdf" ); this.getAppletContext().showDocument( url, "_blank" ); } catch (Exception e) { System.err.println( "Error: Could not display PDF document!" ); }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS