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, rootRendererFields inherited from class com.itextpdf.layout.ElementPropertyContainer
properties -
Constructor Summary
ConstructorsConstructorDescriptionDocument(PdfDocument pdfDoc) Creates a document from aPdfDocument.Document(PdfDocument pdfDoc, PageSize pageSize) Creates a document from aPdfDocumentwith a manually setPageSize.Document(PdfDocument pdfDoc, PageSize pageSize, boolean immediateFlush) Creates a document from aPdfDocumentwith a manually setPageSize. -
Method Summary
Modifier and TypeMethodDescriptionTerminates the current element, usually a page.add(IBlockElement element) Adds an element to the root.add(SectionBreak sectionBreak) Terminates the current page if it's not the first one in the document.protected voidChecks whether a method is invoked at the closed documentvoidclose()Closes the document and associated PdfDocument.protected RootRenderervoidflush()Forces all registered renderers (including child element renderers) to flush their contents to the content stream.floatGets the bottom margin, measured in pointsT1 getDefaultProperty(int property) Gets the default property from this entity.GetsFootnotesPropertiesspecified for the document to customize footnotes.floatGets 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.getPageMargins(int pageNumber) Gets page margins by specified page number.Gets PDF document.floatGets the right margin, measured in pointsfloatGets the top margin, measured in pointsbooleanisPageMarginsSpecified(int pageNumber) Checks whether page margins have been specified for the given page number.voidrelayout()Performs an entire recalculation of the document flow, taking into account all its current child elements.voidsetBottomMargin(float bottomMargin) Sets the bottom margin, measured in pointsvoidsetFootnotesProperties(FootnotesProperties footnotesProperties) SetsFootnotesPropertiesfor the document.voidsetLeftMargin(float leftMargin) Sets the left margin, measured in pointsvoidsetMargins(float topMargin, float rightMargin, float bottomMargin, float leftMargin) Convenience method to set all margins with one method.setPageMargins(int pageNumber, PageMarginBoxes margins) Sets page margins for page with provided number.setPageMargins(Function<Integer, PageMarginBoxes> function) Sets page margins for page based on provided function for page number.setPageMargins(Predicate<Integer> condition, PageMarginBoxes margins) Sets page margins for page based on provided condition for page number.voidsetRenderer(DocumentRenderer documentRenderer) Changes theDocumentRendererat runtime.voidsetRightMargin(float rightMargin) Sets the right margin, measured in pointsvoidsetTopMargin(float topMargin) Sets the top margin, measured in pointsMethods inherited from class com.itextpdf.layout.RootElement
add, createAndAddRendererSubTree, deleteOwnProperty, ensureRootTagIsCreated, getFontProvider, getOwnProperty, getProperty, getRenderer, hasOwnProperty, hasProperty, setFontProvider, setProperty, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAligned, showTextAlignedKernedMethods 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
-
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 aPdfDocumentwith a manually setPageSize.- Parameters:
-
pdfDoc- the in-memory representation of the PDF document -
pageSize- the page size
-
Document
Creates a document from aPdfDocumentwith 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 thePdfDocumentas 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
Terminates the current page if it's not the first one in the document. Sets the page size and/or page margins specified in the arguments for the next page.- Parameters:
-
sectionBreak-SectionBreak, optionally with a specified page size and/or page margins - Returns:
-
this same
Documentinstance
-
add
Description copied from class:RootElementAdds an element to the root. The element is immediately placed in the contents.- Overrides:
-
addin 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 theDocumentRendererat runtime. Use this to customize the Document'sIRendererbehavior.- 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.immediateFlushtotrue. -
getLeftMargin
public float getLeftMargin()Gets the left margin, measured in points- Returns:
-
a
floatcontaining the left margin value
-
setLeftMargin
public void setLeftMargin(float leftMargin) Sets the left margin, measured in points- Parameters:
-
leftMargin- afloatcontaining the new left margin value
-
getRightMargin
public float getRightMargin()Gets the right margin, measured in points- Returns:
-
a
floatcontaining the right margin value
-
setRightMargin
public void setRightMargin(float rightMargin) Sets the right margin, measured in points- Parameters:
-
rightMargin- afloatcontaining the new right margin value
-
getTopMargin
public float getTopMargin()Gets the top margin, measured in points- Returns:
-
a
floatcontaining the top margin value
-
setTopMargin
public void setTopMargin(float topMargin) Sets the top margin, measured in points- Parameters:
-
topMargin- afloatcontaining the new top margin value
-
getBottomMargin
public float getBottomMargin()Gets the bottom margin, measured in points- Returns:
-
a
floatcontaining the bottom margin value
-
setBottomMargin
public void setBottomMargin(float bottomMargin) Sets the bottom margin, measured in points- Parameters:
-
bottomMargin- afloatcontaining 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
-
getPageMargins
Gets page margins by specified page number.- Parameters:
-
pageNumber- number of the page to get margins for - Returns:
-
PageMarginBoxespage margins
-
setPageMargins
Sets page margins for page with provided number.- Parameters:
-
pageNumber- number of the page to set margins for -
margins-PageMarginBoxespage margins to set - Returns:
-
this same
Documentinstance
-
setPageMargins
Sets page margins for page based on provided condition for page number.- Parameters:
-
condition- matching rule with page number as argument -
margins-PageMarginBoxespage margins to set - Returns:
-
this same
Documentinstance
-
setPageMargins
Sets page margins for page based on provided function for page number.- Parameters:
-
function- function with page number as argument, returnnullin case result should be ignored andPageMarginBoxespage margins return value - Returns:
-
this same
Documentinstance
-
isPageMarginsSpecified
public boolean isPageMarginsSpecified(int pageNumber) Checks whether page margins have been specified for the given page number.This method returns
trueif the margins for the page are determined by any of the following mechanisms (in order of precedence):- Explicitly set margins for page number via
setPageMargins(int, PageMarginBoxes) - Matching rule set via
setPageMargins(Predicate, PageMarginBoxes) - Function set via
setPageMargins(Function)
NOTE: the method returns
trueeven if the value produced by the mechanisms isnull. Only when none of the above apply and the default static margins are used, this method returnsfalse.- Parameters:
-
pageNumber- the page number to check - Returns:
-
trueif margins for the page are defined explicitly by page number, by matching rule or by function,falseotherwise
- Explicitly set margins for page number via
-
getFootnotesProperties
GetsFootnotesPropertiesspecified for the document to customize footnotes.- Returns:
-
FootnotesPropertiesspecified for the document
-
setFootnotesProperties
SetsFootnotesPropertiesfor the document.- Parameters:
-
footnotesProperties-FootnotesPropertiesto customize footnotes
-
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
Rectanglewith the required dimensions and origin point
-
getDefaultProperty
publicT1 getDefaultProperty (int property) Description copied from interface:IPropertyContainerGets the default property from this entity.- Specified by:
-
getDefaultPropertyin interfaceIPropertyContainer - Overrides:
-
getDefaultPropertyin 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,
nullwill be returned
-
ensureRootRendererNotNull
- Specified by:
-
ensureRootRendererNotNullin classRootElement<Document>
-
checkClosingStatus
protected void checkClosingStatus()Checks whether a method is invoked at the closed document
-