This guide shows how to work with the Printing API.

Overview

The web page can be printed using one of the following ways:

  1. Using the window.print() JavaScript function. This function can be invoked from the JavaScript code on a web page.
  2. Using the Frame.print() method of the JxBrowser API. The method requests printing of the frame. If you need to print the whole page, you should call the print() method in the main frame. For example:

     browser.getMainFrame().ifPresent(Frame::print);
    

The web page is not printed immediately. The PrintCallback will be invoked to tell the engine how to handle the print request. By default all print requests are canceled.

To allow the print request and display the Print Preview dialog use the following code:

browser.set(PrintCallback.class, (params, callback) -> callback.showPrintPreview());

In the Print Preview dialog you can select the preferred printing options:

Print Preview

Canceling Printing

If you need to cancel or disable printing at all use the following approach:

browser.set(PrintCallback.class, (params, callback) -> callback.cancel());
Go Top