About Andreas Pomarolli

Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he has been involved with a large number of research projects ranging from software engineering to data engineering and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web engineering and project management. He currently works as a software engineer in the IT sector where he is mainly involved with projects based on Java, Databases and Web Technologies.

The JavaFX Print API

This is a JavaFX Print Example. JavaFX added support for printing nodes through the Print API in the javafx.print package. The API consists of the following classes:

Printer

PrinterAttributes

PrintResolution

PrinterJob

JobSettings

Paper

PaperSource

PageLayout

PageRange

Instances of the above-listed classes represent different parts of the printing process. For example, a Printer represents a printer that can be used for printing jobs. A PrinterJob represents a print job that can be sent to a Printer for printing. And a Paper represents the paper sizes available on printers.

The Print API provides support for printing nodes that may or may not be attached to a Scene Graph.

If a node is modified during the printing process, the printed node may not appear correct. Note that the printing of a Node may span multiple pulse events resulting in concurrent change in the content being printed. To ensure correct printing, please make sure that the Node being printed is not modified during the print process.

Nodes can be printed on any thread including the JavaFX Application Thread. It is recommended that large, time-consuming print jobs be submitted on a background thread to keep the UI responsive.

Classes in the Print API are final as they represent existing printing device properties. Most of them do not provide any public constructor as you cannot make up a printing device. Rather, you obtain their references using factory methods in various classes.

The Printer.getAllPrinters() static method returns an ObservableList of installed printers on the machine. Note that the list of printers returned by the method may change over time as new printers are installed or old printers are removed. Use the getName() method of the Printer to get the name of the printer.

The following snippet of code lists all installed printers on the machine running the code. You may get a different output.

The Printer.getDefaultPrinter() method returns the default Printer. The method may return null if no printer is installed. The default printer may be changed on a machine. Therefore, the method may return different printers from call to call, and the printer returned may not be valid after some time.

You can use the createPrinterJob() static method of the PrinterJob class to create a printer job:

public static PrinterJob createPrinterJob()

public static PrinterJob createPrinterJob(Printer printer)

The method with no-args creates a printer job for the default printer. You can use the other version of the method to create a printer job for the specified printer.

You can change the printer for a PrinterJob by calling its setPrinter() method. If the current printer job settings are not supported by the new printer, the settings are reset automatically for the new printer.

Setting the Printer to null for the job will use the default printer. Use one of the following printPage() methods to print a Node:

boolean printPage(Node node)

boolean printPage(PageLayout pageLayout, Node node)

The first version of the method takes only the node to be printed as the parameter. It uses the default page layout for the job for printing.

The second version lets you specify a page layout for printing the Node. The specified PageLayout will override the PageLayout for the job and it will be used only for printing the specified node. For subsequent printing, the default PageLayout for the job will be used. You can create a PageLayout using the Printer class.

The printPage() method returns true if the printing was successful. Otherwise, it returns false. When you are done printing, call the endJob() method. The method returns true if the job can be successfully spooled to the printer queue. Otherwise, it returns false, which may indicate that the job could not be spooled or it was already completed. After successful completion of the job, the job can no longer be reused.

You can cancel a print job using the cancelJob() method of the PrinterJob. The printing may not be cancelled immediately, for example, when a page is in the middle of printing. The cancellation occurs as soon as possible. The method does not have any effect if,

The job has already been requested to be cancelled.

The job is already completed.

The job has an error.

A PrinterJob has a read-only status, which is defined by one of the constants of the PrinterJob.JobStatus enum:

NOT_STARTED

PRINTING

CANCELED

DONE

ERROR

The NOT_STARTED status indicates a new job. In this status, the job can be configured and printing can be initiated. The PRINTING status indicates that the job has requested to print at least one page and it has not terminated printing. In this status, the job cannot be configured.

