Class PrinterJob
PrinterJob 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.- 
Constructor SummaryConstructorsConstructorDescriptionAPrinterJobobject should be created using the staticgetPrinterJobmethod.
- 
Method SummaryModifier and TypeMethodDescriptionabstract voidcancel()Cancels a print job that is in progress.Creates a newPageFormatinstance and sets it to a default size and orientation.abstract PageFormatdefaultPage(PageFormat page) Clones thePageFormatargument and alters the clone to describe a default page size and orientation.abstract intGets the number of copies to be printed.abstract StringGets the name of the document to be printed.getPageFormat(PrintRequestAttributeSet attributes) Calculates aPageFormatwith values consistent with those supported by the currentPrintServicefor this job (ie the value returned bygetPrintService()) and media, printable area and orientation contained inattributes.static PrinterJobCreates and returns aPrinterJobwhich is initially associated with the default printer.Returns the service (printer) for this printer job.abstract StringGets the name of the printing user.abstract booleanReturnstrueif a print job is in progress, but is going to be cancelled at the next opportunity; otherwise returnsfalse.static PrintService[]A convenience method which looks up 2D print services.static StreamPrintServiceFactory[]lookupStreamPrintServices(String mimeType) A convenience method which locates factories for stream print services which can image 2D graphics.abstract PageFormatpageDialog(PageFormat page) Displays a dialog that allows modification of aPageFormatinstance.pageDialog(PrintRequestAttributeSet attributes) A convenience method which displays a cross-platform page setup dialog.abstract voidprint()Prints a set of pages.voidprint(PrintRequestAttributeSet attributes) Prints a set of pages using the settings in the attribute set.abstract booleanPresents a dialog to the user for changing the properties of the print job.booleanprintDialog(PrintRequestAttributeSet attributes) A convenience method which displays a cross-platform print dialog for all services which are capable of printing 2D graphics using thePageableinterface.abstract voidsetCopies(int copies) Sets the number of copies to be printed.abstract voidsetJobName(String jobName) Sets the name of the document to be printed.abstract voidsetPageable(Pageable document) Queriesdocumentfor the number of pages and thePageFormatandPrintablefor each page held in thePageableinstance,document.abstract voidsetPrintable(Printable painter) Callspainterto render the pages.abstract voidsetPrintable(Printable painter, PageFormat format) Callspainterto render the pages in the specifiedformat.voidsetPrintService(PrintService service) Associate this PrinterJob with a new PrintService.abstract PageFormatvalidatePage(PageFormat page) Returns the clone ofpagewith its settings adjusted to be compatible with the current printer of thisPrinterJob.
- 
Constructor Details- 
PrinterJobpublic PrinterJob()APrinterJobobject should be created using the staticgetPrinterJobmethod.
 
- 
- 
Method Details- 
getPrinterJobCreates and returns aPrinterJobwhich is initially associated with the default printer. If no printers are available on the system, a PrinterJob will still be returned from this method, butgetPrintService()will returnnull, and callingprintwith thisPrinterJobmight generate an exception. Applications that need to determine if there are suitable printers before creating aPrinterJobshould ensure that the array returned fromlookupPrintServicesis not empty.- Returns:
- a new PrinterJob.
 
- 
lookupPrintServicesA convenience method which looks up 2D print services. Services returned from this method may be installed onPrinterJobs which support print services. Calling this method is equivalent to callingPrintServiceLookup.lookupPrintServices()and specifying a Pageable DocFlavor.- Returns:
- a possibly empty array of 2D print services.
- Since:
- 1.4
 
