Class PdfDocument
java.lang.Object
org.openpdf.text.Document
org.openpdf.text.pdf.PdfDocument
- All Implemented Interfaces:
AutoCloseable, EventListener, DocListener, ElementListener
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 TypeClassDescriptionstatic class(package private) static classPdfCatalogis the PDF Catalog-object.static classPdfInfois the PDF InfoDictionary.protected static classThis is a helper class for adding a Table to a document. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PdfDictionaryprotected intThis represents the current alignment of the PDF Elements.protected PdfActionThe current activePdfActionwhen processing anAnchor.(package private) PdfAnnotationsImpprotected HashMap<String, PdfRectangle> This is the size of the several boxes that will be used in the next page.protected PdfCollectionprotected floatThis is the current height of the document.protected PdfOutlineThis is the currentPdfOutlinein the hierarchy of outlines.protected HashMap<String, PdfIndirectReference> protected HashMap<String, PdfIndirectReference> protected intThe duration of the pageprotected booleanSignals that OnOpenDocument should be called.protected PdfContentByteThis is the PdfContentByte object, containing the borders and other Graphics.(package private) static final StringThe characters to be applied the hanging punctuation.protected floatThis is the position where the image ends.protected ImageThis is the image that could not be shown on a previous page.protected PdfDocument.Indentationprotected PdfDocument.PdfInfosome meta information about the Document.private booleanThis is the flag meaning whether document is creating footer.protected booleanSignals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).(package private) intStores a list of document level JavaScript actions.protected intHolds the type of the last element, that has been added to the document.protected floatThis represents the leading of the lines.protected intSignals that the current leading has to be subtracted from a YMark object when positive.protected PdfLineThe line that is currently being written.The lines that are written until now.Stores the destinations keyed by name.protected intprotected floatmargin in y direction starting from the bottom.protected floatmargin in x direction starting from the left.protected floatmargin in x direction starting from the right.protected floatmargin in y direction starting from the top.protected RectangleThis is the size of the next page.protected PdfActionprotected Stringprotected PdfDictionaryprivate booleanThis checks if the page is empty.protected PdfPageLabelsprotected PageResourcesThis are the page resources of the current Page.protected PdfOutlineThis is the root outline of the document.protected static final DecimalFormatprotected booleanHolds value of property strictImageSequence.protected PdfContentByteThis is the PdfContentByte object, containing the text.protected intprotected HashMap<String, PdfRectangle> This is the size of the several boxes of the current Page.protected PdfIndirectReferenceprotected PdfTransitionThe page transitionprotected PdfViewerPreferencesImpContains the Viewer preferences of this PDF document.protected PdfWriterThePdfWriter.protected byte[]XMP Metadata for the page.Fields inherited from class Document
chapternumber, close, compress, footer, header, htmlStyleClass, javaScript_onLoad, javaScript_onUnLoad, marginBottom, marginLeft, marginMirroring, marginMirroringTopBottom, marginRight, marginTop, open, pageN, pageSize, plainRandomAccess, wmfFontCorrection -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanSignals that anElementwas added to theDocument.protected voidAdds an image to the document.(package private) voidaddAdditionalAction(PdfName actionType, PdfAction action) (package private) voidaddAnnotation(PdfAnnotation annot) (package private) voidaddCalculationOrder(PdfFormField formField) protected voidOccupies space forImagethat will be added later instead of now(package private) voidaddFileAttachment(String description, PdfFileSpecification fs) (package private) voidaddJavaScript(String name, PdfAction js) (package private) void(package private) voidaddOutline(PdfOutline outline, String name) Adds a named outline to the document .private voidaddPdfTable(Table t) Adds a new table to the document.(package private) voidAdds aPdfPTableto the document.protected voidaddSpacing(float extraspace, float oldleading, Font f) Adds extra space.(package private) voidaddViewerPreference(PdfName key, PdfObject value) voidAdds aPdfWriterto thePdfDocument.protected voidanalyzeRow(List<List<PdfCell>> rows, PdfDocument.RenderingContext ctx) (package private) floatReturns the bottomvalue of aTableif it were added to this document.(package private) voidUpdates the count in the outlines.protected voidIf the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added.voidMethod added by Pelikan Stephanvoidclose()Closes the document.protected voidconsumeRowspan(List<PdfCell> row, PdfDocument.RenderingContext ctx) (package private) static PdfPTablecreateInOneCell(Paragraph paragraph) Integrate a paragraph into a table, so it can be a whole.protected voiddelayTableAddition(PdfPTable table) Occupies space forPdfPTablethat will be added later instead of nowprotected voiddoFooter()protected voiddoHeader()protected voidEnsures that a new line has been started.extractRows(List<PdfCell> cells, PdfDocument.RenderingContext ctx) (package private) booleanChecks if aPdfPTablefits the current page of thePdfDocument.protected floatWrites all the lines to the text-object.protected voidwrite non-textElementinto document(package private) PdfAcroFormGets the AcroForm object.(package private) RectanglegetBoxSize(String boxName) Gives the size of a trim, art, crop or bleed box, or null if not defined.(package private) PdfDocument.PdfCataloggetCatalog(PdfIndirectReference pages) Gets thePdfCatalog-object.(package private) HashMap<String, PdfIndirectReference> (package private) HashMap<String, PdfIndirectReference> protected PdfDocument.PdfInfogetInfo()Gets thePdfInfo-object.floatGetter for the current leading.(package private) PdfActiongetLocalGotoAction(String name) (package private) int(package private) PageResourcesGets the root outline.floatgetVerticalPosition(boolean ensureNewLine) Gets the current vertical page position.(package private) void(package private) floatGets the indentation on the bottom side.protected floatGets the indentation on the left side.protected floatGets the indentation on the right side.protected floatGets the indentation on the top side.protected voidinitPage()Initializes a page.(package private) boolean(package private) booleanGetter for property strictImageSequence.(package private) booleanlocalDestination(String name, PdfDestination destination) The local destination to where a local goto with the same name will jump to.(package private) voidImplements a link to other part of the document.protected booleanmayBeRemoved(List<PdfCell> row) protected voidnewLine()Adds the current line to the list of lines and also adds an empty line.booleannewPage()Makes a new page and sends it to thePdfWriter.voidopen()Opens the document.(package private) voidoutlineTree(PdfOutline outline) Recursive method used to write outlines.(package private) voidremoteGoto(String filename, int page, float llx, float lly, float urx, float ury) Implements a link to another document.(package private) voidremoteGoto(String filename, String name, float llx, float lly, float urx, float ury) Implements a link to another document.protected voidrenderCells(PdfDocument.RenderingContext ctx, List cells, boolean hasToFit) voidResets the footer of this document.voidResets the header of this document.voidSets the page number to 0.private voidresetText(boolean move) (package private) voidImplements an action in an area.(package private) voidsetBoxSize(String boxName, Rectangle size) voidsetCollection(PdfCollection collection) Sets the collection dictionary.(package private) voidsetCropBoxSize(Rectangle crop) (package private) voidsetDuration(int seconds) Sets the display duration for the page (for presentations)voidsetFooter(HeaderFooter footer) Changes the footer of this document.voidsetHeader(HeaderFooter header) Changes the header of this document.(package private) voidsetLeading(float leading) Setter for the current leading.booleansetMarginMirroring(boolean MarginMirroring) Set the margin mirroring.booleansetMarginMirroringTopBottom(boolean MarginMirroringTopBottom) Set the margin mirroring.booleansetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.protected void(package private) voidsetOpenAction(String name) (package private) voidsetOpenAction(PdfAction action) (package private) voidsetPageAction(PdfName actionType, PdfAction action) voidsetPageCount(int pageN) Sets the page number.(package private) voidsetPageEmpty(boolean pageEmpty) (package private) voidsetPageLabels(PdfPageLabels pageLabels) Sets the page labelsbooleansetPageSize(Rectangle pageSize) Sets the pagesize.(package private) voidsetSigFlags(int f) (package private) voidsetStrictImageSequence(boolean strictImageSequence) Setter for property strictImageSequence.private voidsetTableWidth(PdfPTable table) (package private) voidsetThumbnail(Image image) (package private) voidsetTransition(PdfTransition transition) Sets the transition for the page(package private) voidsetViewerPreferences(int preferences) voidsetXmpMetadata(byte[] xmpMetadata) Use this method to set the XMP Metadata.(package private) voidtraverseOutlineCount(PdfOutline outline) Recursive method to update the count in the outlines.(package private) voidwriteLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio) Writes a text line to the document.(package private) voidWrites the outline tree to the body of the PDF document.Methods inherited from class Document
addAuthor, addCreationDate, addCreationDate, addCreator, addDocListener, addHeader, addKeywords, addModificationDate, addModificationDate, addProducer, addProducer, addSubject, addTitle, bottom, bottom, bottomMargin, getDocumentLanguage, getHtmlStyleClass, getJavaScript_onLoad, getJavaScript_onUnLoad, getPageNumber, getPageSize, getProduct, getRelease, getTextRenderingOptions, getVersion, isGlyphSubstitutionEnabled, isMarginMirroring, isOpen, left, left, leftMargin, removeDocListener, right, right, rightMargin, setDocumentLanguage, setGlyphSubstitutionEnabled, setHtmlStyleClass, setJavaScript_onLoad, setJavaScript_onUnLoad, setTextRenderingOptions, top, top, topMargin
-
Field Details
-
SIXTEEN_DIGITS
-
hangingPunctuation
The characters to be applied the hanging punctuation.- See Also:
-
writer
ThePdfWriter. -
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
-
leadingCount
protected int leadingCountSignals that the current leading has to be subtracted from a YMark object when positive.- Since:
- 2.1.2
-
anchorAction
The current activePdfActionwhen processing anAnchor. -
textEmptySize
protected int textEmptySize -
xmpMetadata
protected byte[] xmpMetadataXMP Metadata for the page. -
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
-
lastElementType
protected int lastElementTypeHolds the type of the last element, that has been added to the document. -
indentation
-
info
some meta information about the Document. -
rootOutline
This is the root outline of the document. -
currentOutline
This is the currentPdfOutlinein the hierarchy of outlines. -
viewerPreferences
Contains the Viewer preferences of this PDF document. -
pageLabels
-
localDestinations
-
documentLevelJS
-
documentFileAttachment
-
openActionName
-
openActionAction
-
additionalActions
-
collection
-
markPoint
protected int markPoint -
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. -
duration
protected int durationThe duration of the page -
transition
The page transition -
pageAA
-
thumb
-
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. -
jsCounter
int jsCounterStores a list of document level JavaScript actions. -
annotationsImp
PdfAnnotationsImp annotationsImp -
pageEmpty
private boolean pageEmptyThis checks if the page is empty.
-
-
Constructor Details
-
PdfDocument
public PdfDocument()Constructs a new PDF document.
-
-
Method Details
-
createInOneCell
Integrate a paragraph into a table, so it can be a whole.Note: This is not a table with square, it's just like the paragraph, but it cannot be separated.
- Parameters:
paragraph- theParagraphincoming paragraphs to be consolidated- Returns:
PdfPTablethe whole which will be used later
-
addWriter
Adds aPdfWriterto thePdfDocument.- Parameters:
writer- thePdfWriterthat 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
-
add
Signals that anElementwas added to theDocument.- Specified by:
addin interfaceElementListener- Overrides:
addin classDocument- Parameters:
element- the element to add- Returns:
trueif the element was added,falseif 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:
openin interfaceDocListener- Overrides:
openin 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceDocListener- Overrides:
closein classDocument
-
setXmpMetadata
public void setXmpMetadata(byte[] xmpMetadata) Use this method to set the XMP Metadata.- Parameters:
xmpMetadata- The xmpMetadata to set.
-
newPage
public boolean newPage()Makes a new page and sends it to thePdfWriter.- Specified by:
newPagein interfaceDocListener- Overrides:
newPagein classDocument- Returns:
- a
boolean
-
setPageSize
Sets the pagesize.- Specified by:
setPageSizein interfaceDocListener- Overrides:
setPageSizein classDocument- Parameters:
pageSize- the new pagesize- Returns:
trueif the page size was set
-
setMargins
public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.- Specified by:
setMarginsin interfaceDocListener- Overrides:
setMarginsin 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:DocumentSet the margin mirroring. It will mirror right/left margins for odd/even pages.Note: it will not work with
Table.- Specified by:
setMarginMirroringin interfaceDocListener- Overrides:
setMarginMirroringin classDocument- Parameters:
MarginMirroring-trueto mirror the margins- Returns:
- always
true - See Also:
-
setMarginMirroringTopBottom
public boolean setMarginMirroringTopBottom(boolean MarginMirroringTopBottom) Description copied from class:DocumentSet the margin mirroring. It will mirror top/bottom margins for odd/even pages.Note: it will not work with
Table.- Specified by:
setMarginMirroringTopBottomin interfaceDocListener- Overrides:
setMarginMirroringTopBottomin classDocument- Parameters:
MarginMirroringTopBottom-trueto mirror the margins- Returns:
- always
true - Since:
- 2.1.6
- See Also:
-
setPageCount
public void setPageCount(int pageN) Sets the page number.- Specified by:
setPageCountin interfaceDocListener- Overrides:
setPageCountin classDocument- Parameters:
pageN- the new page number
-
resetPageCount
public void resetPageCount()Sets the page number to 0.- Specified by:
resetPageCountin interfaceDocListener- Overrides:
resetPageCountin classDocument
-
resetHeader
public void resetHeader()Resets the header of this document.- Specified by:
resetHeaderin interfaceDocListener- Overrides:
resetHeaderin classDocument
-
resetText
private void resetText(boolean move) -
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
-
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
void 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
textargument must be in text object scope (beginText()).- Parameters:
line- the line to be writtentext- thePdfContentBytewhere the text will be written tographics- thePdfContentBytewhere the graphics will be written tocurrentValues- the current font and extra spacing valuesratio-- Throws:
DocumentException- on error
-
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
Adds extra space. This method should probably be rewritten.- Parameters:
extraspace- extra spaceoldleading- old leadingf- font
-
getInfo
-
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) - See Also:
-
addViewerPreference
-
setPageLabels
Sets the page labels- Parameters:
pageLabels- the page labels
-
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- thePdfActionllx- 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- thePdfDestinationwith the jump coordinates- Returns:
trueif the local destination was added,falseif a local destination with the same name already existed
-
addJavaScript
-
addJavaScript
-
getDocumentLevelJS
HashMap<String, PdfIndirectReference> getDocumentLevelJS() -
addFileAttachment
- Throws:
IOException
-
getDocumentFileAttachment
HashMap<String, PdfIndirectReference> 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
-
getMarkPoint
int getMarkPoint() -
incMarkPoint
void incMarkPoint() -
setCropBoxSize
-
setBoxSize
-
setNewPageSizeAndMargins
protected void setNewPageSizeAndMargins() -
getBoxSize
-
isPageEmpty
boolean isPageEmpty() -
setPageEmpty
void setPageEmpty(boolean pageEmpty) -
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:
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 -
add
Adds an image to the document.- Parameters:
image- theImageto add- Throws:
PdfException- on errorDocumentException- on error
-
flushSpecial
protected void flushSpecial()write non-textElementinto document -
addDelay
Occupies space forImagethat will be added later instead of now- Parameters:
image- the newImage
-
delayTableAddition
Occupies space forPdfPTablethat will be added later instead of now- Parameters:
table- the newPdfPTable
-
addPTable
Adds aPdfPTableto the document.- Parameters:
ptable- thePdfPTableto be added to the document.- Throws:
DocumentException- on error
-
fitsPage
Checks if aPdfPTablefits the current page of thePdfDocument.- Parameters:
table- the table that has to be checkedmargin- a certain margin- Returns:
trueif thePdfPTablefits the page,falseotherwise.
-
setTableWidth
-
addPdfTable
Adds a new table to the document.- Parameters:
t- Table to add. Rendered rows will be deleted after processing.- Throws:
DocumentException- Since:
- iText 2.0.8
-
analyzeRow
-
mayBeRemoved
-
consumeRowspan
-
extractRows
-
renderCells
protected void renderCells(PdfDocument.RenderingContext ctx, List cells, boolean hasToFit) throws DocumentException - Throws:
DocumentException
-
bottom
Returns the bottomvalue of aTableif it were added to this document.- Parameters:
table- the table that may or may not be added to this document- Returns:
- a bottom value
-
doHeader
- Throws:
DocumentException
-