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 booleanisCanvasOfPageprotected PdfPagepageIs initialized and used only when Canvas element autotagging is enabled, seeenableAutoTagging(PdfPage).protected PdfCanvaspdfCanvasprotected RectanglerootArea-
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 orPdfFormXObjectstream.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 voidclose()Closes theCanvas.voidenableAutoTagging(PdfPage page)Enables canvas content autotagging.protected RootRendererensureRootRendererNotNull()voidflush()Forces all registered renderers (including child element renderers) to flush their contents to the content stream.PdfPagegetPage()The page on which this canvas will be rendered.PdfCanvasgetPdfCanvas()Gets thePdfCanvas.PdfDocumentgetPdfDocument()Gets thePdfDocumentfor this canvas.RectanglegetRootArea()Gets the root area rectangle.private static PdfCanvasinitPdfCanvasOrThrowIfPageIsFlushed(PdfPage page)booleanisAutoTaggingEnabled()booleanisCanvasOfPage()Defines if the canvas is exactly the direct content of the page.voidrelayout()Performs an entire recalculation of the element flow on the canvas, taking into account all its current child elements.voidsetRenderer(CanvasRenderer canvasRenderer)Sets theIRendererfor this Canvas.-
Methods inherited from class com.itextpdf.layout.RootElement
add, add, createAndAddRendererSubTree, deleteOwnProperty, ensureRootTagIsCreated, 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, getTransparentStrokeColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundImage, setBackgroundImage, setBaseDirection, setBorder, setBorderBottom, setBorderBottomLeftRadius, setBorderBottomRightRadius, setBorderLeft, setBorderRadius, setBorderRight, setBorderTop, setBorderTopLeftRadius, setBorderTopRightRadius, setCharacterSpacing, setDashPattern, setDestination, setFixedPosition, setFixedPosition, setFixedPosition, setFixedPosition, setFont, setFontColor, setFontColor, setFontColor, setFontFamily, setFontFamily, setFontKerning, setFontScript, setFontSize, setHorizontalAlignment, setHyphenation, setLineThrough, setOpacity, setRelativePosition, setSplitCharacters, setStrokeColor, setStrokeColor, setStrokeColor, setStrokeWidth, setTextAlignment, setTextRenderingMode, setUnderline, setUnderline, setUnderline, setUnderline, setUnderline, setWordSpacing, simulateBold, simulateItalic
-
-
-
-
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 addLinkelements 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 orPdfFormXObjectstream.- 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 thePdfDocumentfor 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 theIRendererfor 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
PdfPageinstance, 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.immediateFlushtotrue.
-
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 withCanvasobject, as due to some properties set there might be some 'hanging' elements, which are waiting other elements to be added and processed.close()tells theCanvasthat no more elements will be added and it is time to finish processing all the elements.
-
ensureRootRendererNotNull
protected RootRenderer ensureRootRendererNotNull()
- Specified by:
ensureRootRendererNotNullin classRootElement<Canvas>
-
-