Package com.itextpdf.text.pdf
Class PdfDocument
java.lang.Object
com.itextpdf.text.Document
com.itextpdf.text.pdf.PdfDocument
- All Implemented Interfaces:
DocListener
,ElementListener
,IAccessibleElement
,EventListener
PdfDocument
is the class that is used by PdfWriter
to translate a Document
into a PDF with different pages.
A PdfDocument
always listens to a Document
and adds the Pdf representation of every Element
that is
added to the Document
.
- Since:
- 2.0.8 (class was package-private before)
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
static class
(package private) static class
PdfCatalog
is the PDF Catalog-object.static class
PdfInfo
is the PDF InfoDictionary. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PdfDictionary
protected int
This represents the current alignment of the PDF Elements.protected PdfAction
The current activePdfAction
when processing anAnchor
.(package private) PdfAnnotationsImp
private PdfBody
protected HashMap<String,
PdfRectangle> This is the size of the several boxes that will be used in the next page.protected PdfCollection
protected float
This is the current height of the document.protected PdfOutline
This is the currentPdfOutline
in the hierarchy of outlines.private TempFileCache
protected boolean
Signals that OnOpenDocument should be called.protected PdfContentByte
This is the PdfContentByte object, containing the borders and other Graphics.(package private) static final String
The characters to be applied the hanging punctuation.protected float
This is the position where the image ends.protected Image
This is the image that could not be shown on a previous page.protected PdfDocument.Indentation
protected PdfDocument.PdfInfo
some meta information about the Document.protected boolean
Signals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).private boolean
(package private) int
Stores a list of document level JavaScript actions.protected PdfString
protected int
Holds the type of the last element, that has been added to the document.protected float
This represents the leading of the lines.Signals that the current leading has to be subtracted from a YMark object when positive and save current leadingprotected PdfLine
The line that is currently being written.The lines that are written until now.protected TreeMap<String,
PdfDocument.Destination> Stores the destinations keyed by name.protected float
margin in y direction starting from the bottom.protected float
margin in x direction starting from the left.protected float
margin in x direction starting from the right.protected float
margin in y direction starting from the top.protected Rectangle
This is the size of the next page.protected PdfAction
protected String
protected boolean
protected PdfDictionary
private boolean
This checks if the page is empty.protected PdfPageLabels
protected PageResources
This are the page resources of the current Page.protected PdfOutline
This is the root outline of the document.protected static final DecimalFormat
protected boolean
Holds value of property strictImageSequence.protected TabSettings
The current tab settings.protected PdfContentByte
This is the PdfContentByte object, containing the text.protected int
protected HashMap<String,
PdfRectangle> This is the size of the several boxes of the current Page.protected PdfViewerPreferencesImp
Contains the Viewer preferences of this PDF document.protected PdfWriter
ThePdfWriter
.Fields inherited from class com.itextpdf.text.Document
accessibleAttributes, chapternumber, close, compress, htmlStyleClass, id, javaScript_onLoad, javaScript_onUnLoad, listeners, marginBottom, marginLeft, marginMirroring, marginMirroringTopBottom, marginRight, marginTop, open, pageN, pageSize, plainRandomAccess, role, wmfFontCorrection
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Signals that anElement
was added to theDocument
.protected void
Adds an image to the document.(package private) void
addAdditionalAction
(PdfName actionType, PdfAction action) (package private) void
addAnnotation
(PdfAnnotation annot) (package private) void
addCalculationOrder
(PdfFormField formField) private void
(package private) void
addFileAttachment
(String description, PdfFileSpecification fs) (package private) void
(package private) void
addJavaScript
(String name, PdfAction js) (package private) void
addOutline
(PdfOutline outline, String name) Adds a named outline to the document .(package private) void
Adds aPdfPTable
to the document.protected void
addSpacing
(float extraspace, float oldleading, Font f) Calls addSpacing(float, float, Font, boolean (false)).protected void
addSpacing
(float extraspace, float oldleading, Font f, boolean spacingAfter) Adds extra spacing.(package private) void
addViewerPreference
(PdfName key, PdfObject value) void
Adds aPdfWriter
to thePdfDocument
.protected float
line.height() is usually the same as the leading We should take leading into account if it is not the same as the line.height(package private) void
Updates the count in the outlines.protected void
If the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added.void
Method added by Pelikan Stephanvoid
close()
Closes the document.protected ArrayList<IAccessibleElement>
endPage()
protected void
Ensures that a new line has been started.(package private) boolean
Checks if aPdfPTable
fits the current page of thePdfDocument
.private void
protected float
Writes all the lines to the text-object.protected void
(package private) PdfAcroForm
Gets the AcroForm object.(package private) Rectangle
getBoxSize
(String boxName) Gives the size of a trim, art, crop or bleed box, or null if not defined.(package private) PdfDocument.PdfCatalog
getCatalog
(PdfIndirectReference pages) Gets thePdfCatalog
-object.(package private) PdfDocument.PdfInfo
getInfo()
Gets thePdfInfo
-object.private PdfLine
float
Getter for the current leading.(package private) PdfAction
getLocalGotoAction
(String name) int
getNextMarkPoint
(Object obj) (package private) PageResources
Gets the root outline.protected PdfStructureElement
protected PdfStructureElement
getStructElement
(AccessibleElementId id, boolean toSaveFetchedElement) int
int[]
Getter for the current tab stops.float
getVerticalPosition
(boolean ensureNewLine) Gets the current vertical page position.(package private) float
Gets the indentation on the bottom side.protected float
Gets the indentation on the left side.protected float
Gets the indentation on the right side.protected float
Gets the indentation on the top side.protected void
initPage()
Initializes a page.(package private) boolean
(package private) boolean
Getter for property strictImageSequence.private static boolean
(package private) boolean
localDestination
(String name, PdfDestination destination) The local destination to where a local goto with the same name will jump to.(package private) void
Implements a link to other part of the document.protected void
newLine()
Adds the current line to the list of lines and also adds an empty line.boolean
newPage()
Makes a new page and sends it to thePdfWriter
.void
open()
Opens the document.(package private) void
outlineTree
(PdfOutline outline) Recursive method used to write outlines.protected void
Restore @leading from leadingStackprotected void
Save current @leading(package private) void
remoteGoto
(String filename, int page, float llx, float lly, float urx, float ury) Implements a link to another document.(package private) void
remoteGoto
(String filename, String name, float llx, float lly, float urx, float ury) Implements a link to another document.void
Sets the page number to 0.protected void
saveStructElement
(AccessibleElementId id, PdfStructureElement element) (package private) void
Implements an action in an area.(package private) void
setBoxSize
(String boxName, Rectangle size) void
setCollection
(PdfCollection collection) Sets the collection dictionary.(package private) void
setCropBoxSize
(Rectangle crop) (package private) void
setDuration
(int seconds) Sets the display duration for the page (for presentations)(package private) void
setLanguage
(String language) (package private) void
setLeading
(float leading) Setter for the current leading.boolean
setMarginMirroring
(boolean MarginMirroring) Set the margin mirroring.boolean
setMarginMirroringTopBottom
(boolean MarginMirroringTopBottom) Set the margin mirroring.boolean
setMargins
(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.protected void
(package private) void
setOpenAction
(PdfAction action) (package private) void
setOpenAction
(String name) (package private) void
setPageAction
(PdfName actionType, PdfAction action) void
setPageCount
(int pageN) Sets the page number.(package private) void
setPageEmpty
(boolean pageEmpty) (package private) void
setPageLabels
(PdfPageLabels pageLabels) Sets the page labelsboolean
setPageSize
(Rectangle pageSize) Sets the pagesize.(package private) void
setSigFlags
(int f) (package private) void
setStrictImageSequence
(boolean strictImageSequence) Setter for property strictImageSequence.void
setTabSettings
(TabSettings tabSettings) Setter for the current tab stops.(package private) void
setThumbnail
(Image image) (package private) void
setTransition
(PdfTransition transition) Sets the transition for the page(package private) void
setViewerPreferences
(int preferences) void
setXmpMetadata
(byte[] xmpMetadata) Use this method to set the XMP Metadata.(package private) void
traverseOutlineCount
(PdfOutline outline) Recursive method to update the count in the outlines.protected void
useExternalCache
(TempFileCache externalCache) (package private) float
writeLineToContent
(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio) Writes a text line to the document.(package private) void
Writes the outline tree to the body of the PDF document.Methods inherited from class com.itextpdf.text.Document
addAuthor, addCreationDate, addCreator, addDocListener, addHeader, addKeywords, addLanguage, addProducer, addSubject, addTitle, bottom, bottom, bottomMargin, getAccessibleAttribute, getAccessibleAttributes, getHtmlStyleClass, getId, getJavaScript_onLoad, getJavaScript_onUnLoad, getPageNumber, getPageSize, getRole, isInline, isMarginMirroring, isOpen, left, left, leftMargin, removeDocListener, right, right, rightMargin, setAccessibleAttribute, setHtmlStyleClass, setId, setJavaScript_onLoad, setJavaScript_onUnLoad, setRole, top, top, topMargin
-
Field Details
-
writer
ThePdfWriter
. -
structElements
-
externalCache
-
externallyStoredStructElements
-
elementsParents
-
isToUseExternalCache
private boolean isToUseExternalCache -
openMCDocument
protected boolean openMCDocument -
structParentIndices
-
markPoints
-
text
This is the PdfContentByte object, containing the text. -
graphics
This is the PdfContentByte object, containing the borders and other Graphics. -
leading
protected float leadingThis represents the leading of the lines. -
alignment
protected int alignmentThis represents the current alignment of the PDF Elements. -
currentHeight
protected float currentHeightThis is the current height of the document. -
isSectionTitle
protected boolean isSectionTitleSignals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).- Since:
- 2.1.2
-
anchorAction
The current activePdfAction
when processing anAnchor
. -
tabSettings
The current tab settings.- Since:
- 5.4.0
-
leadingStack
Signals that the current leading has to be subtracted from a YMark object when positive and save current leading- Since:
- 2.1.2
-
body
-
textEmptySize
protected int textEmptySize -
nextMarginLeft
protected float nextMarginLeftmargin in x direction starting from the left. Will be valid in the next page -
nextMarginRight
protected float nextMarginRightmargin in x direction starting from the right. Will be valid in the next page -
nextMarginTop
protected float nextMarginTopmargin in y direction starting from the top. Will be valid in the next page -
nextMarginBottom
protected float nextMarginBottommargin in y direction starting from the bottom. Will be valid in the next page -
firstPageEvent
protected boolean firstPageEventSignals that OnOpenDocument should be called. -
line
The line that is currently being written. -
lines
The lines that are written until now. -
lastElementType
protected int lastElementTypeHolds the type of the last element, that has been added to the document. -
hangingPunctuation
The characters to be applied the hanging punctuation.- See Also:
-
indentation
-
info
some meta information about the Document. -
rootOutline
This is the root outline of the document. -
currentOutline
This is the currentPdfOutline
in the hierarchy of outlines. -
viewerPreferences
Contains the Viewer preferences of this PDF document. -
pageLabels
-
localDestinations
Stores the destinations keyed by name. Value is aDestination
. -
jsCounter
int jsCounterStores a list of document level JavaScript actions. -
documentLevelJS
-
SIXTEEN_DIGITS
-
documentFileAttachment
-
openActionName
-
openActionAction
-
additionalActions
-
collection
-
annotationsImp
PdfAnnotationsImp annotationsImp -
language
-
nextPageSize
This is the size of the next page. -
thisBoxSize
This is the size of the several boxes of the current Page. -
boxSize
This is the size of the several boxes that will be used in the next page. -
pageEmpty
private boolean pageEmptyThis checks if the page is empty. -
pageAA
-
pageResources
This are the page resources of the current Page. -
strictImageSequence
protected boolean strictImageSequenceHolds value of property strictImageSequence. -
imageEnd
protected float imageEndThis is the position where the image ends. -
imageWait
This is the image that could not be shown on a previous page. -
floatingElements
-
-
Constructor Details
-
PdfDocument
public PdfDocument()Constructs a new PDF document.
-
-
Method Details
-
addWriter
Adds aPdfWriter
to thePdfDocument
.- Parameters:
writer
- thePdfWriter
that writes everything what is added to this document to an outputstream.- Throws:
DocumentException
- on error
-
getLeading
public float getLeading()Getter for the current leading.- Returns:
- the current leading
- Since:
- 2.1.2
-
setLeading
void setLeading(float leading) Setter for the current leading.- Parameters:
leading
- the current leading- Since:
- 2.1.6
-
pushLeading
protected void pushLeading()Save current @leading -
popLeading
protected void popLeading()Restore @leading from leadingStack -
getTabSettings
Getter for the current tab stops.- Since:
- 5.4.0
-
setTabSettings
Setter for the current tab stops.- Parameters:
tabSettings
- the current tab settings- Since:
- 5.4.0
-
add
Signals that anElement
was added to theDocument
.- Specified by:
add
in interfaceElementListener
- Overrides:
add
in classDocument
- Parameters:
element
- the element to add- Returns:
true
if the element was added,false
if not.- Throws:
DocumentException
- when a document isn't open yet, or has been closed
-
open
public void open()Opens the document.You have to open the document before you can begin to add content to the body of the document.
- Specified by:
open
in interfaceDocListener
- Overrides:
open
in classDocument
-
close
public void close()Closes the document. Once all the content has been written in the body, you have to close the body. After that nothing can be written to the body anymore.- Specified by:
close
in interfaceDocListener
- Overrides:
close
in classDocument
-
setXmpMetadata
Use this method to set the XMP Metadata.- Parameters:
xmpMetadata
- The xmpMetadata to set.- Throws:
IOException
-
newPage
public boolean newPage()Makes a new page and sends it to thePdfWriter
.- Specified by:
newPage
in interfaceDocListener
- Overrides:
newPage
in classDocument
- Returns:
- true if new page is added
-
endPage
-
setPageSize
Sets the pagesize.- Specified by:
setPageSize
in interfaceDocListener
- Overrides:
setPageSize
in classDocument
- Parameters:
pageSize
- the new pagesize- Returns:
true
if the page size was set
-
setMargins
public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.- Specified by:
setMargins
in interfaceDocListener
- Overrides:
setMargins
in classDocument
- Parameters:
marginLeft
- the margin on the leftmarginRight
- the margin on the rightmarginTop
- the margin on the topmarginBottom
- the margin on the bottom- Returns:
- a
boolean
-
setMarginMirroring
public boolean setMarginMirroring(boolean MarginMirroring) Description copied from class:Document
Set the margin mirroring. It will mirror right/left margins for odd/even pages.- Specified by:
setMarginMirroring
in interfaceDocListener
- Overrides:
setMarginMirroring
in classDocument
- Parameters:
MarginMirroring
-true
to mirror the margins- Returns:
- always
true
- See Also:
-
setMarginMirroringTopBottom
public boolean setMarginMirroringTopBottom(boolean MarginMirroringTopBottom) Description copied from class:Document
Set the margin mirroring. It will mirror top/bottom margins for odd/even pages.- Specified by:
setMarginMirroringTopBottom
in interfaceDocListener
- Overrides:
setMarginMirroringTopBottom
in classDocument
- Parameters:
MarginMirroringTopBottom
-true
to mirror the margins- Returns:
- always
true
- Since:
- 2.1.6
- See Also:
-
setPageCount
public void setPageCount(int pageN) Sets the page number.- Specified by:
setPageCount
in interfaceDocListener
- Overrides:
setPageCount
in classDocument
- Parameters:
pageN
- the new page number
-
resetPageCount
public void resetPageCount()Sets the page number to 0.- Specified by:
resetPageCount
in interfaceDocListener
- Overrides:
resetPageCount
in classDocument
-
initPage
Initializes a page.If the footer/header is set, it is printed.
- Throws:
DocumentException
- on error
-
newLine
Adds the current line to the list of lines and also adds an empty line.- Throws:
DocumentException
- on error
-
calculateLineHeight
protected float calculateLineHeight()line.height() is usually the same as the leading We should take leading into account if it is not the same as the line.height- Returns:
- float combined height of the line
- Since:
- 5.5.1
-
carriageReturn
protected void carriageReturn()If the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added. -
getVerticalPosition
public float getVerticalPosition(boolean ensureNewLine) Gets the current vertical page position.- Parameters:
ensureNewLine
- Tells whether a new line shall be enforced. This may cause side effects for elements that do not terminate the lines they've started because those lines will get terminated.- Returns:
- The current vertical page position.
-
ensureNewLine
protected void ensureNewLine()Ensures that a new line has been started. -
flushLines
Writes all the lines to the text-object.- Returns:
- the displacement that was caused
- Throws:
DocumentException
- on error
-
writeLineToContent
float writeLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio) throws DocumentException Writes a text line to the document. It takes care of all the attributes.Before entering the line position must have been established and the
text
argument must be in text object scope (beginText()
).- Parameters:
line
- the line to be writtentext
- thePdfContentByte
where the text will be written tographics
- thePdfContentByte
where the graphics will be written tocurrentValues
- the current font and extra spacing valuesratio
-- Throws:
DocumentException
- on error- Since:
- 5.0.3 returns a float instead of void
-
indentLeft
protected float indentLeft()Gets the indentation on the left side.- Returns:
- a margin
-
indentRight
protected float indentRight()Gets the indentation on the right side.- Returns:
- a margin
-
indentTop
protected float indentTop()Gets the indentation on the top side.- Returns:
- a margin
-
indentBottom
float indentBottom()Gets the indentation on the bottom side.- Returns:
- a margin
-
addSpacing
Calls addSpacing(float, float, Font, boolean (false)). -
addSpacing
Adds extra spacing. -
getInfo
PdfDocument.PdfInfo getInfo()Gets thePdfInfo
-object.- Returns:
PdfInfo
-
getCatalog
Gets thePdfCatalog
-object.- Parameters:
pages
- an indirect reference to this document pages- Returns:
PdfCatalog
-
addOutline
Adds a named outline to the document .- Parameters:
outline
- the outline to be addedname
- the name of this local destination
-
getRootOutline
Gets the root outline. All the outlines must be created with a parent. The first level is created with this outline.- Returns:
- the root outline
-
calculateOutlineCount
void calculateOutlineCount()Updates the count in the outlines. -
traverseOutlineCount
Recursive method to update the count in the outlines. -
writeOutlines
Writes the outline tree to the body of the PDF document.- Throws:
IOException
-
outlineTree
Recursive method used to write outlines.- Throws:
IOException
-
setViewerPreferences
void setViewerPreferences(int preferences) -
addViewerPreference
-
setPageLabels
Sets the page labels- Parameters:
pageLabels
- the page labels
-
getPageLabels
-
localGoto
Implements a link to other part of the document. The jump will be made to a local destination with the same name, that must exist.- Parameters:
name
- the name for this linkllx
- the lower left x corner of the activation areally
- the lower left y corner of the activation areaurx
- the upper right x corner of the activation areaury
- the upper right y corner of the activation area
-
remoteGoto
Implements a link to another document.- Parameters:
filename
- the filename for the remote documentname
- the name to jump tollx
- the lower left x corner of the activation areally
- the lower left y corner of the activation areaurx
- the upper right x corner of the activation areaury
- the upper right y corner of the activation area
-
remoteGoto
Implements a link to another document.- Parameters:
filename
- the filename for the remote documentpage
- the page to jump tollx
- the lower left x corner of the activation areally
- the lower left y corner of the activation areaurx
- the upper right x corner of the activation areaury
- the upper right y corner of the activation area
-
setAction
Implements an action in an area.- Parameters:
action
- thePdfAction
llx
- the lower left x corner of the activation areally
- the lower left y corner of the activation areaurx
- the upper right x corner of the activation areaury
- the upper right y corner of the activation area
-
getLocalGotoAction
-
localDestination
The local destination to where a local goto with the same name will jump to.- Parameters:
name
- the name of this local destinationdestination
- thePdfDestination
with the jump coordinates- Returns:
true
if the local destination was added,false
if a local destination with the same name already existed
-
addJavaScript
-
addJavaScript
-
getDocumentLevelJS
-
addFileAttachment
- Throws:
IOException
-
getDocumentFileAttachment
-
setOpenAction
-
setOpenAction
-
addAdditionalAction
-
setCollection
Sets the collection dictionary.- Parameters:
collection
- a dictionary of type PdfCollection
-
getAcroForm
PdfAcroForm getAcroForm()Gets the AcroForm object.- Returns:
- the PdfAcroform object of the PdfDocument
-
setSigFlags
void setSigFlags(int f) -
addCalculationOrder
-
addAnnotation
-
setLanguage
-
setCropBoxSize
-
setBoxSize
-
setNewPageSizeAndMargins
protected void setNewPageSizeAndMargins() -
getBoxSize
Gives the size of a trim, art, crop or bleed box, or null if not defined.- Parameters:
boxName
- crop, trim, art or bleed
-
setPageEmpty
void setPageEmpty(boolean pageEmpty) -
isPageEmpty
boolean isPageEmpty() -
setDuration
void setDuration(int seconds) Sets the display duration for the page (for presentations)- Parameters:
seconds
- the number of seconds to display the page
-
setTransition
Sets the transition for the page- Parameters:
transition
- the PdfTransition object
-
setPageAction
-
setThumbnail
- Throws:
PdfException
DocumentException
-
getPageResources
PageResources getPageResources() -
isStrictImageSequence
boolean isStrictImageSequence()Getter for property strictImageSequence.- Returns:
- Value of property strictImageSequence.
-
setStrictImageSequence
void setStrictImageSequence(boolean strictImageSequence) Setter for property strictImageSequence.- Parameters:
strictImageSequence
- New value of property strictImageSequence.
-
clearTextWrap
public void clearTextWrap()Method added by Pelikan Stephan -
getStructParentIndex
-
getNextMarkPoint
-
getStructParentIndexAndNextMarkPoint
-
add
Adds an image to the document.- Parameters:
image
- theImage
to add- Throws:
PdfException
- on errorDocumentException
- on error
-
addPTable
Adds aPdfPTable
to the document.- Parameters:
ptable
- thePdfPTable
to be added to the document.- Throws:
DocumentException
- on error
-
addDiv
- Throws:
DocumentException
-
flushFloatingElements
- Throws:
DocumentException
-
fitsPage
Checks if aPdfPTable
fits the current page of thePdfDocument
.- Parameters:
table
- the table that has to be checkedmargin
- a certain margin- Returns:
true
if thePdfPTable
fits the page,false
otherwise.
-
isTagged
-
getLastLine
-
useExternalCache
-
saveStructElement
-
getStructElement
-
getStructElement
protected PdfStructureElement getStructElement(AccessibleElementId id, boolean toSaveFetchedElement) -
flushStructureElementsOnNewPage
protected void flushStructureElementsOnNewPage() -
getStructElements
-