Package com.itextpdf.forms
Class PdfAcroForm
This class represents the static form technology AcroForm on a PDF file.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
To be used withsetSignatureFlags(int)
.protected PdfDocument
The PdfDocument to which the PdfAcroForm belongs.protected Map<String,
PdfFormField> A map of field names and their associatedform field
objects.protected boolean
Keeps track of whether or not appearances must be generated by the form fields themselves, or by the PDF viewer application.static final int
To be used withsetSignatureFlags(int)
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addField
(PdfFormField field) This method adds the field to the last page in the document.void
addField
(PdfFormField field, PdfPage page) This method adds the field to a specific page.void
addField
(PdfFormField field, PdfPage page, boolean throwExceptionOnError) This method adds the field to a specific page.void
addFieldAppearanceToPage
(PdfFormField field, PdfPage page) This method merges field with its annotation and places it on the given page.Creates an in-memory copy of aPdfFormField
.void
Disables appearance stream regeneration for all the root fields in the Acroform, so all of its children in the hierarchy will also not be regenerated.void
Enables appearance stream regeneration for all the fields in the Acroform and regenerates them.void
Flattens interactiveform field
s in the document.static PdfAcroForm
getAcroForm
(PdfDocument document, boolean createIfNotExist) Retrieves AcroForm from the document.static PdfAcroForm
getAcroForm
(PdfDocument document, boolean createIfNotExist, OnDuplicateFormFieldNameStrategy onDuplicateFieldNameStrategy) Retrieves AcroForm from the document.Gets allform field
s as aMap
including fields kids.Gets allform field
s as aSet
including fields kids and nameless fields.Gets theCO
array property on the AcroForm.Gets theDA
String property on the AcroForm.Gets theQ
integer property on the AcroForm.Gets theDR
dictionary property on the AcroForm.Gets aform field
by its name.protected PdfArray
Gets all AcroForm fields in the document.Gets a collection ofform field
s, prepared for flattening usingpartialFormFlattening(java.lang.String)
method.Gets theNeedAppearances
boolean property on the AcroForm.Gets thePdfDocument
thisPdfAcroForm
belongs to.Gets root fields (i.e.int
Gets theSigFlags
integer property on the AcroForm.Gets theXfaForm
atribute.Gets theXFA
property on the AcroForm.boolean
Determines whether the AcroForm contains XFA data.boolean
Gets the attribute generateAppearance, which tellsflattenFields()
to generate an appearance Stream for allform field
s that don't have one.protected boolean
Defines if the object behind this wrapper must be an indirect object in the resultant document.void
partialFormFlattening
(String fieldName) Adds aform field
, identified by name, to the list of fields to be flattened.Put a key/value pair in the dictionary and overwrite previous value if it already exists.void
release()
Releases underlying pdf object and other pdf entities used by wrapper.boolean
removeField
(String fieldName) Tries to remove theform field
with the specified name from the document.void
Removes the XFA stream from the document.void
renameField
(String oldName, String newName) Changes the identifier of aform field
.void
replaceField
(String name, PdfFormField field) Replaces thePdfFormField
of a certain name with anotherPdfFormField
.setCalculationOrder
(PdfArray calculationOrder) Sets theCO
array property on the AcroForm.setDefaultAppearance
(String appearance) Sets theDA
String property on the AcroForm.setDefaultJustification
(int justification) Sets theQ
integer property on the AcroForm.setDefaultResources
(PdfDictionary defaultResources) Sets theDR
dictionary property on the AcroForm.void
setGenerateAppearance
(boolean generateAppearance) Sets the attribute generateAppearance, which tellsflattenFields()
to generate an appearance Stream for allform field
s that don't have one.setNeedAppearances
(boolean needAppearances) Sets theNeedAppearances
boolean property on the AcroForm.setSignatureFlag
(int sigFlag) Changes theSigFlags
integer property on the AcroForm.setSignatureFlags
(int sigFlags) Sets theSigFlags
integer property on the AcroForm.setXFAResource
(PdfArray xfaResource) Sets theXFA
property on the AcroForm.setXFAResource
(PdfStream xfaResource) Sets theXFA
property on the AcroForm.Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, flush, getPdfObject, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setPdfObject, unsetForbidRelease
-
Field Details
-
SIGNATURE_EXIST
public static final int SIGNATURE_EXISTTo be used withsetSignatureFlags(int)
.
If set, the document contains at least one signature field. This flag allows a conforming reader to enable user interface items (such as menu items or pushbuttons) related to signature processing without having to scan the entire document for the presence of signature fields. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- See Also:
-
APPEND_ONLY
public static final int APPEND_ONLYTo be used withsetSignatureFlags(int)
.
If set, the document contains signatures that may be invalidated if the file is saved (written) in a way that alters its previous contents, as opposed to an incremental update. Merely updating the file by appending new information to the end of the previous version is safe. Conforming readers may use this flag to inform a user requesting a full save that signatures will be invalidated and require explicit confirmation before continuing with the operation. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- See Also:
-
generateAppearance
protected boolean generateAppearanceKeeps track of whether or not appearances must be generated by the form fields themselves, or by the PDF viewer application. Default istrue
. -
fields
A map of field names and their associatedform field
objects. -
document
The PdfDocument to which the PdfAcroForm belongs.
-
-
Method Details
-
getAcroForm
Retrieves AcroForm from the document. If there is no AcroForm in the document Catalog and createIfNotExist flag is true then the AcroForm dictionary will be created and added to the document.- Parameters:
-
document
- the document to retrieve thePdfAcroForm
from -
createIfNotExist
- whentrue
, this method will create aPdfAcroForm
if none exists for this document - Returns:
-
the
document
's AcroForm, or a new one provided thatcreateIfNotExist
parameter istrue
, otherwisenull
.
-
getAcroForm
public static PdfAcroForm getAcroForm(PdfDocument document, boolean createIfNotExist, OnDuplicateFormFieldNameStrategy onDuplicateFieldNameStrategy) Retrieves AcroForm from the document. If there is no AcroForm in the document Catalog and createIfNotExist flag is true then the AcroForm dictionary will be created and added to the document.- Parameters:
-
document
- the document to retrieve thePdfAcroForm
from -
createIfNotExist
- whentrue
, this method will create aPdfAcroForm
if none exists for this document -
onDuplicateFieldNameStrategy
- the strategy to be used when a field with the same name already exists - Returns:
-
the
document
's AcroForm, or a new one provided thatcreateIfNotExist
parameter istrue
, otherwisenull
.
-
addField
This method adds the field to the last page in the document. If there's no pages, creates a new one.- Parameters:
-
field
- thePdfFormField
to be added to the form
-
addField
This method adds the field to a specific page.- Parameters:
-
field
- thePdfFormField
to be added to the form -
page
- thePdfPage
on which to add the field
-
addField
This method adds the field to a specific page.- Parameters:
-
field
- thePdfFormField
to be added to the form -
page
- thePdfPage
on which to add the field -
throwExceptionOnError
- true if the exception is expected to be thrown in case of error.
-
addFieldAppearanceToPage
This method merges field with its annotation and places it on the given page. This method also work if the field has more than one widget annotation, but doesn't work with no annotations.- Parameters:
-
field
- thePdfFormField
to be added to the form -
page
- thePdfPage
on which to add the field
-
getRootFormFields
Gets root fields (i.e. direct children of Acroform dictionary).- Returns:
-
a map of field names and their associated
form field
objects
-
getAllFormFields
Gets allform field
s as aMap
including fields kids.- Returns:
-
a map of field names and their associated
form field
objects
-
getAllFormFieldsAndAnnotations
Gets allform field
s as aSet
including fields kids and nameless fields.- Returns:
-
a set of
form field
objects.
-
getFieldsForFlattening
Gets a collection ofform field
s, prepared for flattening usingpartialFormFlattening(java.lang.String)
method. If returned collection is empty, all form fields will be flattened onflattenFields
call.- Returns:
-
a collection of
form field
s for flattening
-
getPdfDocument
Gets thePdfDocument
thisPdfAcroForm
belongs to.- Returns:
- the document of this form
-
setNeedAppearances
Sets theNeedAppearances
boolean property on the AcroForm. NeedAppearances has been deprecated in PDF 2.0.
NeedAppearances is a flag specifying whether to construct appearance streams and appearance dictionaries for all widget annotations in the document. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- Parameters:
-
needAppearances
- a boolean. Default value isfalse
- Returns:
- current AcroForm.
-
getNeedAppearances
Gets theNeedAppearances
boolean property on the AcroForm. NeedAppearances has been deprecated in PDF 2.0.
NeedAppearances is a flag specifying whether to construct appearance streams and appearance dictionaries for all widget annotations in the document. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- Returns:
-
the
NeedAppearances
property as aPdfBoolean
. Default value isfalse
-
setSignatureFlags
Sets theSigFlags
integer property on the AcroForm.
SigFlags is a set of flags specifying various document-level characteristics related to signature fields. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- Parameters:
-
sigFlags
- an integer. UseSIGNATURE_EXIST
and/orAPPEND_ONLY
. Use bitwise OR operator to combine these values. Default value is0
- Returns:
- current AcroForm.
-
setSignatureFlag
Changes theSigFlags
integer property on the AcroForm. This method allows only to add flags, not to remove them.
SigFlags is a set of flags specifying various document-level characteristics related to signature fields. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- Parameters:
-
sigFlag
- an integer. UseSIGNATURE_EXIST
and/orAPPEND_ONLY
. Use bitwise OR operator to combine these values. Default is0
- Returns:
- current AcroForm.
-
getSignatureFlags
public int getSignatureFlags()Gets theSigFlags
integer property on the AcroForm.
SigFlags is a set of flags specifying various document-level characteristics related to signature fields (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")
- Returns:
-
current value for
SigFlags
.
-
setCalculationOrder
Sets theCO
array property on the AcroForm.
CO
, Calculation Order, is an array of indirect references to field dictionaries with calculation actions, defining the calculation order in which their values will be recalculated when the value of any field changes (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")- Parameters:
-
calculationOrder
- an array of indirect references - Returns:
- current AcroForm
-
getCalculationOrder
Gets theCO
array property on the AcroForm.
CO
, Calculation Order, is an array of indirect references to field dictionaries with calculation actions, defining the calculation order in which their values will be recalculated when the value of any field changes (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")- Returns:
- an array of indirect references
-
setDefaultResources
Sets theDR
dictionary property on the AcroForm.
DR
is a resource dictionary containing default resources (such as fonts, patterns, or colour spaces) that shall be used by form field appearance streams. At a minimum, this dictionary shall contain a Font entry specifying the resource name and font dictionary of the default font for displaying text. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")- Parameters:
-
defaultResources
- a resource dictionary - Returns:
- current AcroForm
-
getDefaultResources
Gets theDR
dictionary property on the AcroForm.
DR
is a resource dictionary containing default resources (such as fonts, patterns, or colour spaces) that shall be used by form field appearance streams. At a minimum, this dictionary shall contain a Font entry specifying the resource name and font dictionary of the default font for displaying text. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary")- Returns:
- a resource dictionary
-
setDefaultAppearance
Sets theDA
String property on the AcroForm.
This method sets a default (fallback value) for theDA
attribute of variable textform field
s.- Parameters:
-
appearance
- a String containing a sequence of valid PDF syntax - Returns:
- current AcroForm
-
getDefaultAppearance
Gets theDA
String property on the AcroForm.
This method returns the default (fallback value) for theDA
attribute of variable textform field
s.- Returns:
-
the form-wide default appearance, as a
String
-
setDefaultJustification
Sets theQ
integer property on the AcroForm.
This method sets a default (fallback value) for theQ
attribute of variable textform field
s.- Parameters:
-
justification
- an integer representing a justification value - Returns:
- current AcroForm
- See Also:
-
getDefaultJustification
Gets theQ
integer property on the AcroForm.
This method gets the default (fallback value) for theQ
attribute of variable textform field
s.- Returns:
- an integer representing a justification value
- See Also:
-
setXFAResource
Sets theXFA
property on the AcroForm.
XFA
can either be aPdfStream
or aPdfArray
. Its contents must be valid XFA.- Parameters:
-
xfaResource
- a stream containing the XDP - Returns:
- current AcroForm
-
setXFAResource
Sets theXFA
property on the AcroForm.
XFA
can either be aPdfStream
or aPdfArray
. Its contents must be valid XFA.- Parameters:
-
xfaResource
- an array of text string and stream pairs representing the individual packets comprising the XML Data Package. (ISO 32000-1, section 12.7.2 "Interactive Form Dictionary") - Returns:
- current AcroForm
-
getXFAResource
Gets theXFA
property on the AcroForm. -
getField
Gets aform field
by its name.- Parameters:
-
fieldName
- the name of theform field
to retrieve - Returns:
-
the
form field
, ornull
if it isn't present
-
isGenerateAppearance
public boolean isGenerateAppearance()Gets the attribute generateAppearance, which tellsflattenFields()
to generate an appearance Stream for allform field
s that don't have one.- Returns:
- bolean value indicating if the appearances need to be generated
-
setGenerateAppearance
public void setGenerateAppearance(boolean generateAppearance) Sets the attribute generateAppearance, which tellsflattenFields()
to generate an appearance Stream for allform field
s that don't have one.Not generating appearances will speed up form flattening but the results can be unexpected in Acrobat. Don't use it unless your environment is well controlled. The default is
true
.If generateAppearance is set to
true
, thenNeedAppearances
is set tofalse
. This does not apply vice versa.Note, this method does not change default behaviour of
PdfFormField.setValue(String)
method.- Parameters:
-
generateAppearance
- a boolean
-
flattenFields
public void flattenFields()Flattens interactiveform field
s in the document. If no fields have been explicitly included viapartialFormFlattening(java.lang.String)
, then all fields are flattened. Otherwise only the included fields are flattened. -
removeField
Tries to remove theform field
with the specified name from the document.- Parameters:
-
fieldName
- the name of theform field
to remove - Returns:
- a boolean representing whether or not the removal succeeded.
-
partialFormFlattening
Adds aform field
, identified by name, to the list of fields to be flattened. Does not perform a flattening operation in itself.- Parameters:
-
fieldName
- the name of theform field
to be flattened
-
renameField
Changes the identifier of aform field
.- Parameters:
-
oldName
- the current name of the field -
newName
- the new name of the field. Must not be used currently.
-
copyField
Creates an in-memory copy of aPdfFormField
. This new field is not added to the document.- Parameters:
-
name
- the name of theform field
to be copied - Returns:
-
a clone of the original
PdfFormField
-
replaceField
Replaces thePdfFormField
of a certain name with anotherPdfFormField
.- Parameters:
-
name
- the name of theform field
to be replaced -
field
- the newPdfFormField
-
disableRegenerationForAllFields
public void disableRegenerationForAllFields()Disables appearance stream regeneration for all the root fields in the Acroform, so all of its children in the hierarchy will also not be regenerated. -
enableRegenerationForAllFields
public void enableRegenerationForAllFields()Enables appearance stream regeneration for all the fields in the Acroform and regenerates them. -
getFields
Gets all AcroForm fields in the document.- Returns:
-
a
PdfArray
of field dictionaries
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()Description copied from class:PdfObjectWrapper
Defines if the object behind this wrapper must be an indirect object in the resultant document.
If this method returns true it doesn't necessarily mean that object must be in the indirect state at any moment, but rather defines that when the object will be written to the document it will be transformed into indirect object if it's not indirect yet.
Return value of this method shouldn't depend on any logic, it should return always true or false.- Specified by:
-
isWrappedObjectMustBeIndirect
in classPdfObjectWrapper<PdfDictionary>
- Returns:
- true if in the resultant document the object behind the wrapper must be indirect, otherwise false.
-
hasXfaForm
public boolean hasXfaForm()Determines whether the AcroForm contains XFA data.- Returns:
- a boolean
-
getXfaForm
Gets theXfaForm
atribute.- Returns:
- the XFA form object
-
removeXfaForm
public void removeXfaForm()Removes the XFA stream from the document. -
put
Put a key/value pair in the dictionary and overwrite previous value if it already exists.- Parameters:
-
key
- the key as pdf name -
value
- the value as pdf object - Returns:
-
this
PdfAcroForm
instance
-
release
public void release()Releases underlying pdf object and other pdf entities used by wrapper. This method should be called instead of direct call toPdfObject.release()
if the wrapper is used. -
setModified
- Overrides:
-
setModified
in classPdfObjectWrapper<PdfDictionary>
-