Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Beginner
Mar 30, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Displaying PDF Documents

Need the ability to view a PDF document within a Java applet? 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\n" + "\n" + "This type was created in VisualAge.\n" + ""; } /** * 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!" ); } }

Gerald C.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date