Package com.itextpdf.forms
Class PdfAcroForm
This class represents the static form technology AcroForm on a PDF file.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intTo be used withsetSignatureFlags(int).protected PdfDocumentThe PdfDocument to which the PdfAcroForm belongs.protected Map<String,PdfFormField> A map of field names and their associatedform fieldobjects.protected booleanKeeps track of whether or not appearances must be generated by the form fields themselves, or by the PDF viewer application.static final intTo be used withsetSignatureFlags(int). -
Method Summary
Modifier and TypeMethodDescriptionvoidaddField(PdfFormField field) This method adds the field to the last page in the document.voidaddField(PdfFormField field, PdfPage page) This method adds the field to a specific page.voidaddField(PdfFormField field, PdfPage page, boolean throwExceptionOnError) This method adds the field to a specific page.voidaddFieldAppearanceToPage(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.voidDisables appearance stream regeneration for all the root fields in the Acroform, so all of its children in the hierarchy will also not be regenerated.voidEnables appearance stream regeneration for all the fields in the Acroform and regenerates them.voidFlattens interactiveform fields in the document.static PdfAcroFormgetAcroForm(PdfDocument document, boolean createIfNotExist) Retrieves AcroForm from the document.static PdfAcroFormgetAcroForm(PdfDocument document, boolean createIfNotExist, OnDuplicateFormFieldNameStrategy onDuplicateFieldNameStrategy) Retrieves AcroForm from the document.Gets allform fields as aMapincluding fields kids.Gets allform fields as aSetincluding fields kids and nameless fields.Gets theCOarray property on the AcroForm.Gets theDAString property on the AcroForm.Gets theQinteger property on the AcroForm.Gets theDRdictionary property on the AcroForm.Gets aform fieldby its name.protected PdfArrayGets all AcroForm fields in the document.Gets a collection ofform fields, prepared for flattening usingpartialFormFlattening(java.lang.String)method.Gets theNeedAppearancesboolean property on the AcroForm.Gets thePdfDocumentthisPdfAcroFormbelongs to.Gets root fields (i.e.intGets theSigFlagsinteger property on the AcroForm.Gets theXfaFormatribute.Gets theXFAproperty on the AcroForm.booleanDetermines whether the AcroForm contains XFA data.booleanGets the attribute generateAppearance, which tellsflattenFields()to generate an appearance Stream for allform fields that don't have one.protected booleanDefines if the object behind this wrapper must be an indirect object in the resultant document.voidpartialFormFlattening(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.voidrelease()Releases underlying pdf object and other pdf entities used by wrapper.booleanremoveField(String fieldName) Tries to remove theform fieldwith the specified name from the document.voidRemoves the XFA stream from the document.voidrenameField(String oldName, String newName) Changes the identifier of aform field.voidreplaceField(String name, PdfFormField field) Replaces thePdfFormFieldof a certain name with anotherPdfFormField.setCalculationOrder(PdfArray calculationOrder) Sets theCOarray property on the AcroForm.setDefaultAppearance(String appearance) Sets theDAString property on the AcroForm.setDefaultJustification(int justification) Sets theQinteger property on the AcroForm.setDefaultResources(PdfDictionary defaultResources) Sets theDRdictionary property on the AcroForm.voidsetGenerateAppearance(boolean generateAppearance) Sets the attribute generateAppearance, which tellsflattenFields()to generate an appearance Stream for allform fields that don't have one.setNeedAppearances(boolean needAppearances) Sets theNeedAppearancesboolean property on the AcroForm.setSignatureFlag(int sigFlag) Changes theSigFlagsinteger property on the AcroForm.setSignatureFlags(int sigFlags) Sets theSigFlagsinteger property on the AcroForm.setXFAResource(PdfArray xfaResource) Sets theXFAproperty on the AcroForm.setXFAResource(PdfStream xfaResource) Sets theXFAproperty 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 fieldobjects. -
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 thePdfAcroFormfrom -
createIfNotExist- whentrue, this method will create aPdfAcroFormif none exists for this document - Returns:
-
the
document's AcroForm, or a new one provided thatcreateIfNotExistparameter 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 thePdfAcroFormfrom -
createIfNotExist- whentrue, this method will create aPdfAcroFormif 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 thatcreateIfNotExistparameter 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- thePdfFormFieldto be added to the form
-
addField
This method adds the field to a specific page.- Parameters:
-
field- thePdfFormFieldto be added to the form -
page- thePdfPageon which to add the field
-
addField
This method adds the field to a specific page.- Parameters:
-
field- thePdfFormFieldto be added to the form -
page- thePdfPageon 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- thePdfFormFieldto be added to the form -
page- thePdfPageon 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 fieldobjects
-
getAllFormFields
Gets allform fields as aMapincluding fields kids.- Returns:
-
a map of field names and their associated
form fieldobjects
-
getAllFormFieldsAndAnnotations
Gets allform fields as aSetincluding fields kids and nameless fields.- Returns:
-
a set of
form fieldobjects.
-
getFieldsForFlattening
Gets a collection ofform fields, prepared for flattening usingpartialFormFlattening(java.lang.String)method. If returned collection is empty, all form fields will be flattened onflattenFieldscall.- Returns:
-
a collection of
form fields for flattening
-
getPdfDocument
Gets thePdfDocumentthisPdfAcroFormbelongs to.- Returns:
- the document of this form
-
setNeedAppearances
Sets theNeedAppearancesboolean 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 theNeedAppearancesboolean 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
NeedAppearancesproperty as aPdfBoolean. Default value isfalse
-
setSignatureFlags
Sets theSigFlagsinteger 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_EXISTand/orAPPEND_ONLY. Use bitwise OR operator to combine these values. Default value is0 - Returns:
- current AcroForm.
-
setSignatureFlag
Changes theSigFlagsinteger 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_EXISTand/orAPPEND_ONLY. Use bitwise OR operator to combine these values. Default is0 - Returns:
- current AcroForm.
-
getSignatureFlags
public int getSignatureFlags()Gets theSigFlagsinteger 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 theCOarray 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 theCOarray 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 theDRdictionary property on the AcroForm.
DRis 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 theDRdictionary property on the AcroForm.
DRis 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 theDAString property on the AcroForm.
This method sets a default (fallback value) for theDAattribute of variable textform fields.- Parameters:
-
appearance- a String containing a sequence of valid PDF syntax - Returns:
- current AcroForm
-
getDefaultAppearance
Gets theDAString property on the AcroForm.
This method returns the default (fallback value) for theDAattribute of variable textform fields.- Returns:
-
the form-wide default appearance, as a
String
-
setDefaultJustification
Sets theQinteger property on the AcroForm.
This method sets a default (fallback value) for theQattribute of variable textform fields.- Parameters:
-
justification- an integer representing a justification value - Returns:
- current AcroForm
- See Also:
-
getDefaultJustification
Gets theQinteger property on the AcroForm.
This method gets the default (fallback value) for theQattribute of variable textform fields.- Returns:
- an integer representing a justification value
- See Also:
-
setXFAResource
Sets theXFAproperty on the AcroForm.
XFAcan either be aPdfStreamor aPdfArray. Its contents must be valid XFA.- Parameters:
-
xfaResource- a stream containing the XDP - Returns:
- current AcroForm
-
setXFAResource
Sets theXFAproperty on the AcroForm.
XFAcan either be aPdfStreamor 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 theXFAproperty on the AcroForm. -
getField
Gets aform fieldby its name.- Parameters:
-
fieldName- the name of theform fieldto retrieve - Returns:
-
the
form field, ornullif it isn't present
-
isGenerateAppearance
public boolean isGenerateAppearance()Gets the attribute generateAppearance, which tellsflattenFields()to generate an appearance Stream for allform fields 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 fields 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, thenNeedAppearancesis 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 fields 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 fieldwith the specified name from the document.- Parameters:
-
fieldName- the name of theform fieldto 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 fieldto 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 fieldto be copied - Returns:
-
a clone of the original
PdfFormField
-
replaceField
Replaces thePdfFormFieldof a certain name with anotherPdfFormField.- Parameters:
-
name- the name of theform fieldto 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
PdfArrayof field dictionaries
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()Description copied from class:PdfObjectWrapperDefines 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:
-
isWrappedObjectMustBeIndirectin 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 theXfaFormatribute.- 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
PdfAcroForminstance
-
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:
-
setModifiedin classPdfObjectWrapper<PdfDictionary>
-