- 
lookupStreamPrintServicesA convenience method which locates factories for stream print services which can image 2D graphics. Sample usage :
 Services returned from this method may be installed onFileOutputStream outstream; StreamPrintService psPrinter; String psMimeType = "application/postscript"; PrinterJob pj = PrinterJob.getPrinterJob(); StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(psMimeType); if (factories.length > 0) { try { outstream = new File("out.ps"); psPrinter = factories[0].getPrintService(outstream); // psPrinter can now be set as the service on a PrinterJob pj.setPrintService(psPrinter) } catch (Exception e) { e.printStackTrace(); } }PrinterJobinstances which support print services. Calling this method is equivalent to callingStreamPrintServiceFactory.lookupStreamPrintServiceFactories()and specifying a Pageable DocFlavor.- Parameters:
- mimeType- the required output format, or null to mean any format.
- Returns:
- a possibly empty array of 2D stream print service factories.
- Since:
- 1.4
 
- 
getPrintServiceReturns the service (printer) for this printer job. Implementations of this class which do not support print services may return null. null will also be returned if no printers are available.- Returns:
- the service for this printer job.
- Since:
- 1.4
- See Also:
 
- 
setPrintServiceAssociate this PrinterJob with a new PrintService. This method is overridden by subclasses which support specifying a Print Service. ThrowsPrinterExceptionif the specified service cannot support thePageableandPrintableinterfaces necessary to support 2D printing.- Parameters:
- service- a print service that supports 2D printing
- Throws:
- 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.
- Since:
- 1.4
- See Also:
 
- 
setPrintableCallspainterto render the pages. The pages in the document to be printed by thisPrinterJobare rendered by thePrintableobject,painter. ThePageFormatfor each page is the default page format.- Parameters:
- painter- the- Printablethat renders each page of the document.
 
- 
setPrintableCallspainterto render the pages in the specifiedformat. The pages in the document to be printed by thisPrinterJobare rendered by thePrintableobject,painter. ThePageFormatof each page isformat.- Parameters:
- painter- the- Printablecalled to render each page of the document
- format- the size and orientation of each page to be printed
 
- 
setPageableQueriesdocumentfor the number of pages and thePageFormatandPrintablefor each page held in thePageableinstance,document.- Parameters:
- document- the pages to be printed. It can not be- null.
- Throws:
- NullPointerException- the- Pageablepassed in was- null.
- See Also:
 
- 
printDialogPresents 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 useprintDialog(PrintRequestAttributeSet).PrinterJob implementations which can use PrintService's will update the PrintService for this PrinterJob to reflect the new service selected by the user. - Returns:
- trueif the user does not cancel the dialog;- falseotherwise.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- See Also:
 
- 
printDialogA convenience method which displays a cross-platform print dialog for all services which are capable of printing 2D graphics using thePageableinterface. 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.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. As the user scrolls to a new print service selection, the values copied are based on the settings for the previous service, together with any user changes. The values are not based on the original settings supplied by the client. With the exception of selected printer, the PrinterJob state is not updated to reflect the user's changes. For the selections to affect a printer job, the attributes must be specified in the call to the print(PrintRequestAttributeSet)method. If using the Pageable interface, clients which intend to use media selected by the user must create a PageFormat derived from the user's selections. If the user cancels the dialog, the attributes will not reflect any changes made by the user.- Parameters:
- attributes- on input is application supplied attributes, on output the contents are updated to reflect user choices. This parameter may not be null.
- Returns:
- trueif the user does not cancel the dialog;- falseotherwise.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- NullPointerException- if- attributesparameter is null.
- Since:
- 1.4
- See Also:
 
- 
pageDialogDisplays a dialog that allows modification of aPageFormatinstance. Thepageargument is used to initialize controls in the page setup dialog. If the user cancels the dialog then this method returns the originalpageobject unmodified. If the user okays the dialog then this method returns a newPageFormatobject with the indicated changes. In either case, the originalpageobject is not modified.- Parameters:
- page- the default- PageFormatpresented to the user for modification
- Returns:
- the original pageobject if the dialog is cancelled; a newPageFormatobject containing the format indicated by the user if the dialog is acknowledged.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- Since:
- 1.2
- See Also:
 
- 
pageDialogA convenience method which displays a cross-platform page setup dialog. The choices available will reflect the print service currently set on this PrinterJob.The attributes parameter on input will reflect the client's required initial selections in the user dialog. Attributes which are 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. The return value will be a PageFormat equivalent to the selections in the PrintRequestAttributeSet. If the user cancels the dialog, the attributes will not reflect any changes made by the user, and the return value will be null. - Parameters:
- attributes- on input is application supplied attributes, on output the contents are updated to reflect user choices. This parameter may not be null.
- Returns:
- a page format if the user does not cancel the dialog;
 nullotherwise.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- NullPointerException- if- attributesparameter is null.
- Since:
- 1.4
- See Also:
 
- 
defaultPageClones thePageFormatargument and alters the clone to describe a default page size and orientation.- Parameters:
- page- the- PageFormatto be cloned and altered
- Returns:
- clone of page, altered to describe a defaultPageFormat.
 
- 
defaultPageCreates a newPageFormatinstance and sets it to a default size and orientation.- Returns:
- a PageFormatset to a default size and orientation.
 
- 
getPageFormatCalculates aPageFormatwith values consistent with those supported by the currentPrintServicefor this job (ie the value returned bygetPrintService()) and media, printable area and orientation contained inattributes.Calling this method does not update the job. It is useful for clients that have a set of attributes obtained from printDialog(PrintRequestAttributeSet attributes)and need a PageFormat to print a Pageable object.- Parameters:
- attributes- a set of printing attributes, for example obtained from calling printDialog. If- attributesis null a default PageFormat is returned.
- Returns:
- a PageFormatwhose settings conform with those of the current service and the specified attributes.
- Since:
- 1.6
 
- 
validatePageReturns the clone ofpagewith its settings adjusted to be compatible with the current printer of thisPrinterJob. For example, the returnedPageFormatcould have its imageable area adjusted to fit within the physical area of the paper that is used by the current printer.- Parameters:
- page- the- PageFormatthat is cloned and whose settings are changed to be compatible with the current printer
- Returns:
- a PageFormatthat is cloned frompageand whose settings are changed to conform with thisPrinterJob.
 
- 
printPrints a set of pages.- Throws:
- PrinterException- an error in the print system caused the job to be aborted.
- See Also:
 
- 
printPrints a set of pages using the settings in the attribute set. The default implementation ignores the attribute set.Note that some attributes may be set directly on the PrinterJob by equivalent method calls, (for example), copies: setCopies(int), job name:setJobName(String)and specifying media size and orientation though thePageFormatobject.If a supported attribute-value is specified in this attribute set, it will take precedence over the API settings for this print() operation only. The following behaviour is specified for PageFormat: If a client uses the Printable interface, then the attributesparameter to this method is examined for attributes which specify media (by size), orientation, and imageable area, and those are used to construct a new PageFormat which is passed to the Printable object's print() method. SeePrintablefor an explanation of the required behaviour of a Printable to ensure optimal printing via PrinterJob. For clients of the Pageable interface, the PageFormat will always be as supplied by that interface, on a per page basis.These behaviours allow an application to directly pass the user settings returned from printDialog(PrintRequestAttributeSet attributesto this print() method.- Parameters:
- attributes- a set of attributes for the job
- Throws:
- PrinterException- an error in the print system caused the job to be aborted.
- Since:
- 1.4
- See Also:
 
- 
setCopiespublic abstract void setCopies(int copies) Sets the number of copies to be printed.- Parameters:
- copies- the number of copies to be printed
- See Also:
 
- 
getCopiespublic abstract int getCopies()Gets the number of copies to be printed.- Returns:
- the number of copies to be printed.
- See Also:
 
- 
getUserNameGets the name of the printing user.- Returns:
- the name of the printing user
 
- 
setJobNameSets the name of the document to be printed. The document name can not benull.- Parameters:
- jobName- the name of the document to be printed
- See Also:
 
- 
getJobNameGets the name of the document to be printed.- Returns:
- the name of the document to be printed.
- See Also:
 
- 
cancelpublic abstract void cancel()Cancels a print job that is in progress. Ifprinthas been called but has not returned then this method signals that the job should be cancelled at the next chance. If there is no print job in progress then this call does nothing.
- 
isCancelledpublic abstract boolean isCancelled()Returnstrueif a print job is in progress, but is going to be cancelled at the next opportunity; otherwise returnsfalse.- Returns:
- trueif the job in progress is going to be cancelled;- falseotherwise.
 
 
-