The other three statuses, CANCELED, DONE, and ERROR, indicate the termination state of the job. Once the job is in one of these statuses, it should not be reused. There is no need to call the endJob() method when the status goes to CANCELED or ERROR. The DONE status is entered when the printing was successful and the endJob() method was called. The PrinterJob class contains a read-only jobStatus property that indicates the current status of the print job.

3.2 The GUI

The GUI of the above program shows how to print nodes. It displays a TextArea where you can enter text.

Two Buttons are provided: one prints the TextArea node and the other the entire Scene. When printing is initiated, the print job status is displayed in a Label.

The Print API allows users to interact with the printing process. Users can change the printer settings interactively before the printing is initiated. The API lets you show Page Setup and Print Setup dialogs for setting the page properties and printer settings for the job.

You can let the user configure the page layout by showing a Page Setup dialog. Use theshowPageSetupDialog(Window owner) method of the PrinterJob to show a Page Setup dialog. The user can set the page size, source, orientation, and margin. The dialog may allow the user to access other printing properties such as the list of printers.

Once the user confirms the settings on the dialog, the PrinterJob has the new settings. The method returns true if the user confirms the settings on the dialog. It returns false if the user cancels the dialog. It also returns false if the dialog cannot be displayed, such as when the job is not in the NOT_STARTED state.

The owner parameter to the method is the window that will be the owner of the dialog box. It can be null. If specified, the inputs to the window will be blocked while the dialog is displayed.

You can use the showPrintDialog(Window owner) method to show a Print dialog where the user can modify the printer and settings for the PrinterJob. The return value and parameter of this method have meanings similar to that of the showPageSetupDialog() method.

The Print API contains two classes that are related to printer and printer job settings:

PrinterAttributes

JobSettings

A Printer has attributes, which indicate the printing capabilities of the printer. Examples of printer attributes are default paper size, supported paper sizes, maximum number of copies, and default collation.

A PrinterAttributes object encapsulates the attributes of a Printer. The Print API does not let you change the printer attributes as you cannot change the capabilities of a printer. You can only use its capabilities.

You cannot create a PrinterAttributes object directly. You need to get it from a Printer object using the getPrinterAttributes() method.

The following snippet of code prints some attributes of the default printer in the machine: You may get a different output.

A JobSettings contains the printer attributes to be used for a print job for a specific printer. You can obtain the JobSettings of a print job using the getJobSettings() method of the PrinterJob object. A JobSettings is a mutable object. It contains a property for each printer attribute that can be set for a print job. By default, its properties are initialized to the default properties of the printer. You can change the property that will be used for the current print job. If you change the property of a JobSettings that is not supported by the printer, the property reverts to the default value for the printer.

The following snippet of code sets the printSides property to DUPLEX. In this case, the printer supports only ONE_SIDED printing.

Therefore, the printSides property is set to ONE_SIDED, which is the default, and only supported printSides value by the printer. You may get a different output.

An instance of the PageLayout class represents the page setup for a print job. By default, it is set to the printer default value. You have already seen setting up the page layout using the Page Setup dialog. A PageLayout encapsulates three things:

The paper size

The page orientation

The page margins

A PageLayout is used to configure the printable area of the page, which must lie within the printable area of the hardware. If a page is rendered outside the printable area of the hardware, the content is clipped. You cannot create a PageLayout object directly. You need to use one of the createPageLayout() methods of the Printer to get a PageLayout.

Sometimes, you want to know the size of the printable area on the page. You can get it using the getPrintableWidth() and getPrintableHeight() methods of the PageLayout. This is useful if you want to resize a node before printing, so it fits the printable area.

The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

Subscribe

newestoldestmost voted

Notify of

Guest

Adewunmi Micheal

The Best Full Tutorial on JavaFx Printing API

Vote Up3Vote Down Reply

1 year ago

Guest

Scott

What about printing multiple pages which most print applications may require. Currently, when a node is passed to the printPage(node), it will only print the first page. It will not print remaining pages.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.