Package com.itextpdf.layout
Class Document
- All Implemented Interfaces:
-
IPropertyContainer
,Closeable
,AutoCloseable
Document is the default root element when creating a self-sufficient PDF. It mainly operates high-level operations e.g. setting page size and rotation, adding elements, and writing text at specific coordinates. It has no knowledge of the actual PDF concepts and syntax.
A Document
's rendering behavior can be modified by extending DocumentRenderer
and setting an instance of this newly created with setRenderer(com.itextpdf.layout.renderer.DocumentRenderer)
.
-
Field Summary
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
ConstructorDescriptionDocument
(PdfDocument pdfDoc) Creates a document from aPdfDocument
.Document
(PdfDocument pdfDoc, PageSize pageSize) Creates a document from aPdfDocument
with a manually setPageSize
.Document
(PdfDocument pdfDoc, PageSize pageSize, boolean immediateFlush) Creates a document from aPdfDocument
with a manually setPageSize
. -
Method Summary
Modifier and TypeMethodDescriptionTerminates the current element, usually a page.add
(IBlockElement element) Adds an element to the root.protected void
Checks whether a method is invoked at the closed documentvoid
close()
Closes the document and associated PdfDocument.protected RootRenderer
void
flush()
Forces all registered renderers (including child element renderers) to flush their contents to the content stream.float
Gets the bottom margin, measured in pointsT1 getDefaultProperty
(int property) Gets the default property from this entity.float
Gets the left margin, measured in pointsgetPageEffectiveArea
(PageSize pageSize) Returns the area that will actually be used to write on the page, given the current margins.Gets PDF document.float
Gets the right margin, measured in pointsfloat
Gets the top margin, measured in pointsvoid
relayout()
Performs an entire recalculation of the document flow, taking into account all its current child elements.void
setBottomMargin
(float bottomMargin) Sets the bottom margin, measured in pointsvoid
setLeftMargin
(float leftMargin) Sets the left margin, measured in pointsvoid
setMargins
(float topMargin, float rightMargin, float bottomMargin, float leftMargin) Convenience method to set all margins with one method.void
setRenderer
(DocumentRenderer documentRenderer) Changes theDocumentRenderer
at runtime.void
setRightMargin
(float rightMargin) Sets the right margin, measured in pointsvoid
setTopMargin
(float topMargin) Sets the top margin, measured in pointsMethods inherited from class com.itextpdf.layout.RootElement
add, createAndAddRendererSubTree, deleteOwnProperty, 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
-
Constructor Details
-
Document
Creates a document from aPdfDocument
. Initializes the first page with thePdfDocument
's current defaultPageSize
.- Parameters:
-
pdfDoc
- the in-memory representation of the PDF document
-
Document
Creates a document from aPdfDocument
with a manually setPageSize
.- Parameters:
-
pdfDoc
- the in-memory representation of the PDF document -
pageSize
- the page size
-
Document
Creates a document from aPdfDocument
with a manually setPageSize
.- Parameters:
-
pdfDoc
- the in-memory representation of the PDF document -
pageSize
- the page size -
immediateFlush
- if true, write pages and page-related instructions to thePdfDocument
as soon as possible.
-
-
Method Details
-
close
public void close()Closes the document and associated PdfDocument. -
add
Terminates the current element, usually a page. Sets the next element to be the size specified in the argument.- Parameters:
-
areaBreak
- anAreaBreak
, optionally with a specified size - Returns:
- this element
-
add
Description copied from class:RootElement
Adds an element to the root. The element is immediately placed in the contents.- Overrides:
-
add
in classRootElement<Document>
- Parameters:
-
element
- an element with spacial margins, tabbing, and alignment - Returns:
- this element
- See Also:
-
getPdfDocument
Gets PDF document.- Returns:
- the in-memory representation of the PDF document
-
setRenderer
Changes theDocumentRenderer
at runtime. Use this to customize the Document'sIRenderer
behavior.- Parameters:
-
documentRenderer
- the DocumentRenderer to set
-
flush
public void flush()Forces all registered renderers (including child element renderers) to flush their contents to the content stream. -
relayout
public void relayout()Performs an entire recalculation of the document flow, taking into account all its current child elements. May become very resource-intensive for large documents.Do not use when you have set
RootElement.immediateFlush
totrue
. -
getLeftMargin
public float getLeftMargin()Gets the left margin, measured in points- Returns:
-
a
float
containing the left margin value
-
setLeftMargin
public void setLeftMargin(float leftMargin) Sets the left margin, measured in points- Parameters:
-
leftMargin
- afloat
containing the new left margin value
-
getRightMargin
public float getRightMargin()Gets the right margin, measured in points- Returns:
-
a
float
containing the right margin value
-
setRightMargin
public void setRightMargin(float rightMargin) Sets the right margin, measured in points- Parameters:
-
rightMargin
- afloat
containing the new right margin value
-
getTopMargin
public float getTopMargin()Gets the top margin, measured in points- Returns:
-
a
float
containing the top margin value
-
setTopMargin
public void setTopMargin(float topMargin) Sets the top margin, measured in points- Parameters:
-
topMargin
- afloat
containing the new top margin value
-
getBottomMargin
public float getBottomMargin()Gets the bottom margin, measured in points- Returns:
-
a
float
containing the bottom margin value
-
setBottomMargin
public void setBottomMargin(float bottomMargin) Sets the bottom margin, measured in points- Parameters:
-
bottomMargin
- afloat
containing the new bottom margin value
-
setMargins
public void setMargins(float topMargin, float rightMargin, float bottomMargin, float leftMargin) Convenience method to set all margins with one method.- Parameters:
-
topMargin
- the upper margin -
rightMargin
- the right margin -
leftMargin
- the left margin -
bottomMargin
- the lower margin
-
getPageEffectiveArea
Returns the area that will actually be used to write on the page, given the current margins. Does not have any side effects on the document.- Parameters:
-
pageSize
- the size of the page to - Returns:
-
a
Rectangle
with the required dimensions and origin point
-
getDefaultProperty
publicT1 getDefaultProperty (int property) Description copied from interface:IPropertyContainer
Gets the default property from this entity.- Specified by:
-
getDefaultProperty
in interfaceIPropertyContainer
- Overrides:
-
getDefaultProperty
in classRootElement<Document>
- Type Parameters:
-
T1
- the return type associated with the property - Parameters:
-
property
- the property to be retrieved - Returns:
-
the default property value. If the default property is not defined,
null
will be returned
-
ensureRootRendererNotNull
- Specified by:
-
ensureRootRendererNotNull
in classRootElement<Document>
-
checkClosingStatus
protected void checkClosingStatus()Checks whether a method is invoked at the closed document
-