📄 printerjob.java
字号:
/* * @(#)PrinterJob.java 1.34 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.print;import java.awt.AWTError;import java.awt.HeadlessException;import java.util.Enumeration;import javax.print.DocFlavor;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.print.StreamPrintServiceFactory;import javax.print.attribute.PrintRequestAttributeSet;import sun.security.action.GetPropertyAction;/** * The <code>PrinterJob</code> class is the principal class that controls * printing. An application calls methods in this class to set up a job, * optionally to invoke a print dialog with the user, and then to print * the pages of the job. */public abstract class PrinterJob { /* Public Class Methods */ /** * Creates and returns a <code>PrinterJob</code> which is initially * associated with the default printer. * If no printers are available on the system, a PrinterJob will still * be returned from this method, but <code>getPrintService()</code> * will return <code>null</code>, and calling * {@link #print() print} with this <code>PrinterJob</code> might * generate an exception. Applications that need to determine if * there are suitable printers before creating a <code>PrinterJob</code> * should ensure that the array returned from * {@link #lookupPrintServices() lookupPrintServices} is not empty. * @return a new <code>PrinterJob</code>. */ public static PrinterJob getPrinterJob() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } return (PrinterJob) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { String nm = System.getProperty("java.awt.printerjob", null); try { return (PrinterJob)Class.forName(nm).newInstance(); } catch (ClassNotFoundException e) { throw new AWTError("PrinterJob not found: " + nm); } catch (InstantiationException e) { throw new AWTError("Could not instantiate PrinterJob: " + nm); } catch (IllegalAccessException e) { throw new AWTError("Could not access PrinterJob: " + nm); } } }); } /** * A convenience method which looks up 2D print services. * Services returned from this method may be installed on * <code>PrinterJob</code>s which support print services. * Calling this method is equivalent to calling * {@link javax.print.PrintServiceLookup#lookupPrintServices( * DocFlavor, AttributeSet) * <code>PrintServiceLookup.lookupPrintServices()</code>} * and specifying a Pageable DocFlavor. * @return a possibly empty array of 2D print services. * @since 1.4 */ public static PrintService[] lookupPrintServices() { return PrintServiceLookup. lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); } /** * A convenience method which locates factories for stream print * services which can image 2D graphics. * Sample usage : * <pre> * FileOutputStream outstream; * StreamPrintService psPrinter; * String psMimeType = "application/postscript"; * * StreamPrintServiceFactory[] factories = * PrinterJob.lookupStreamPrintServices(psMimeType); * if (factories.length > 0) { * try { * outstream = new File("out.ps"); * psPrinter = factories[0].getPrintService(fos); * // psPrinter can now be set as the service on a PrinterJob * } catch (FileNotFoundException e) { * } * } * </pre> * Services returned from this method may be installed on * <code>PrinterJob</code> instances which support print services. * Calling this method is equivalent to calling * {@link javax.print.StreamPrintServiceFactory#lookupStreamPrintServiceFactories(DocFlavor, String) * <code>StreamPrintServiceFactory.lookupStreamPrintServiceFactories() * </code>} and specifying a Pageable DocFlavor. * * @param mimeType the required output format, or null to mean any format. * @return a possibly empty array of 2D stream print service factories. * @since 1.4 */ public static StreamPrintServiceFactory[] lookupStreamPrintServices(String mimeType) { return StreamPrintServiceFactory.lookupStreamPrintServiceFactories( DocFlavor.SERVICE_FORMATTED.PAGEABLE, mimeType); } /* Public Methods */ /** * A <code>PrinterJob</code> object should be created using the * static {@link #getPrinterJob() <code>getPrinterJob</code>} method. */ public PrinterJob() { } /** * Returns the service (printer) for this printer job. * Implementations of this class which do not support print services * may return null; * @return the service for this printer job. * @see #setPrintService(PrintService) * @since 1.4 */ public PrintService getPrintService() { return null; } /** * Associate this PrinterJob with a new PrintService. * This method is overridden by subclasses which support * specifying a Print Service. * * Throws <code>PrinterException</code> if the specified service * cannot support the <code>Pageable</code> and * <code>Printable</code> interfaces necessary to support 2D printing. * @param service a print service that supports 2D printing * @exception PrinterException if the specified service does not support * 2D printing, or this PrinterJob class does not support * setting a 2D print service, or the specified service is * otherwise not a valid print service. * @see #getPrintService * @since 1.4 */ public void setPrintService(PrintService service) throws PrinterException { throw new PrinterException( "Setting a service is not supported on this class"); } /** * Calls <code>painter</code> to render the pages. The pages in the * document to be printed by this * <code>PrinterJob</code> are rendered by the {@link Printable} * object, <code>painter</code>. The {@link PageFormat} for each page * is the default page format. * @param painter the <code>Printable</code> that renders each page of * the document. */ public abstract void setPrintable(Printable painter); /** * Calls <code>painter</code> to render the pages in the specified * <code>format</code>. The pages in the document to be printed by * this <code>PrinterJob</code> are rendered by the * <code>Printable</code> object, <code>painter</code>. The * <code>PageFormat</code> of each page is <code>format</code>. * @param painter the <code>Printable</code> called to render * each page of the document * @param format the size and orientation of each page to * be printed */ public abstract void setPrintable(Printable painter, PageFormat format); /** * Queries <code>document</code> for the number of pages and * the <code>PageFormat</code> and <code>Printable</code> for each * page held in the <code>Pageable</code> instance, * <code>document</code>. * @param document the pages to be printed. It can not be * <code>null</code>. * @exception NullPointerException the <code>Pageable</code> passed in * was <code>null</code>. * @see PageFormat * @see Printable */ public abstract void setPageable(Pageable document) throws NullPointerException; /** * Presents a dialog to the user for changing the properties of * the print job. * This method will display a native dialog if a native print * service is selected, and user choice of printers will be restricted * to these native print services. * To present the cross platform print dialog for all services, * including native ones instead use * <code>printDialog(PrintRequestAttributeSet)</code>. * <p> * PrinterJob implementations which can use PrintService's will update * the PrintService for this PrinterJob to reflect the new service * selected by the user. * @return <code>true</code> if the user does not cancel the dialog; * <code>false</code> otherwise. * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless */ public abstract boolean printDialog() throws HeadlessException; /** * A convenience method which displays a cross-platform print dialog * for all services which are capable of printing 2D graphics using the * <code>Pageable</code> interface. The selected printer when the * dialog is initially displayed will reflect the print service currently * attached to this print job. * If the user changes the print service, the PrinterJob will be * updated to reflect this, unless the user cancels the dialog. * As well as allowing the user to select the destination printer, * the user can also select values of various print request attributes. * <p> * The attributes parameter on input will reflect the applications * required initial selections in the user dialog. Attributes not * specified display using the default for the service. On return it * will reflect the user's choices. Selections may be updated by * the implementation to be consistent with the supported values * for the currently selected print service.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -