com.lowagie.text.pdf

Class PdfDocument

public class PdfDocument extends Document

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: Document DocListener PdfWriter

Nested Class Summary
static classPdfDocument.Indentation
static classPdfDocument.PdfCatalog
PdfCatalog is the PDF Catalog-object.
static classPdfDocument.PdfInfo
PdfInfo is the PDF InfoDictionary.
protected static classPdfDocument.RenderingContext
This is a helper class for adding a Table to a document.
Field Summary
protected PdfDictionaryadditionalActions
protected intalignment
This represents the current alignment of the PDF Elements.
protected PdfActionanchorAction
The current active PdfAction when processing an Anchor.
PdfAnnotationsImpannotationsImp
protected HashMapboxSize
This is the size of the several boxes that will be used in the next page.
protected PdfCollectioncollection
protected floatcurrentHeight
This is the current height of the document.
protected PdfOutlinecurrentOutline
This is the current PdfOutline in the hierarchy of outlines.
protected HashMapdocumentFileAttachment
protected HashMapdocumentLevelJS
protected intduration
The duration of the page
protected booleanfirstPageEvent
Signals that OnOpenDocument should be called.
protected PdfContentBytegraphics
This is the PdfContentByte object, containing the borders and other Graphics.
static StringhangingPunctuation
The characters to be applied the hanging punctuation.
protected floatimageEnd
This is the position where the image ends.
protected ImageimageWait
This is the image that could not be shown on a previous page.
protected PdfDocument.Indentationindentation
protected PdfDocument.PdfInfoinfo
some meta information about the Document.
protected booleanisSectionTitle
Signals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).
intjsCounter
Stores a list of document level JavaScript actions.
protected intlastElementType
Holds the type of the last element, that has been added to the document.
protected floatleading
This represents the leading of the lines.
protected intleadingCount
Signals that the current leading has to be subtracted from a YMark object when positive.
protected PdfLineline
The line that is currently being written.
protected ArrayListlines
The lines that are written until now.
protected TreeMaplocalDestinations
Stores the destinations keyed by name.
protected intmarkPoint
protected floatnextMarginBottom
margin in y direction starting from the bottom.
protected floatnextMarginLeft
margin in x direction starting from the left.
protected floatnextMarginRight
margin in x direction starting from the right.
protected floatnextMarginTop
margin in y direction starting from the top.
protected RectanglenextPageSize
This is the size of the next page.
protected PdfActionopenActionAction
protected StringopenActionName
protected PdfDictionarypageAA
protected booleanpageEmpty
This checks if the page is empty.
protected PdfPageLabelspageLabels
protected PageResourcespageResources
This are the page resources of the current Page.
protected PdfOutlinerootOutline
This is the root outline of the document.
protected booleanstrictImageSequence
Holds value of property strictImageSequence.
protected static DecimalFormatSIXTEEN_DIGITS
protected PdfContentBytetext
This is the PdfContentByte object, containing the text.
protected inttextEmptySize
protected HashMapthisBoxSize
This is the size of the several boxes of the current Page.
protected PdfIndirectReferencethumb
protected PdfTransitiontransition
The page transition
protected PdfViewerPreferencesImpviewerPreferences
Contains the Viewer preferences of this PDF document.
protected PdfWriterwriter
The PdfWriter.
protected byte[]xmpMetadata
XMP Metadata for the page.
Constructor Summary
PdfDocument()
Constructs a new PDF document.
Method Summary
booleanadd(Element element)
Signals that an Element was added to the Document.
protected voidadd(Image image)
Adds an image to the document.
voidaddAdditionalAction(PdfName actionType, PdfAction action)
voidaddAnnotation(PdfAnnotation annot)
voidaddCalculationOrder(PdfFormField formField)
voidaddFileAttachment(String description, PdfFileSpecification fs)
voidaddJavaScript(PdfAction js)
voidaddJavaScript(String name, PdfAction js)
voidaddOutline(PdfOutline outline, String name)
Adds a named outline to the document .
voidaddPdfTable(Table t)
Adds a new table to the document.
voidaddPTable(PdfPTable ptable)
Adds a PdfPTable to the document.
protected voidaddSpacing(float extraspace, float oldleading, Font f)
Adds extra space.
voidaddViewerPreference(PdfName key, PdfObject value)
voidaddWriter(PdfWriter writer)
Adds a PdfWriter to the PdfDocument.
protected voidanalyzeRow(ArrayList rows, PdfDocument.RenderingContext ctx)
floatbottom(Table table)
Returns the bottomvalue of a Table if it were added to this document.
voidcalculateOutlineCount()
Updates the count in the outlines.
protected voidcarriageReturn()
If the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added.
voidclearTextWrap()
Method added by Pelikan Stephan
voidclose()
Closes the document.
protected voidconsumeRowspan(ArrayList row, PdfDocument.RenderingContext ctx)
protected voiddoFooter()
protected voiddoHeader()
protected voidensureNewLine()
Ensures that a new line has been started.
protected ArrayListextractRows(ArrayList cells, PdfDocument.RenderingContext ctx)
booleanfitsPage(PdfPTable table, float margin)
Checks if a PdfPTable fits the current page of the PdfDocument.
protected floatflushLines()
Writes all the lines to the text-object.
PdfAcroFormgetAcroForm()
Gets the AcroForm object.
RectanglegetBoxSize(String boxName)
Gives the size of a trim, art, crop or bleed box, or null if not defined.
PdfDocument.PdfCataloggetCatalog(PdfIndirectReference pages)
Gets the PdfCatalog-object.
HashMapgetDocumentFileAttachment()
HashMapgetDocumentLevelJS()
PdfDocument.PdfInfogetInfo()
Gets the PdfInfo-object.
floatgetLeading()
Getter for the current leading.
PdfActiongetLocalGotoAction(String name)
intgetMarkPoint()
PageResourcesgetPageResources()
PdfOutlinegetRootOutline()
Gets the root outline.
floatgetVerticalPosition(boolean ensureNewLine)
Gets the current vertical page position.
voidincMarkPoint()
floatindentBottom()
Gets the indentation on the bottom side.
protected floatindentLeft()
Gets the indentation on the left side.
protected floatindentRight()
Gets the indentation on the right side.
protected floatindentTop()
Gets the indentation on the top side.
protected voidinitPage()
Initializes a page.
booleanisStrictImageSequence()
Getter for property strictImageSequence.
booleanlocalDestination(String name, PdfDestination destination)
The local destination to where a local goto with the same name will jump to.
voidlocalGoto(String name, float llx, float lly, float urx, float ury)
Implements a link to other part of the document.
protected booleanmayBeRemoved(ArrayList 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 the PdfWriter.
voidopen()
Opens the document.
voidoutlineTree(PdfOutline outline)
Recursive method used to write outlines.
voidremoteGoto(String filename, String name, float llx, float lly, float urx, float ury)
Implements a link to another document.
voidremoteGoto(String filename, int page, float llx, float lly, float urx, float ury)
Implements a link to another document.
protected voidrenderCells(PdfDocument.RenderingContext ctx, List cells, boolean hasToFit)
voidresetFooter()
Resets the footer of this document.
voidresetHeader()
Resets the header of this document.
voidresetPageCount()
Sets the page number to 0.
voidsetAction(PdfAction action, float llx, float lly, float urx, float ury)
Implements an action in an area.
voidsetBoxSize(String boxName, Rectangle size)
voidsetCollection(PdfCollection collection)
Sets the collection dictionary.
voidsetCropBoxSize(Rectangle crop)
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.
booleansetMarginMirroring(boolean MarginMirroring)
booleansetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.
protected voidsetNewPageSizeAndMargins()
voidsetOpenAction(String name)
voidsetOpenAction(PdfAction action)
voidsetPageAction(PdfName actionType, PdfAction action)
voidsetPageCount(int pageN)
Sets the page number.
voidsetPageEmpty(boolean pageEmpty)
voidsetPageLabels(PdfPageLabels pageLabels)
Sets the page labels
booleansetPageSize(Rectangle pageSize)
Sets the pagesize.
voidsetSigFlags(int f)
voidsetStrictImageSequence(boolean strictImageSequence)
Setter for property strictImageSequence.
voidsetThumbnail(Image image)
voidsetTransition(PdfTransition transition)
Sets the transition for the page
voidsetViewerPreferences(int preferences)
voidsetXmpMetadata(byte[] xmpMetadata)
Use this method to set the XMP Metadata.
voidtraverseOutlineCount(PdfOutline outline)
Recursive method to update the count in the outlines.
voidwriteLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio)
Writes a text line to the document.
voidwriteOutlines()
Writes the outline tree to the body of the PDF document.

Field Detail

additionalActions

protected PdfDictionary additionalActions

alignment

protected int alignment
This represents the current alignment of the PDF Elements.

anchorAction

protected PdfAction anchorAction
The current active PdfAction when processing an Anchor.

annotationsImp

PdfAnnotationsImp annotationsImp

boxSize

protected HashMap boxSize
This is the size of the several boxes that will be used in the next page.

collection

protected PdfCollection collection

currentHeight

protected float currentHeight
This is the current height of the document.

currentOutline

protected PdfOutline currentOutline
This is the current PdfOutline in the hierarchy of outlines.

documentFileAttachment

protected HashMap documentFileAttachment

documentLevelJS

protected HashMap documentLevelJS

duration

protected int duration
The duration of the page

firstPageEvent

protected boolean firstPageEvent
Signals that OnOpenDocument should be called.

graphics

protected PdfContentByte graphics
This is the PdfContentByte object, containing the borders and other Graphics.

hangingPunctuation

static final String hangingPunctuation
The characters to be applied the hanging punctuation.

imageEnd

protected float imageEnd
This is the position where the image ends.

imageWait

protected Image imageWait
This is the image that could not be shown on a previous page.

indentation

protected PdfDocument.Indentation indentation

info

protected PdfDocument.PdfInfo info
some meta information about the Document.

isSectionTitle

protected boolean isSectionTitle
Signals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).

Since: 2.1.2

jsCounter

int jsCounter
Stores a list of document level JavaScript actions.

lastElementType

protected int lastElementType
Holds the type of the last element, that has been added to the document.

leading

protected float leading
This represents the leading of the lines.

leadingCount

protected int leadingCount
Signals that the current leading has to be subtracted from a YMark object when positive.

Since: 2.1.2

line

protected PdfLine line
The line that is currently being written.

lines

protected ArrayList lines
The lines that are written until now.

localDestinations

protected TreeMap localDestinations
Stores the destinations keyed by name. Value is Object[]{PdfAction,PdfIndirectReference,PdfDestintion}.

markPoint

protected int markPoint

nextMarginBottom

protected float nextMarginBottom
margin in y direction starting from the bottom. Will be valid in the next page

nextMarginLeft

protected float nextMarginLeft
margin in x direction starting from the left. Will be valid in the next page

nextMarginRight

protected float nextMarginRight
margin in x direction starting from the right. Will be valid in the next page

nextMarginTop

protected float nextMarginTop
margin in y direction starting from the top. Will be valid in the next page

nextPageSize

protected Rectangle nextPageSize
This is the size of the next page.

openActionAction

protected PdfAction openActionAction

openActionName

protected String openActionName

pageAA

protected PdfDictionary pageAA

pageEmpty

protected boolean pageEmpty
This checks if the page is empty.

pageLabels

protected PdfPageLabels pageLabels

pageResources

protected PageResources pageResources
This are the page resources of the current Page.

rootOutline

protected PdfOutline rootOutline
This is the root outline of the document.

strictImageSequence

protected boolean strictImageSequence
Holds value of property strictImageSequence.

SIXTEEN_DIGITS

protected static final DecimalFormat SIXTEEN_DIGITS

text

protected PdfContentByte text
This is the PdfContentByte object, containing the text.

textEmptySize

protected int textEmptySize

thisBoxSize

protected HashMap thisBoxSize
This is the size of the several boxes of the current Page.

thumb

protected PdfIndirectReference thumb

transition

protected PdfTransition transition
The page transition

viewerPreferences

protected PdfViewerPreferencesImp viewerPreferences
Contains the Viewer preferences of this PDF document.

writer

protected PdfWriter writer
The PdfWriter.

xmpMetadata

protected byte[] xmpMetadata
XMP Metadata for the page.

Constructor Detail

PdfDocument

public PdfDocument()
Constructs a new PDF document.

Method Detail

add

public boolean add(Element element)
Signals that an Element was added to the Document.

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

add

protected void add(Image image)
Adds an image to the document.

Parameters: image the Image to add

Throws: PdfException on error DocumentException on error

addAdditionalAction

void addAdditionalAction(PdfName actionType, PdfAction action)

addAnnotation

void addAnnotation(PdfAnnotation annot)

addCalculationOrder

void addCalculationOrder(PdfFormField formField)

addFileAttachment

void addFileAttachment(String description, PdfFileSpecification fs)

addJavaScript

void addJavaScript(PdfAction js)

addJavaScript

void addJavaScript(String name, PdfAction js)

addOutline

void addOutline(PdfOutline outline, String name)
Adds a named outline to the document .

Parameters: outline the outline to be added name the name of this local destination

addPdfTable

private void addPdfTable(Table t)
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

addPTable

void addPTable(PdfPTable ptable)
Adds a PdfPTable to the document.

Parameters: ptable the PdfPTable to be added to the document.

Throws: DocumentException on error

addSpacing

protected void addSpacing(float extraspace, float oldleading, Font f)
Adds extra space. This method should probably be rewritten.

addViewerPreference

void addViewerPreference(PdfName key, PdfObject value)

See Also: PdfViewerPreferences

addWriter

public void addWriter(PdfWriter writer)
Adds a PdfWriter to the PdfDocument.

Parameters: writer the PdfWriter that writes everything what is added to this document to an outputstream.

Throws: DocumentException on error

analyzeRow

protected void analyzeRow(ArrayList rows, PdfDocument.RenderingContext ctx)

bottom

float bottom(Table table)
Returns the bottomvalue of a Table if it were added to this document.

Parameters: table the table that may or may not be added to this document

Returns: a bottom value

calculateOutlineCount

void calculateOutlineCount()
Updates the count in the outlines.

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.

clearTextWrap

public void clearTextWrap()
Method added by Pelikan Stephan

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.

consumeRowspan

protected void consumeRowspan(ArrayList row, PdfDocument.RenderingContext ctx)

doFooter

protected void doFooter()

doHeader

protected void doHeader()

ensureNewLine

protected void ensureNewLine()
Ensures that a new line has been started.

extractRows

protected ArrayList extractRows(ArrayList cells, PdfDocument.RenderingContext ctx)

fitsPage

boolean fitsPage(PdfPTable table, float margin)
Checks if a PdfPTable fits the current page of the PdfDocument.

Parameters: table the table that has to be checked margin a certain margin

Returns: true if the PdfPTable fits the page, false otherwise.

flushLines

protected float flushLines()
Writes all the lines to the text-object.

Returns: the displacement that was caused

Throws: DocumentException on error

getAcroForm

PdfAcroForm getAcroForm()
Gets the AcroForm object.

Returns: the PdfAcroform object of the PdfDocument

getBoxSize

Rectangle getBoxSize(String boxName)
Gives the size of a trim, art, crop or bleed box, or null if not defined.

Parameters: boxName crop, trim, art or bleed

getCatalog

PdfDocument.PdfCatalog getCatalog(PdfIndirectReference pages)
Gets the PdfCatalog-object.

Parameters: pages an indirect reference to this document pages

Returns: PdfCatalog

getDocumentFileAttachment

HashMap getDocumentFileAttachment()

getDocumentLevelJS

HashMap getDocumentLevelJS()

getInfo

PdfDocument.PdfInfo getInfo()
Gets the PdfInfo-object.

Returns: PdfInfo

getLeading

public float getLeading()
Getter for the current leading.

Returns: the current leading

Since: 2.1.2

getLocalGotoAction

PdfAction getLocalGotoAction(String name)

getMarkPoint

int getMarkPoint()

getPageResources

PageResources getPageResources()

getRootOutline

public PdfOutline 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

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.

incMarkPoint

void incMarkPoint()

indentBottom

float indentBottom()
Gets the indentation on the bottom side.

Returns: a margin

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

initPage

protected void initPage()
Initializes a page.

If the footer/header is set, it is printed.

Throws: DocumentException on error

isStrictImageSequence

boolean isStrictImageSequence()
Getter for property strictImageSequence.

Returns: Value of property strictImageSequence.

localDestination

boolean localDestination(String name, PdfDestination destination)
The local destination to where a local goto with the same name will jump to.

Parameters: name the name of this local destination destination the PdfDestination with the jump coordinates

Returns: true if the local destination was added, false if a local destination with the same name already existed

localGoto

void localGoto(String name, float llx, float lly, float urx, float ury)
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 link llx the lower left x corner of the activation area lly the lower left y corner of the activation area urx the upper right x corner of the activation area ury the upper right y corner of the activation area

mayBeRemoved

protected boolean mayBeRemoved(ArrayList row)

newLine

protected void newLine()
Adds the current line to the list of lines and also adds an empty line.

Throws: DocumentException on error

newPage

public boolean newPage()
Makes a new page and sends it to the PdfWriter.

Returns: a boolean

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.

outlineTree

void outlineTree(PdfOutline outline)
Recursive method used to write outlines.

remoteGoto

void remoteGoto(String filename, String name, float llx, float lly, float urx, float ury)
Implements a link to another document.

Parameters: filename the filename for the remote document name the name to jump to llx the lower left x corner of the activation area lly the lower left y corner of the activation area urx the upper right x corner of the activation area ury the upper right y corner of the activation area

remoteGoto

void remoteGoto(String filename, int page, float llx, float lly, float urx, float ury)
Implements a link to another document.

Parameters: filename the filename for the remote document page the page to jump to llx the lower left x corner of the activation area lly the lower left y corner of the activation area urx the upper right x corner of the activation area ury the upper right y corner of the activation area

renderCells

protected void renderCells(PdfDocument.RenderingContext ctx, List cells, boolean hasToFit)

resetFooter

public void resetFooter()
Resets the footer of this document.

resetHeader

public void resetHeader()
Resets the header of this document.

resetPageCount

public void resetPageCount()
Sets the page number to 0.

setAction

void setAction(PdfAction action, float llx, float lly, float urx, float ury)
Implements an action in an area.

Parameters: action the PdfAction llx the lower left x corner of the activation area lly the lower left y corner of the activation area urx the upper right x corner of the activation area ury the upper right y corner of the activation area

setBoxSize

void setBoxSize(String boxName, Rectangle size)

setCollection

public void setCollection(PdfCollection collection)
Sets the collection dictionary.

Parameters: collection a dictionary of type PdfCollection

setCropBoxSize

void setCropBoxSize(Rectangle crop)

setDuration

void setDuration(int seconds)
Sets the display duration for the page (for presentations)

Parameters: seconds the number of seconds to display the page

setFooter

public void setFooter(HeaderFooter footer)
Changes the footer of this document.

Parameters: footer the new footer

setHeader

public void setHeader(HeaderFooter header)
Changes the header of this document.

Parameters: header the new header

setMarginMirroring

public boolean setMarginMirroring(boolean MarginMirroring)

See Also: DocListener

setMargins

public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.

Parameters: marginLeft the margin on the left marginRight the margin on the right marginTop the margin on the top marginBottom the margin on the bottom

Returns: a boolean

setNewPageSizeAndMargins

protected void setNewPageSizeAndMargins()

setOpenAction

void setOpenAction(String name)

setOpenAction

void setOpenAction(PdfAction action)

setPageAction

void setPageAction(PdfName actionType, PdfAction action)

setPageCount

public void setPageCount(int pageN)
Sets the page number.

Parameters: pageN the new page number

setPageEmpty

void setPageEmpty(boolean pageEmpty)

setPageLabels

void setPageLabels(PdfPageLabels pageLabels)
Sets the page labels

Parameters: pageLabels the page labels

setPageSize

public boolean setPageSize(Rectangle pageSize)
Sets the pagesize.

Parameters: pageSize the new pagesize

Returns: true if the page size was set

setSigFlags

void setSigFlags(int f)

setStrictImageSequence

void setStrictImageSequence(boolean strictImageSequence)
Setter for property strictImageSequence.

Parameters: strictImageSequence New value of property strictImageSequence.

setThumbnail

void setThumbnail(Image image)

setTransition

void setTransition(PdfTransition transition)
Sets the transition for the page

Parameters: transition the PdfTransition object

setViewerPreferences

void setViewerPreferences(int preferences)

setXmpMetadata

public void setXmpMetadata(byte[] xmpMetadata)
Use this method to set the XMP Metadata.

Parameters: xmpMetadata The xmpMetadata to set.

traverseOutlineCount

void traverseOutlineCount(PdfOutline outline)
Recursive method to update the count in the outlines.

writeLineToContent

void writeLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio)
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 written text the PdfContentByte where the text will be written to graphics the PdfContentByte where the graphics will be written to currentValues the current font and extra spacing values ratio

Throws: DocumentException on error

writeOutlines

void writeOutlines()
Writes the outline tree to the body of the PDF document.