-
Notifications
You must be signed in to change notification settings - Fork 14
NatTable supports printing of the table. Internally this is solved by drawing on a printer GC.
To enable printing you simply need to ensure that the PrintCommandHandler is registered in your layer stack. If this is true, you just need to execute the PrintCommand which will open a PrintDialog and start printing after you confirm.
The page break on printing is on cell borders and not inside a cell. This way the print result is better readable. For spanned cells the page break is not calculated on the cell border.
ILayerCommand |
ILayerCommandHandler |
Description |
|---|---|---|
PrintCommand |
PrintCommandHandler |
Executing this command will start the process for printing the NatTable. |
Note:
The PrintCommandHandler is registered to the GridLayer by default. The DefaultGridLayerConfiguration also aggregates the DefaultPrintBindings which adds the CTRL + P key binding to the PrintAction. If no GridLayer is involved in your layer stack, you need to ensure that the PrintCommandHandler and the key binding are registered yourself.
The way NatTable is printed can be configured via ConfigRegistry. The corresponding configuration attributes are defined in the class PrintConfigAttributes.
ConfigAttribute |
Description |
|---|---|
PrintConfigAttributes.DATE_FORMAT |
Configuration attribute to configure the date format that is used for rendering the print date in the footer region. If not specified the default value EEE, d MMM yyyy HH:mm a will be used. |
PrintConfigAttributes.DEFAULT_PAGE_ORIENTATION |
Configuration attribute to configure the default orientation of the paper that should be applied to the PrintDialog. Can be either PrinterData#PORTRAIT or PrinterData#LANDSCAPE. |
PrintConfigAttributes.FITTING_MODE |
Configuration attribute to configure the scaling mode on printing. - Direction#NONE - no content related scaling, simple DPI scaling (default)- Direction#HORIZONTAL - the content is scaled so that all columns are printed on one page- Direction#VERTICAL - the content is scaled so that all rows are printed on one page- Direction#BOTH - the content is scaled so that all columns and rows are printed on one page |
PrintConfigAttributes.FOOTER_HEIGHT |
Configuration attribute to configure the height of the footer. Needs to be specified in printer DPI value. If not set the default value 300 will be used. |
PrintConfigAttributes.FOOTER_PAGE_PATTERN |
Configuration attribute to configure the pattern for rendering the page information in the footer. The pattern can include placeholders for the current page and the total page count, where {0} = current page {1} = total page count e.g. Page {0}/{1} to show Page 1/6
|
PrintConfigAttributes.FOOTER_STYLE |
Configuration attribute to configure the IStyle that should be used to print the footer. Currently only background color, foreground color and font style attributes are supported. |
PrintConfigAttributes.STRETCH |
Configuration attribute to configure a scaling option in case FITTING_MODE is set. By default only downscaling is supported on enabling the fit-to-page scaling. By setting STRETCH to true also upscaling will be performed to make the most out of the available space.Note: stretching is only supported for fitting mode Direction.HORIZONTAL. |
CellConfigAttributes.GRID_LINE_WIDTH |
Configure the grid line with of the table. If not configured explicitly the grid line width will be increased on printing to 2px and decreased again afterwards. The increase of the grid line width is necessary to correct printing issues where grid lines where sometimes not rendered because of rounding issues. |
The class responsible for printing is LayerPrinter. By default a simple configured instance is used by the default PrintCommandHandler. It is possible to modify the printing behavior by creating a custom PrintCommandHandler with a customized LayerPrinter instance. This way it is possible to configure
- pre-rendering
- page count calculation
- multi NatTable printing
- column header repetition
- print listeners
By default the in-memory pre-rendering is enabled to ensure that content painters that dynamically calculate the row height or content width based on the content of the cell trigger the resize before printing. This behavior can be disabled via LayerPrinter#disablePreRendering() and enabled via LayerPrinter#enablePreRendering().
As the total page count calculation can be time consuming for more complicated table setups (e.g. dynamic calculated row heights for huge data sets), the total page count calculation can be enabled or disabled via LayerPrinter#enablePageCountCalculation() and LayerPrinter#disablePageCountCalculation(). By default the page count calculation is enabled.
It is possible to add multiple NatTable instances to one print job. This can be done via LayerPrinter#addPrintTarget(ILayer, IConfigRegistry). Via LayerPrinter#joinPrintTargets(boolean) it is possible to configure whether the tables should be printed consecutively or if each table should be started on a new page (default).
The following snippet demonstrates how to print two tables consecutively:
LayerPrinter printer =
new LayerPrinter(headerTable, headerTable.getConfigRegistry());
printer.addPrintTarget(bodyTable, bodyTable.getConfigRegistry());
printer.joinPrintTargets(true);
printer.print(headerTable.getShell());Additionally it is possible to configure that the first table registered with the LayerPrinter is repeated. This makes it for example possible to create layouts where a fixed header table with meta information can be used as print header. This behavior can be specified via LayerPrinter constructor parameter repeat.
Note:
When printing multiple NatTable instances with different dimensions that should fit horizontally, it might make sense to configure a combination of PrintConfigAttributes#FITTING_MODE and PrintConfigAttributes#STRETCH. By default both instances will share the same scaling, which results in one table not taking the whole available space. By enabling stretching the smaller table will be stretched to take the whole available space.
The LayerPrinter can be configured to repeat for example the column header on every print page. For a single table with a GridLayer this can be configured via constructor parameter.
LayerPrinter printer =
new LayerPrinter(
natTable,
((GridLayer) natTable.getLayer()).getColumnHeaderLayer(),
natTable.getConfigRegistry());
printer.print(headerTable.getShell());To repeat the header of a table in a multi-table print, e.g. have a header table and a body table and the header of the body table should be repeated, there is the method LayerPrinter#addPrintTarget(ILayer, ILayer, IConfigRegistry) that allows to specify the repeat header layer for a print target.
LayerPrinter printer =
new LayerPrinter(headerTable, headerTable.getConfigRegistry());
printer.addPrintTarget(
bodyTable,
((GridLayer) bodyTable.getLayer()).getColumnHeaderLayer(),
bodyTable.getConfigRegistry());
printer.print(headerTable.getShell());Using the PrintListener interface it is possible to react on print events. Implementations can be registered on LayerPrinter to get informed when a print operation starts or is finished.
viewportLayer.registerCommandHandler(
new PrintCommandHandler(viewportLayer) {
@Override
public boolean doCommand(PrintCommand command) {
LayerPrinter printer = new LayerPrinter(
this.layer,
command.getConfigRegistry());
printer.addPrintListener(new PrintListener() {
@Override
public void printStarted() {
System.out.println("Print startet!");
}
@Override
public void printFinished() {
System.out.println("Print finished!");
}
});
printer.print(command.getShell());
return true;
}
});-
Tutorial Examples - Additional Functions - PrintExample
This example shows how to trigger printing a NatTable. -
Tutorial Examples - Additional Functions - MultiPrintExample
This example shows how to trigger printing of multiple NatTable instances in one print job.