Package com.itextpdf.layout
Class Canvas
- java.lang.Object
-
- All Implemented Interfaces:
IPropertyContainer
,java.io.Closeable
,java.lang.AutoCloseable
public class Canvas extends RootElement<Canvas>
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
isCanvasOfPage
protected PdfPage
page
Is initialized and used only when Canvas element autotagging is enabled, seeenableAutoTagging(PdfPage)
.protected PdfCanvas
pdfCanvas
protected Rectangle
rootArea
-
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
Constructors Constructor Description Canvas(PdfCanvas pdfCanvas, Rectangle rootArea)
Creates a new Canvas to manipulate a specific content stream, which might be for example a page orPdfFormXObject
stream.Canvas(PdfCanvas pdfCanvas, Rectangle rootArea, boolean immediateFlush)
Creates a new Canvas to manipulate a specific document and page.Canvas(PdfPage page, Rectangle rootArea)
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes theCanvas
.void
enableAutoTagging(PdfPage page)
Enables canvas content autotagging.protected RootRenderer
ensureRootRendererNotNull()
void
flush()
Forces all registered renderers (including child element renderers) to flush their contents to the content stream.PdfPage
getPage()
The page on which this canvas will be rendered.PdfCanvas
getPdfCanvas()
Gets thePdfCanvas
.PdfDocument
getPdfDocument()
Gets thePdfDocument
for this canvas.Rectangle
getRootArea()
Gets the root area rectangle.private static PdfCanvas
initPdfCanvasOrThrowIfPageIsFlushed(PdfPage page)
boolean
isAutoTaggingEnabled()
boolean
isCanvasOfPage()
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, setFontColor, setFontFamily, setFontFamily, setFontKerning, setFontScript, setFontSize, setHorizontalAlignment, setHyphenation, setItalic, setLineThrough, setOpacity, setRelativePosition, setSplitCharacters, setStrokeColor, setStrokeWidth, setTextAlignment, setTextRenderingMode, setUnderline, setUnderline, setUnderline, setUnderline, setWordSpacing
-
-
-
-
Field Detail
-
pdfCanvas
protected PdfCanvas pdfCanvas
-
rootArea
protected Rectangle rootArea
-
page
protected PdfPage page
Is initialized and used only when Canvas element autotagging is enabled, seeenableAutoTagging(PdfPage)
. It is also used to determine if autotagging is enabled.
-
isCanvasOfPage
private boolean isCanvasOfPage
-
-
Constructor Detail
-
Canvas
public Canvas(PdfPage page, Rectangle rootArea)
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
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea)
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 writerrootArea
- the maximum area that the Canvas may write upon
-
Canvas
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea, boolean immediateFlush)
Creates a new Canvas to manipulate a specific document and page.- Parameters:
pdfCanvas
- The low-level content stream writerrootArea
- The maximum area that the Canvas may write uponimmediateFlush
- Whether to flush the canvas immediately after operations, false otherwise
-
Canvas
public Canvas(PdfFormXObject formXObject, PdfDocument pdfDocument)
Creates a new Canvas to manipulate a specificPdfFormXObject
.- Parameters:
formXObject
- the formpdfDocument
- the document that the resulting content stream will be written to
-
-
Method Detail
-
getPdfDocument
public PdfDocument getPdfDocument()
Gets thePdfDocument
for this canvas.- Returns:
- the document that the resulting content stream will be written to
-
getRootArea
public Rectangle getRootArea()
Gets the root area rectangle.- Returns:
- the maximum area that the Canvas may write upon
-
getPdfCanvas
public PdfCanvas getPdfCanvas()
Gets thePdfCanvas
.- Returns:
- the low-level content stream writer
-
setRenderer
public void setRenderer(CanvasRenderer canvasRenderer)
Sets theIRenderer
for this Canvas.- Parameters:
canvasRenderer
- a renderer specific for canvas operations
-
getPage
public PdfPage 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
public void enableAutoTagging(PdfPage page)
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
protected RootRenderer ensureRootRendererNotNull()
- Specified by:
ensureRootRendererNotNull
in classRootElement<Canvas>
-
-