Package javafx.print

Class PrinterJob

java.lang.Object
javafx.print.PrinterJob

public final class PrinterJob extends Object
PrinterJob is the starting place for JavaFX scenegraph printing.

It includes

  • Printer discovery
  • Job creation
  • Job configuration based on supported printer capabilities
  • Page setup
  • Rendering of a node hierachy to a page.

Here ia a very simple example, which prints a single node.

 Node node = new Circle(100, 200, 200);
 PrinterJob job = PrinterJob.createPrinterJob();
 if (job != null) {
    boolean success = job.printPage(node);
    if (success) {
        job.endJob();
    }
 }
 
Points to note

In the example above the node was not added to a scene. Since most printing scenarios are printing content that's either not displayed at all, or must be prepared and formatted differently, this is perfectly acceptable.

If content that is currently part of a Scene and is being displayed, is printed, then because printing a job or even a single page of the job may span over multiple screen "pulses" or frames, it is important for the application to ensure that the node being printed is not updated during the printing process, else partial or smeared rendering is probable.

It should be apparent that the same applies even to nodes that are not displayed - updating them concurrent with printing them is not a good idea.

There is no requirement to do printing on the FX application thread. A node may be prepared for printing on any thread, the job may be invoked on any thread. However, minimising the amount of work done on the FX application thread is generally desirable, so as not to affect the responsiveness of the application UI. So the recommendation is to perform printing on a new thread and let the implementation internally schedule any tasks that need to be performed on the FX thread to be run on that thread.

Since:
JavaFX 8.0