public class CanvasPrinter extends Object
CanvasControl
.
This instance controls the printing output.
Clients can set the setCanvas(CanvasControl)
canvas}, the rectangle
in the
world coordinate system to print, and a scale factor
amongst others.
Clients can also register event handlers to the printing process:
addPrintingListener(IEventHandler)
is the event that is raised just before a page is printed.addPrintedListener(IEventHandler)
is the event that is raised just after a page was printed. content margins
, which enable the
client to place nodes in a space between the printable borders of a page and the content that is being printed.
More information on the printing mechanism can be found in the section Printing a Canvas's Content.
print(javafx.print.PrinterJob, boolean)
Type | Property and Description |
---|---|
ObjectProperty<CanvasControl> |
canvas
Returns the property that holds the canvas to print.
|
BooleanProperty |
centeringContentEnabled
Returns the property that holds the value for whether the contents should be centered on the page.
|
ObjectProperty<InsetsD> |
contentMargins
Returns the property that holds the content margins.
|
BooleanProperty |
pageMarkPrintingEnabled
Returns the property that holds the value for whether the
createPageMarks(PageLayout, int)
method should be called for every page that is printed. |
ObjectProperty<RectD> |
printRectangle
Returns the property that holds the rectangle that will be printed.
|
DoubleProperty |
scale
Returns the property that holds the scale.
|
BooleanProperty |
scalingDownToFitPageEnabled
Returns the property that holds the value for whether to scale down the content so that it always fits on one page.
|
BooleanProperty |
scalingUpToFitPageEnabled
Returns the property that holds the value for whether to scale up the content so that it always fills one page as large as possible.
|
Modifier and Type | Class and Description |
---|---|
static class |
CanvasPrinter.PrintInfo
Encapsulates information for printing with a specific
PageLayout ,
i.e. the scale dependent on the screen DPI as well as the number of rows and columns for poster printing. |
Constructor and Description |
---|
CanvasPrinter()
Initializes a new instance with no initial
CanvasControl . |
CanvasPrinter(CanvasControl control)
Initializes a new instance using the provided
CanvasControl
for printing. |
Modifier and Type | Method and Description |
---|---|
void |
addPrintedListener(IEventHandler<PrintingEvent> printedEventHandler)
Adds a listener for the printed event.
|
void |
addPrintingListener(IEventHandler<PrintingEvent> printingEventHandler)
Adds a listener for the printing event.
|
ObjectProperty<CanvasControl> |
canvasProperty()
Returns the property that holds the canvas to print.
|
BooleanProperty |
centeringContentEnabledProperty()
Returns the property that holds the value for whether the contents should be centered on the page.
|
ObjectProperty<InsetsD> |
contentMarginsProperty()
Returns the property that holds the content margins.
|
protected ContextConfigurator |
createContextConfigurator(RectD printRectangle)
Creates a new
ContextConfigurator instance that is used
to ContextConfigurator.createRenderContext(CanvasControl) create the render context}
that is needed to paint the visuals for the printing. |
protected Node |
createPageMarks(javafx.print.PageLayout pageLayout,
int pageIndex)
Creates page marks.
|
CanvasPrinter.PrintInfo |
createPrintInfo(javafx.print.PageLayout pageLayout)
Determines the scale and the number of rows and columns needed to print the determined contents of the component
dependent on the
isScalingDownToFitPageEnabled() and isScalingUpToFitPageEnabled() properties amongst others. |
protected Node |
createPrintingNode(IRenderContext renderContext)
Constructs the node instance that is used for printing.
|
CanvasControl |
getCanvas()
Gets the canvas to print.
|
InsetsD |
getContentMargins()
Returns the margins that are defined on this printable.
|
RectD |
getPrintRectangle()
Returns the rectangle that will be printed.
|
double |
getScale()
Returns a scale factor to apply for printing.
|
boolean |
isCenteringContentEnabled()
Returns whether the contents should be centered on the page.
|
boolean |
isPageMarkPrintingEnabled()
Returns whether the
createPageMarks(PageLayout, int)
method should be called for every page that is printed. |
boolean |
isScalingDownToFitPageEnabled()
Return whether to scale down the content so that it always fits on one page.
|
boolean |
isScalingUpToFitPageEnabled()
Returns whether to scale up the content so that it always fills one page as large as possible.
|
protected void |
onPrintedPage(PrintingEvent args)
Called whenever a page was printed.
|
protected void |
onPrintingPage(PrintingEvent args)
Called whenever a page is about to be printed.
|
BooleanProperty |
pageMarkPrintingEnabledProperty()
Returns the property that holds the value for whether the
createPageMarks(PageLayout, int)
method should be called for every page that is printed. |
boolean |
print(javafx.print.PrinterJob job,
boolean showPrintDialog)
Prints the pages using the specified
PrinterJob which will
provide the PageLayout for one page to print. |
ObjectProperty<RectD> |
printRectangleProperty()
Returns the property that holds the rectangle that will be printed.
|
void |
removePrintedListener(IEventHandler<PrintingEvent> printedEventHandler)
Removes the given listener for the printed event.
|
void |
removePrintingListener(IEventHandler<PrintingEvent> printingEventHandler)
Removes the given listener for the printing event.
|
DoubleProperty |
scaleProperty()
Returns the property that holds the scale.
|
BooleanProperty |
scalingDownToFitPageEnabledProperty()
Returns the property that holds the value for whether to scale down the content so that it always fits on one page.
|
BooleanProperty |
scalingUpToFitPageEnabledProperty()
Returns the property that holds the value for whether to scale up the content so that it always fills one page as large as possible.
|
void |
setCanvas(CanvasControl canvas)
Sets the canvas to print.
|
void |
setCenteringContentEnabled(boolean centerContent)
Sets whether the contents should be centered on the page.
|
void |
setContentMargins(InsetsD contentMargins)
Sets the margins that are defined on this printable.
|
void |
setPageMarkPrintingEnabled(boolean enabled)
Sets whether the
createPageMarks(PageLayout, int)
method should be called for every page that is printed. |
void |
setPrintRectangle(RectD printRectangle)
Sets the rectangle that will be printed.
|
void |
setScale(double scale)
Sets a scale factor to apply for printing.
|
void |
setScalingDownToFitPageEnabled(boolean scaleDownToFitPage)
Sets whether to scale down the content so that it always fits on one page.
|
void |
setScalingUpToFitPageEnabled(boolean scaleUpToFitPage)
Sets whether to scale up the content so that it always fills one page as large as possible.
|
public ObjectProperty<CanvasControl> canvasProperty
public BooleanProperty centeringContentEnabledProperty
true
.public ObjectProperty<InsetsD> contentMarginsProperty
Keep in mind that those are different to the margins that are
defined by the printableWidth and -Height of the PageLayout
during the printing process. The area in which the actual content is printed
is defined by adding the content margins and the PageLayout
margins.
For example, if the page margins are defined as [20, 20, 20, 20] and the content margins are defined as [20, 50, 10, 30], the real insets will be [40, 70, 30, 50].
The reason to have two distinct sets of insets is to be able to print decorations between the content (of the CanvasControl) and the page border (defined by the PageLayout).
public BooleanProperty pageMarkPrintingEnabledProperty
createPageMarks(PageLayout, int)
method should be called for every page that is printed.
Default value is false
.public ObjectProperty<RectD> printRectangleProperty
canvas component
to print in the world coordinate system.
The default value is the content rectangle
of the canvas to print.public DoubleProperty scaleProperty
0.72
makes 96
units in the world coordinate system appear exactly one inch long.
The default value is 1.0
.public BooleanProperty scalingDownToFitPageEnabledProperty
true
, the values returned by createPrintInfo(javafx.print.PageLayout)
are ignored. Default value is true
.public BooleanProperty scalingUpToFitPageEnabledProperty
false
.public CanvasPrinter()
CanvasControl
.
The corresponding property needs to be set
before printing can be performed.
The print rectangle
is set to a fallback value of [0, 0, 300, 300] in this
case and should be set, too.public CanvasPrinter(CanvasControl control)
CanvasControl
for printing.
This method sets the print rectangle
to the content rectangle of the component
.control
- The CanvasControl to print.public final void addPrintedListener(IEventHandler<PrintingEvent> printedEventHandler)
Clients can register to this event, for example, clean up customizations
performed in the addPrintingListener(IEventHandler)
printing event}.
printedEventHandler
- The listener to add.removePrintedListener(IEventHandler)
public final void addPrintingListener(IEventHandler<PrintingEvent> printingEventHandler)
Clients can register to this event, for example, to add decorations to the printed page by customizing the node to be printed.
printingEventHandler
- The listener to add.removePrintingListener(IEventHandler)
public ObjectProperty<CanvasControl> canvasProperty()
public BooleanProperty centeringContentEnabledProperty()
true
.public ObjectProperty<InsetsD> contentMarginsProperty()
Keep in mind that those are different to the margins that are
defined by the printableWidth and -Height of the PageLayout
during the printing process. The area in which the actual content is printed
is defined by adding the content margins and the PageLayout
margins.
For example, if the page margins are defined as [20, 20, 20, 20] and the content margins are defined as [20, 50, 10, 30], the real insets will be [40, 70, 30, 50].
The reason to have two distinct sets of insets is to be able to print decorations between the content (of the CanvasControl) and the page border (defined by the PageLayout).
protected ContextConfigurator createContextConfigurator(RectD printRectangle)
ContextConfigurator
instance that is used
to ContextConfigurator.createRenderContext(CanvasControl)
create the render context}
that is needed to paint the visuals for the printing.printRectangle
- The world bounds to print.ContextConfigurator
instance.protected Node createPageMarks(javafx.print.PageLayout pageLayout, int pageIndex)
The default implementation places page marks inside the content margin. I.e. page marks are outside the actual content area but inside the printable area of the given page layout.
This method is only called if the
page mark printing
is enabled.
pageLayout
- the page layout for the current print job.pageIndex
- the index of the page for which page marks should be
printed.getContentMargins()
public CanvasPrinter.PrintInfo createPrintInfo(javafx.print.PageLayout pageLayout)
isScalingDownToFitPageEnabled()
and isScalingUpToFitPageEnabled()
properties amongst others.pageLayout
- the PageLayout
that defines the printable area of a page.protected Node createPrintingNode(IRenderContext renderContext)
CanvasControl.exportContent(IRenderContext)
renderContext
- the context for the rendering of the node to be printed. The instance is created by the ContextConfigurator
returned by createContextConfigurator(com.yworks.yfiles.geometry.RectD)
.public CanvasControl getCanvas()
public InsetsD getContentMargins()
Keep in mind that those are different to the margins that are
defined by the printableWidth and -Height of the PageLayout
during the printing process. The area in which the actual content is printed
is defined by adding the content margins and the PageLayout
margins.
For example, if the page margins are defined as [20, 20, 20, 20] and the content margins are defined as [20, 50, 10, 30], the real insets will be [40, 70, 30, 50].
The reason to have two distinct sets of insets is to be able to print decorations between the content (of the CanvasControl) and the page border (defined by the PageLayout).
public RectD getPrintRectangle()
canvas component
to print in the world coordinate system.
The default value is the content rectangle
of the canvas to print.canvas component
to print.public double getScale()
0.72
makes 96
units in the world coordinate system appear exactly one inch long.
The default value is 1.0
.public boolean isCenteringContentEnabled()
true
.public boolean isPageMarkPrintingEnabled()
createPageMarks(PageLayout, int)
method should be called for every page that is printed.
The default value is false
.public boolean isScalingDownToFitPageEnabled()
true
, the values returned by createPrintInfo(javafx.print.PageLayout)
are ignored. Default value is true
.public boolean isScalingUpToFitPageEnabled()
false
.protected void onPrintedPage(PrintingEvent args)
args
- contains the information of what is about to be printed
(column and row of the page in the raster, render context and the printed node itself).protected void onPrintingPage(PrintingEvent args)
args
- contains the information of what is about to be printed
(column and row of the page in the raster, render context and the node to be printed itself).public BooleanProperty pageMarkPrintingEnabledProperty()
createPageMarks(PageLayout, int)
method should be called for every page that is printed.
Default value is false
.public boolean print(javafx.print.PrinterJob job, boolean showPrintDialog)
PrinterJob
which will
provide the PageLayout
for one page to print.
This method will print the contents of the CanvasControl
of the set print rectangle
in world coordinates
either on one page (if it fits on one page or if the isScalingDownToFitPageEnabled()
property is set to true) or on as many pages as it needs to print the content.
Note that calling this method will show the print dialog
of the PrinterJob with the window of the CanvasControl
as owner.
IllegalArgumentException
- if the specified PrinterJob is null.job
- the PrinterJob with the settings for printing. Cannot be null.showPrintDialog
- If the PrinterJob.showPrintDialog(javafx.stage.Window)
method should be called before printing. Clients that set this parameter to
false should call this or a similar method by themselves before calling this
method. If this option is set to true and the user cancels the print dialog,
this method exits immediately and returns false.public ObjectProperty<RectD> printRectangleProperty()
canvas component
to print in the world coordinate system.
The default value is the content rectangle
of the canvas to print.public final void removePrintedListener(IEventHandler<PrintingEvent> printedEventHandler)
printedEventHandler
- The listener to remove.addPrintedListener(IEventHandler)
public final void removePrintingListener(IEventHandler<PrintingEvent> printingEventHandler)
printingEventHandler
- The listener to remove.addPrintingListener(IEventHandler)
public DoubleProperty scaleProperty()
0.72
makes 96
units in the world coordinate system appear exactly one inch long.
The default value is 1.0
.public BooleanProperty scalingDownToFitPageEnabledProperty()
true
, the values returned by createPrintInfo(javafx.print.PageLayout)
are ignored. Default value is true
.public BooleanProperty scalingUpToFitPageEnabledProperty()
false
.public void setCanvas(CanvasControl canvas)
public void setCenteringContentEnabled(boolean centerContent)
true
.public void setContentMargins(InsetsD contentMargins)
Keep in mind that those are different to the margins that are
defined by the printableWidth and -Height of the PageLayout
during the printing process. The area in which the actual content is printed
is defined by adding the content margins and the PageLayout
margins.
For example, if the page margins are defined as [20, 20, 20, 20] and the content margins are defined as [20, 50, 10, 30], the real insets will be [40, 70, 30, 50].
The reason to have two distinct sets of insets is to be able to print decorations between the content (of the CanvasControl) and the page border (defined by the PageLayout).
public void setPageMarkPrintingEnabled(boolean enabled)
createPageMarks(PageLayout, int)
method should be called for every page that is printed.
The default value is false
.public void setPrintRectangle(RectD printRectangle)
canvas component
to print in the world coordinate system.
The default value is the content rectangle
of the canvas to print.printRectangle
- a rectangle in the world coordinate system that defines the area of the contents of the
canvas component
to print.public void setScale(double scale)
0.72
makes 96
units in the world coordinate system appear exactly one inch long.
The default value is 1.0
.public void setScalingDownToFitPageEnabled(boolean scaleDownToFitPage)
true
, the values returned by createPrintInfo(javafx.print.PageLayout)
are ignored. Default value is true
.public void setScalingUpToFitPageEnabled(boolean scaleUpToFitPage)
false
.