一个用Java写出的PDF阅读器的源代码

    技术2022-05-11  2

    在运行之前需要在http://www.adobe.com/products/acrviewer/acrvdnld.html?name=accept下载Acrobat Viewer JavaBean API ,将acrobat.jar文件部署到classpath中

    源代码如下:

    package java学习.PDFProcess.AcrobatViewer.Example;

    import java.awt.*;import java.awt.event.*;import java.io.*;

    import com.adobe.acrobat.Viewer;public class SampleReader extends Viewer {

        public SampleReader() throws Exception {    }

        public static void main(String args[]) { Frame f = new Frame("Sample Acrobat Reader");

     f.setLayout(new BorderLayout()); Label top = new Label("Acrobat Reader created using adobe.Acrobat.Viewer", Label.CENTER); top.setBackground(Color.red); f.add(top, BorderLayout.NORTH); f.add(new Label("Adobe Acrobat Reader - Alpha release - 1998", Label.CENTER), BorderLayout.SOUTH); try {

         // Construct a acrobat object aka Acrobar Reader     // note that you must also call its activate     // method before you show the containing panel,     // in this case the frame object.

         // The acrobat object is declared as final     // so that it could be referenced in the     // following windowClosing method.

         final Viewer acrobat = new Viewer();

         f.addWindowListener(new WindowAdapter() {  public void windowClosing(WindowEvent e) {

          if (acrobat != null) {

       // The deactivate method will ensure that the   // acrobat.properties file is saved   // upon exit.

       acrobat.deactivate();      }

          System.exit(0);  }     });

         if (args.length > 0) {  try {

          // assumes that args[0] is the name of a file

          FileInputStream in = new FileInputStream(args[0]);      acrobat.setDocumentInputStream(in);

      } catch (FileNotFoundException x) {      System.out.println("File not found!");      // The viewer will display a blank screen.      // You can then use the Viewer's pop-up menu      // to open a local or remote PDF file.  }     }

         f.add(acrobat, BorderLayout.CENTER);

         // you must call activate to enable the Viewer object     // to layout its sub-components and the further initialization     // needed for it to be displayed.

         acrobat.activate(); //WithoutBars();

     } catch (Exception x) {     f.add(new Label("Unable to create an Acrobat Reader"), "Center"); }

     f.setSize(400, 400); f.show();

        }}

     

    转自

    http://blog.donews.com/karim/archive/2006/02/19/732857.aspx


    最新回复(0)