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 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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as