📄 pdfviewer.java
字号:
/*** $Id: PDFViewer.java,v 1.3 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.awt.BorderLayout;import java.awt.Container;import java.io.InputStream;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import javax.activation.DataHandler;import javax.activation.CommandObject;import javax.swing.JPanel;import javax.swing.JTextArea;/** * Class PDFViewer implements a CommandObject for the Java Activation Framework. * PDFView tries to instantiate com.adobe.acrobat.Viewer from the Adobe library, * which is possible if the path to the library is included in CLASSPATH. * <p> * The Adobe library can be obtained directly from www.adobe.com. Look for the Java * viewer, and then the bean library. * <p> * The only verb supported is "view", loading the contents of the PDF file into * a JPanel for display. And as you've already guessed, allows the contents to * be viewed by activating the panel. * * @see QuickViewer */public class PDFViewer extends JPanel implements CommandObject{ /** * Construct an empty viewer, which can be loaded later. */ public PDFViewer() { super(); setLayout( new BorderLayout() ); } /** * Sets the contents of the viewer from the given content and type. * <p> * One other point is that the contentType passed is MIME formatted and * can contain a charset parameter which is set to a MIME value. This * needs to be converted to a JAVA value before it can be used. * <p> * @param content data contents, possibly character encoded * @param contentType MIME formatted Content-Type header string */ public void setContent( InputStream content, String contentType ) { try { Container xcontainer = (Container)instantiate( "com.adobe.acrobat.Viewer" ); Object[] xparams = new Object[1]; xparams[0] = content; Class[] xparamtypes = new Class[1]; xparamtypes[0] = Class.forName( "java.io.InputStream" ); invoke( xcontainer, "setDocumentInputStream", xparamtypes, xparams ); invoke( xcontainer, "activate", null, null ); add( xcontainer, BorderLayout.CENTER ); } catch ( Exception xex ) { System.out.println( "invoke() exception: " + xex.toString() ); add( new JTextArea( "Exception: " + xex.toString() + '\n' + "Try getting the Adobe PDF bean from www.adobe.com to view this file type" ) ); } } /** * Initialize the command object from the data handler as described by the verb. * <p> * Implements CommandObject.setCommandContext(). * <p> * @param verb the command from the activation framework * @param dh the datahandler containing the data contents and type * @see javax.activation.CommandObject */ public void setCommandContext( String verb, DataHandler dh ) throws IOException { if ( verb == null || dh == null ) throw new IOException( "Invalid parameters to PDFViewer.setCommandContext()" ); if ( ! verb.equalsIgnoreCase( "view" ) ) throw new IOException( "Only 'view' accepted to PDFViewer.setCommandContext()" ); String contentType = dh.getContentType(); InputStream content = dh.getInputStream(); setContent( content, contentType ); content.close(); } /** * Instantiate an object by name. * Objects can only be instantiated if it has a constructor without parameters. * * @param classname the name of the class to instantiate * @return an instantiatation of the class */ static Object instantiate( String classname ) throws ClassNotFoundException, InstantiationException, IllegalAccessException { try { Class xclass = Class.forName( classname ); return xclass.newInstance(); } catch ( NoClassDefFoundError xex ) { throw new ClassNotFoundException( xex.toString() ); } } /** * Invoke a method by name on the given object. * * @param object the object of which to invoke the method * @param methodname the name of the method to invoke * @param parameters null, or the specific objects to pass when invoking the method * @return the object returned by the invoked method, if any */ static Object invoke( Object object, String methodname, Class[] paramTypes, Object[] parameters ) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { // locate the method// Class[] xtypes = null;// if ( parameters != null ) {// xtypes = new Class[parameters.length];// int xindex;// for ( xindex = 0; xindex < parameters.length; xindex++ ) {// xtypes[xindex] = parameters[xindex].getClass();// }// } Method xmethod = object.getClass().getMethod( methodname, paramTypes ); // invoke the method return xmethod.invoke( object, parameters ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -