Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel.interactive.form

public final Class PDAcroForm

extends Object
implements COSObjectable
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.pdmodel.common.COSObjectable
Imports
java.awt.geom.GeneralPath, .Rectangle2D, java.io.IOException, java.lang.ref.SoftReference, java.util.ArrayList, .Collections, .HashMap, .HashSet, .Iterator, .List, .Map, .Set, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSName, org.apache.pdfbox.pdmodel.PDDocument, .PDPage, .PDPageContentStream, .PDPageContentStream.AppendMode, .PDPageTree, .PDResources, org.apache.pdfbox.pdmodel.common.COSArrayList, .COSObjectable, .PDRectangle, org.apache.pdfbox.pdmodel.fdf.FDFCatalog, .FDFDictionary, .FDFDocument, .FDFField, org.apache.pdfbox.pdmodel.font.PDFont, org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject, org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation, .PDAnnotationWidget, .PDAppearanceStream, org.apache.pdfbox.util.Matrix

An interactive form, also known as an AcroForm.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private final COSDictionary
private final Map<COSName, SoftReference<PDFont>>
private final PDDocument
private Map<String, PDField>
private static final int
private static final int
private static final Log
private ScriptingHandler

Constructor Summary

AccessConstructor and Description
public
PDAcroForm(PDDocument
The document that this form is part of.
doc
)

Constructor.

public
PDAcroForm(PDDocument
The document that this form is part of.
doc
,
COSDictionary
The existing acroForm.
form
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private Map<COSDictionary, Set<COSDictionary>>
buildPagesWidgetsMap(List<PDField>
a list of fields to be flattened
fields
,
PDPageTree
the page tree
pages
)

Build a map of pages => widgets

private Set<COSDictionary>
createWidgetDictionarySet(List<PDField> fields)

Return a set of all annotation widget dictionaries related to the fields to be flattened.

public FDFDocument

Returns:

An FDF document used to export the document.
exportFDF
()

This will export all FDF form data.

private void
public void
flatten()

This will flatten all form fields.

public void
flatten(List<PDField>
a list of fields to be flattened
fields
,
boolean
if set to true the appearances for the form field widgets will be updated
refreshAppearances
)

This will flatten the specified form fields.

public List<PDField>

Returns:

field list. Note these objects may not be identical to PDField objects retrieved from other methods (depending on cache setting). The best strategy is to call getCOSObject() to check for identity. The list is not backed by the /CO COSArray in the document.
getCalcOrder
()

Return the calculation order in which field values should be recalculated when the value of any field changes.

public COSDictionary
getCOSObject()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Convert this standard java object to a COS object.

public String

Returns:

the DA element of the dictionary object
getDefaultAppearance
()

Get the default appearance.

public PDResources

Returns:

The default resources or null if there is none.
getDefaultResources
()

This will get the default resources for the AcroForm.

pack-priv PDDocument

Returns:

The PDF document.
getDocument
()

This will get the document associated with this form.

public PDField

Returns:

The field with that name of null if one was not found.
getField
(String
The name of the field to get.
fullyQualifiedName
)

This will get a field by name, possibly using the cache if setCache is true.

public Iterator<PDField>

Returns:

an iterator which walks all fields in the field tree
getFieldIterator
()

Returns an iterator which walks all fields in the field tree, in order.

public List<PDField>

Returns:

A list of the documents root fields, never null. If there are no fields then this method returns an empty list.
getFields
()

This will return all of the documents root fields.

public PDFieldTree

Returns:

the field tree representing all form fields
getFieldTree
()

Return the field tree representing all form fields

public boolean

Returns:

the value of NeedAppearances, false if the value isn't set
getNeedAppearances
()

True if the viewing application should construct the appearances of all field widgets.

public int

Returns:

The justification of the variable text fields.
getQ
()

This will get the document-wide default value for the quadding/justification of variable text fields.

public ScriptingHandler

Returns:

scriptingHandler the handler to support JavaScript actions in the form
getScriptingHandler
()

Set a handler to support JavaScript actions in the form.

private Rectangle2D

Returns:

the transformed rectangle
getTransformedAppearanceBBox
(PDAppearanceStream appearanceStream)

Calculate the transformed appearance box.

public PDXFAResource

Returns:

The xfa resource or null if it does not exist.
getXFA
()

Get the XFA resource, the XFA resource is only used for PDF 1.5+ forms.

public boolean

Returns:

true if the AcroForm is an XFA form
hasXFA
()

This will tell if the AcroForm has XFA content.

public void
importFDF(FDFDocument
The FDF document to import.
fdf
)

This method will import an entire FDF document into the PDF document that this acroform is part of.

public boolean

Returns:

true if the document contains signatures that may be invalidated if the file is saved.
isAppendOnly
()

Determines if AppendOnly is set.

public boolean

Returns:

true if the fields are being cached.
isCachingFields
()

This will tell if this acro form is caching the fields.

public boolean

Returns:

true if the document contains at least one signature.
isSignaturesExist
()

Determines if SignaturesExist is set.

private boolean
public void
refreshAppearances()

Refreshes the appearance streams and appearance dictionaries for the widget annotations of all fields.

public void
refreshAppearances(List<PDField>
a list of fields to be refreshed
fields
)

Refreshes the appearance streams and appearance dictionaries for the widget annotations of the specified fields.

private void
private Matrix
public void
setAppendOnly(boolean
The value for AppendOnly.
appendOnly
)

Set the AppendOnly bit.

public void
setCacheFields(boolean
A boolean telling if we should cache the fields.
cache
)

This will tell this form to cache the fields into a Map structure for fast access via the getField method.

public void
setCalcOrder(List<PDField>
The field list.
fields
)

Set the calculation order in which field values should be recalculated when the value of any field changes.

public void
setDefaultAppearance(String
a string describing the default appearance
daValue
)

Set the default appearance.

public void
setDefaultResources(PDResources
The new default resources.
dr
)

This will set the default resources for the acroform.

public void
setFields(List<PDField>
The fields that are part of the documents root fields.
fields
)

Set the documents root fields.

public void
setNeedAppearances(Boolean
the value for NeedAppearances
value
)

Set the NeedAppearances value.

public void
setQ(int
The justification of the variable text fields.
q
)

This will set the document-wide default value for the quadding/justification of variable text fields.

public void
setScriptingHandler(ScriptingHandler
a handler to support JavaScript actions in the form
scriptingHandler
)

Set a handler to support JavaScript actions in the form.

public void
setSignaturesExist(boolean
The value for SignaturesExist.
signaturesExist
)

Set the SignaturesExist bit.

public void
setXFA(PDXFAResource
The xfa resource.
xfa
)

Set the XFA resource, this is only used for PDF 1.5+ forms.

public boolean

Returns:

true if the AcroForm is a dynamic XFA form
xfaIsDynamic
()

This will tell if the AcroForm is a dynamic XFA form.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dictionaryback to summary
private final COSDictionary dictionary
directFontCacheback to summary
private final Map<COSName, SoftReference<PDFont>> directFontCache
documentback to summary
private final PDDocument document
fieldCacheback to summary
private Map<String, PDField> fieldCache
FLAG_APPEND_ONLYback to summary
private static final int FLAG_APPEND_ONLY
FLAG_SIGNATURES_EXISTback to summary
private static final int FLAG_SIGNATURES_EXIST
LOGback to summary
private static final Log LOG
scriptingHandlerback to summary
private ScriptingHandler scriptingHandler

Constructor Detail

PDAcroFormback to summary
public PDAcroForm(PDDocument doc)

Constructor.

Parameters
doc:PDDocument

The document that this form is part of.

PDAcroFormback to summary
public PDAcroForm(PDDocument doc, COSDictionary form)

Constructor.

Parameters
doc:PDDocument

The document that this form is part of.

form:COSDictionary

The existing acroForm.

Method Detail

buildPagesWidgetsMapback to summary
private Map<COSDictionary, Set<COSDictionary>> buildPagesWidgetsMap(List<PDField> fields, PDPageTree pages) throws IOException

Build a map of pages => widgets

Parameters
fields:List<PDField>

a list of fields to be flattened

pages:PDPageTree

the page tree

createWidgetDictionarySetback to summary
private Set<COSDictionary> createWidgetDictionarySet(List<PDField> fields)

Return a set of all annotation widget dictionaries related to the fields to be flattened.

exportFDFback to summary
public FDFDocument exportFDF() throws IOException

This will export all FDF form data.

Returns:FDFDocument

An FDF document used to export the document.

Exceptions
IOException:
If there is an error when exporting the document.
fillPagesAnnotationMapback to summary
private void fillPagesAnnotationMap(Map<COSDictionary, Set<COSDictionary>> pagesAnnotationsMap, PDPage page, PDAnnotationWidget widget)
flattenback to summary
public void flatten() throws IOException

This will flatten all form fields.

Flattening a form field will take the current appearance and make that part of the pages content stream. All form fields and annotations associated are removed.

Invisible and hidden fields will be skipped and will not become part of the page content stream

The appearances for the form fields widgets will not be generated

Exceptions
IOException:
if something went wrong flattening the fields
flattenback to summary
public void flatten(List<PDField> fields, boolean refreshAppearances) throws IOException

This will flatten the specified form fields.

Flattening a form field will take the current appearance and make that part of the pages content stream. All form fields and annotations associated are removed.

Invisible and hidden fields will be skipped and will not become part of the page content stream

Parameters
fields:List<PDField>

a list of fields to be flattened

refreshAppearances:boolean

if set to true the appearances for the form field widgets will be updated

Exceptions
IOException:
if something went wrong flattening the fields
getCalcOrderback to summary
public List<PDField> getCalcOrder()

Return the calculation order in which field values should be recalculated when the value of any field changes. (Read about "Trigger Events" in the PDF specification)

Returns:List<PDField>

field list. Note these objects may not be identical to PDField objects retrieved from other methods (depending on cache setting). The best strategy is to call getCOSObject() to check for identity. The list is not backed by the /CO COSArray in the document.

getCOSObjectback to summary
public COSDictionary getCOSObject()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Doc from org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Convert this standard java object to a COS object.

Returns:COSDictionary

The cos object that matches this Java object.

Annotations
@Override
getDefaultAppearanceback to summary
public String getDefaultAppearance()

Get the default appearance.

Returns:String

the DA element of the dictionary object

getDefaultResourcesback to summary
public PDResources getDefaultResources()

This will get the default resources for the AcroForm.

Returns:PDResources

The default resources or null if there is none.

getDocumentback to summary
pack-priv PDDocument getDocument()

This will get the document associated with this form.

Returns:PDDocument

The PDF document.

getFieldback to summary
public PDField getField(String fullyQualifiedName)

This will get a field by name, possibly using the cache if setCache is true.

Parameters
fullyQualifiedName:String

The name of the field to get.

Returns:PDField

The field with that name of null if one was not found.

getFieldIteratorback to summary
public Iterator<PDField> getFieldIterator()

Returns an iterator which walks all fields in the field tree, in order.

Returns:Iterator<PDField>

an iterator which walks all fields in the field tree

getFieldsback to summary
public List<PDField> getFields()

This will return all of the documents root fields. A field might have children that are fields (non-terminal field) or does not have children which are fields (terminal fields). The fields within an AcroForm are organized in a tree structure. The documents root fields might either be terminal fields, non-terminal fields or a mixture of both. Non-terminal fields mark branches which contents can be retrieved using PDNonTerminalField#getChildren().

Returns:List<PDField>

A list of the documents root fields, never null. If there are no fields then this method returns an empty list.

getFieldTreeback to summary
public PDFieldTree getFieldTree()

Return the field tree representing all form fields

Returns:PDFieldTree

the field tree representing all form fields

getNeedAppearancesback to summary
public boolean getNeedAppearances()

True if the viewing application should construct the appearances of all field widgets. The default value is false.

Returns:boolean

the value of NeedAppearances, false if the value isn't set

getQback to summary
public int getQ()

This will get the document-wide default value for the quadding/justification of variable text fields.

0 - Left(default)
1 - Centered
2 - Right
See the QUADDING constants of PDVariableText.

Returns:int

The justification of the variable text fields.

getScriptingHandlerback to summary
public ScriptingHandler getScriptingHandler()

Set a handler to support JavaScript actions in the form.

Returns:ScriptingHandler

scriptingHandler the handler to support JavaScript actions in the form

getTransformedAppearanceBBoxback to summary
private Rectangle2D getTransformedAppearanceBBox(PDAppearanceStream appearanceStream)

Calculate the transformed appearance box. Apply the Matrix (or an identity transform) to the BBox of the appearance stream

Returns:Rectangle2D

the transformed rectangle

getXFAback to summary
public PDXFAResource getXFA()

Get the XFA resource, the XFA resource is only used for PDF 1.5+ forms.

Returns:PDXFAResource

The xfa resource or null if it does not exist.

hasXFAback to summary
public boolean hasXFA()

This will tell if the AcroForm has XFA content.

Returns:boolean

true if the AcroForm is an XFA form

importFDFback to summary
public void importFDF(FDFDocument fdf) throws IOException

This method will import an entire FDF document into the PDF document that this acroform is part of.

Parameters
fdf:FDFDocument

The FDF document to import.

Exceptions
IOException:
If there is an error doing the import.
isAppendOnlyback to summary
public boolean isAppendOnly()

Determines if AppendOnly is set.

Returns:boolean

true if the document contains signatures that may be invalidated if the file is saved.

isCachingFieldsback to summary
public boolean isCachingFields()

This will tell if this acro form is caching the fields.

Returns:boolean

true if the fields are being cached.

isSignaturesExistback to summary
public boolean isSignaturesExist()

Determines if SignaturesExist is set.

Returns:boolean

true if the document contains at least one signature.

isVisibleAnnotationback to summary
private boolean isVisibleAnnotation(PDAnnotation annotation)
refreshAppearancesback to summary
public void refreshAppearances() throws IOException

Refreshes the appearance streams and appearance dictionaries for the widget annotations of all fields.

Exceptions
IOException:
if the appearance streams could not be refreshed
refreshAppearancesback to summary
public void refreshAppearances(List<PDField> fields) throws IOException

Refreshes the appearance streams and appearance dictionaries for the widget annotations of the specified fields.

Parameters
fields:List<PDField>

a list of fields to be refreshed

Exceptions
IOException:
if the appearance streams could not be refreshed
removeFieldsback to summary
private void removeFields(List<PDField> fields)
resolveTransformationMatrixback to summary
private Matrix resolveTransformationMatrix(PDAnnotation annotation, PDAppearanceStream appearanceStream)
setAppendOnlyback to summary
public void setAppendOnly(boolean appendOnly)

Set the AppendOnly bit.

Parameters
appendOnly:boolean

The value for AppendOnly.

setCacheFieldsback to summary
public void setCacheFields(boolean cache)

This will tell this form to cache the fields into a Map structure for fast access via the getField method. The default is false. You would want this to be false if you were changing the COSDictionary behind the scenes, otherwise setting this to true is acceptable.

Parameters
cache:boolean

A boolean telling if we should cache the fields.

setCalcOrderback to summary
public void setCalcOrder(List<PDField> fields)

Set the calculation order in which field values should be recalculated when the value of any field changes. (Read about "Trigger Events" in the PDF specification)

Parameters
fields:List<PDField>

The field list.

setDefaultAppearanceback to summary
public void setDefaultAppearance(String daValue)

Set the default appearance.

Parameters
daValue:String

a string describing the default appearance

setDefaultResourcesback to summary
public void setDefaultResources(PDResources dr)

This will set the default resources for the acroform.

Parameters
dr:PDResources

The new default resources.

setFieldsback to summary
public void setFields(List<PDField> fields)

Set the documents root fields.

Parameters
fields:List<PDField>

The fields that are part of the documents root fields.

setNeedAppearancesback to summary
public void setNeedAppearances(Boolean value)

Set the NeedAppearances value. If this is false, PDFBox will create appearances for all field widget.

Parameters
value:Boolean

the value for NeedAppearances

setQback to summary
public void setQ(int q)

This will set the document-wide default value for the quadding/justification of variable text fields. See the QUADDING constants of PDVariableText.

Parameters
q:int

The justification of the variable text fields.

setScriptingHandlerback to summary
public void setScriptingHandler(ScriptingHandler scriptingHandler)

Set a handler to support JavaScript actions in the form.

Parameters
scriptingHandler:ScriptingHandler

a handler to support JavaScript actions in the form

setSignaturesExistback to summary
public void setSignaturesExist(boolean signaturesExist)

Set the SignaturesExist bit.

Parameters
signaturesExist:boolean

The value for SignaturesExist.

setXFAback to summary
public void setXFA(PDXFAResource xfa)

Set the XFA resource, this is only used for PDF 1.5+ forms.

Parameters
xfa:PDXFAResource

The xfa resource.

xfaIsDynamicback to summary
public boolean xfaIsDynamic()

This will tell if the AcroForm is a dynamic XFA form.

Returns:boolean

true if the AcroForm is a dynamic XFA form