Package com.itextpdf.layout
Class Canvas
- All Implemented Interfaces:
-
IPropertyContainer
,Closeable
,AutoCloseable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PdfPage
Is initialized and used only when Canvas element autotagging is enabled, seeenableAutoTagging(PdfPage)
.protected PdfCanvas
protected Rectangle
Fields inherited from class com.itextpdf.layout.RootElement
childElements, defaultFont, defaultFontProvider, defaultSplitCharacters, immediateFlush, pdfDocument, rootRenderer
Fields inherited from class com.itextpdf.layout.ElementPropertyContainer
properties
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Canvas to manipulate a specific content stream, which might be for example a page orPdfFormXObject
stream.Creates a new Canvas to manipulate a specific document and page.Creates a new Canvas to manipulate a specific page content stream.Canvas
(PdfFormXObject formXObject, PdfDocument pdfDocument) Creates a new Canvas to manipulate a specificPdfFormXObject
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes theCanvas
.void
enableAutoTagging
(PdfPage page) Enables canvas content autotagging.protected RootRenderer
void
flush()
Forces all registered renderers (including child element renderers) to flush their contents to the content stream.getPage()
The page on which this canvas will be rendered.Gets thePdfCanvas
.Gets thePdfDocument
for this canvas.Gets the root area rectangle.boolean
boolean
Defines if the canvas is exactly the direct content of the page.void
relayout()
Performs an entire recalculation of the element flow on the canvas, taking into account all its current child elements.void
setRenderer
(CanvasRenderer canvasRenderer) Sets theIRenderer
for this Canvas.Methods inherited from class com.itextpdf.layout.RootElement
add, add, createAndAddRendererSubTree, deleteOwnProperty, getDefaultProperty, getFontProvider, getOwnProperty, getProperty, getRenderer, hasOwnProperty, hasProperty, setFontProvider, setProperty, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAlignedKerned
Methods inherited from class com.itextpdf.layout.ElementPropertyContainer
getSplitCharacters, getStrokeColor, getStrokeWidth, getTextRenderingMode, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundImage, setBackgroundImage, setBaseDirection, setBold, setBorder, setBorderBottom, setBorderBottomLeftRadius, setBorderBottomRightRadius, setBorderLeft, setBorderRadius, setBorderRight, setBorderTop, setBorderTopLeftRadius, setBorderTopRightRadius, setCharacterSpacing, setDestination, setFixedPosition, setFixedPosition, setFixedPosition, setFixedPosition, setFont, setFontColor, setFontColor, setFontFamily, setFontFamily, setFontKerning, setFontScript, setFontSize, setHorizontalAlignment, setHyphenation, setItalic, setLineThrough, setOpacity, setRelativePosition, setSplitCharacters, setStrokeColor, setStrokeWidth, setTextAlignment, setTextRenderingMode, setUnderline, setUnderline, setUnderline, setUnderline, setWordSpacing
-
Field Details
-
pdfCanvas
-
rootArea
-
page
Is initialized and used only when Canvas element autotagging is enabled, seeenableAutoTagging(PdfPage)
. It is also used to determine if autotagging is enabled.
-
-
Constructor Details
-
Canvas
Creates a new Canvas to manipulate a specific page content stream. The given page shall not be flushed: drawing on flushed pages is impossible because their content is already written to the output stream. Use this constructor to be able to addLink
elements on it (using any other constructor would result in inability to add PDF annotations, based on which, for example, links work).If the
PdfDocument.isTagged()
is true, using this constructor would automatically enable the tagging for the content. Regarding tagging the effect is the same as usingenableAutoTagging(PdfPage)
.- Parameters:
-
page
- the page on which this canvas will be rendered, shall not be flushed (seePdfObjectWrapper.isFlushed()
). -
rootArea
- the maximum area that the Canvas may write upon
-
Canvas
Creates a new Canvas to manipulate a specific content stream, which might be for example a page orPdfFormXObject
stream.- Parameters:
-
pdfCanvas
- the low-level content stream writer -
rootArea
- the maximum area that the Canvas may write upon
-
Canvas
Creates a new Canvas to manipulate a specific document and page.- Parameters:
-
pdfCanvas
- The low-level content stream writer -
rootArea
- The maximum area that the Canvas may write upon -
immediateFlush
- Whether to flush the canvas immediately after operations, false otherwise
-
Canvas
Creates a new Canvas to manipulate a specificPdfFormXObject
.- Parameters:
-
formXObject
- the form -
pdfDocument
- the document that the resulting content stream will be written to
-
-
Method Details
-
getPdfDocument
Gets thePdfDocument
for this canvas.- Returns:
- the document that the resulting content stream will be written to
-
getRootArea
Gets the root area rectangle.- Returns:
- the maximum area that the Canvas may write upon
-
getPdfCanvas
Gets thePdfCanvas
.- Returns:
- the low-level content stream writer
-
setRenderer
Sets theIRenderer
for this Canvas.- Parameters:
-
canvasRenderer
- a renderer specific for canvas operations
-
getPage
The page on which this canvas will be rendered.- Returns:
-
the specified
PdfPage
instance, might be null if this the page was not set.
-
enableAutoTagging
Enables canvas content autotagging. By default it is disabled.- Parameters:
-
page
- the page, on which this canvas will be rendered.
-
isAutoTaggingEnabled
public boolean isAutoTaggingEnabled()- Returns:
- true if autotagging of canvas content is enabled. Default value - false.
-
isCanvasOfPage
public boolean isCanvasOfPage()Defines if the canvas is exactly the direct content of the page. This is known definitely only if this instance was created byCanvas(PdfPage, Rectangle)
constructor overload, otherwise this method returns false.- Returns:
-
true if the canvas on which this instance performs drawing is directly the canvas of the page; false if the instance of this class was created not with
Canvas(PdfPage, Rectangle)
constructor overload.
-
relayout
public void relayout()Performs an entire recalculation of the element flow on the canvas, taking into account all its current child elements. May become very resource-intensive for large documents. Do not use when you have setRootElement.immediateFlush
totrue
. -
flush
public void flush()Forces all registered renderers (including child element renderers) to flush their contents to the content stream. -
close
public void close()Closes theCanvas
. Although not completely necessary in all cases, it is still recommended to call this method when you are done working withCanvas
object, as due to some properties set there might be some 'hanging' elements, which are waiting other elements to be added and processed.close()
tells theCanvas
that no more elements will be added and it is time to finish processing all the elements. -
ensureRootRendererNotNull
- Specified by:
-
ensureRootRendererNotNull
in classRootElement<Canvas>
-