Class PdfFormField
java.lang.Object
com.itextpdf.kernel.pdf.PdfObjectWrapper<PdfDictionary>
com.itextpdf.forms.fields.AbstractPdfFormField
com.itextpdf.forms.fields.PdfFormField
- Direct Known Subclasses:
PdfButtonFormField, PdfChoiceFormField, PdfSignatureFormField, PdfTextFormField
This class represents a single field or field group in an
AcroForm.
To be able to be wrapped with this PdfObjectWrapper the PdfObject
must be indirect.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected NullableContainer<CheckBoxType> private List<AbstractPdfFormField> private Stringstatic final intFlag that designates, if set, that the field can contain multiple lines of text.static final intThe NoExport flag, which specifies whether or not exporting is forbidden.static final intFlag that designates, if set, that the field's contents must be obfuscated.static final intThe ReadOnly flag, which specifies whether or not the field can be changed.static final intThe Required flag, which specifies whether or not the field must be filled in.protected PdfFormXObjectList of all allowable keys in form fields.protected ImageDataprivate static final org.slf4j.Loggerprotected StringFields inherited from class AbstractPdfFormField
color, DEFAULT_FONT_SIZE, font, fontSize, MIN_FONT_SIZE, parent, pdfConformance -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPdfFormField(PdfWidgetAnnotation widget, PdfDocument pdfDocument) Creates a form field as a parent of aPdfWidgetAnnotation.PdfFormField(PdfDictionary pdfObject) Creates a form field as a wrapper object around aPdfDictionary.protectedPdfFormField(PdfDocument pdfDocument) Creates a minimalPdfFormField. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidAdds a new kid to theKidsarray property from aAbstractPdfFormField.addKid(AbstractPdfFormField kid, boolean throwExceptionOnError) Adds a new kid to theKidsarray property from aAbstractPdfFormField.Adds a new kid to theKidsarray property from aPdfWidgetAnnotation.static voidapplyAccessibilityProperties(PdfFormField formField, IAccessibleElement modelElement, PdfDocument pdfDocument) AppliesAccessibilityPropertiesfor provided form field and its annotation children.private voidcreateKids(PdfDictionary pdfObject) private static PdfStringgenerateDefaultAppearance(PdfName font, float fontSize, Color textColor) private PdfObjectgetAcroFormKey(PdfName key, int type) Gets the currently additional action dictionary for the form field.Gets all childFields of this object, including the children of the children.Gets all childFields of this object, including the children of the children but not annotations.Gets the current alternate name.String[]Gets the appearance state names.getChildField(String fieldName) Gets the child field of form field.Gets the childFields of this object.Gets all child form field's annotationsPdfFormAnnotationof this form field.Gets all child form fields of this form field.Gets default appearance string containing a sequence of valid page-content graphics or text state operators that define such properties as the field's text size and color.Gets a default style string, as described in "Rich Text Strings" section of Pdf spec.Gets the default fallback value for the form field.Gets the current display value of the form field.booleangetFieldFlag(int flag) Checks whether a certain flag, or any of a combination of flags, is set for this form field.intGets the current list of PDF form field flags.Gets a single child form field's annotationPdfFormAnnotation.private PdfNamegetFontNameFromDR(PdfDictionary fontResources, PdfObject font) static Collection<PdfName> Gets a set of all possible form field keys exceptPdfName.Parent.Returns the type of the parent form field, or of the wrapped <PdfDictionary> object.static PdfNamegetFormType(PdfDictionary fieldDict) Returns the type of the form field dictionary, or of the parent <PdfDictionary> object.Gets a code specifying the form of quadding (justification) to be used in displaying the text: 0 Left-justified 1 Centered 2 Right-justifiedgetKids()Gets the kids of this object.Gets the current mapping name.Gets options for the form field.Gets the current field partial name.Gets a rich text string, as described in "Rich Text Strings" section of Pdf spec.static StringgetStringValue(PdfObject value) Retrieves string value fromPdfObjectrepresenting text string or text stream.private static PdfNamegetTypeFromParent(PdfDictionary field) private PdfNamegetUniqueFontNameForDR(PdfDictionary fontResources) getValue()Gets the current value contained in the form field.Gets the current value contained in the form field.Gets allPdfWidgetAnnotationthat its children refer to.private booleanstatic booleanisFormField(PdfDictionary dict) Checks if dictionary contains any of the form field keys.booleanChecks if the document that contains the field is created in reading mode.booleanIf true, the field can contain multiple lines of text; if false, the field's text is restricted to a single line.booleanGets the NoExport attribute.booleanIf true, the field is intended for entering a secure password that should not be echoed visibly to the screen.booleanGets the ReadOnly flag, specifying whether or not the field can be changed.booleanGets the Required flag, specifying whether or not the field must be filled in.static intmakeFieldFlag(int bitPosition) Makes a field flag by bit position.static PdfFormFieldmakeFormField(PdfObject pdfObject, PdfDocument document) Creates a (subtype of)PdfFormFieldobject.static AbstractPdfFormFieldmakeFormFieldOrAnnotation(PdfObject pdfObject, PdfDocument document) private booleanmergeKidsIfKidWithSuchNameExists(AbstractPdfFormField newKid, boolean throwExceptionOnError) private static TextAlignmentnumberToHorizontalAlignment(int alignment) (package private) static StringoptionsArrayToString(PdfArray options) private voidputAcroFormObject(PdfName acroFormKey, PdfObject acroFormObject) Puts object directly to AcroForm dictionary.booleanThis method regenerates appearance stream of the field.voidrelease()Releases underlying pdf object and other pdf entities used by wrapper.voidremoveChild(AbstractPdfFormField fieldName) Removes the childField object of this field.voidRemoves all children from the current field.(package private) voidReplaces /Kids value with passed kids dictionaries, and keeps old flashed fields there.setAdditionalAction(PdfName key, PdfAction action) Sets an additional action for the form field.setAlternativeName(String name) Changes the alternate name of the field to the specified value.setCheckType(CheckBoxType checkType) Changes the type of graphical marker used to mark a checkbox as 'on'.(package private) AbstractPdfFormFieldAdds a field to the children of the current field.Sets the text color and regenerates appearance stream.setDefaultStyle(PdfString defaultStyleString) Sets a default style string, as described in "Rich Text Strings" section of Pdf spec.setDefaultValue(PdfObject value) Sets the default fallback value for the form field.setFieldFlag(int flag) Adds a flag, or combination of flags, for the form field.setFieldFlag(int flag, boolean value) Adds or removes a flag, or combination of flags, for the form field.setFieldFlags(int flags) Sets a flag, or combination of flags, for the form field.setFieldName(String name) Changes the name of the field to the specified value.private PdfFormFieldsetFieldValue(String value, boolean generateAppearance) setJustification(TextAlignment justification) Sets a code specifying the form of quadding (justification) to be used in displaying the text: 0 Left-justified 1 Centered 2 Right-justifiedsetMappingName(String name) Changes the mapping name of the field to the specified value.setNoExport(boolean noExport) Sets the NoExport flag, specifying whether or not exporting is forbidden.setOptions(PdfArray options) Sets options for the form field.setReadOnly(boolean readOnly) Sets the ReadOnly flag, specifying whether or not the field can be changed.setRequired(boolean required) Sets the Required flag, specifying whether or not the field must be filled in.setRichText(PdfObject richText) Sets a rich text string, as described in "Rich Text Strings" section of Pdf spec.Sets a value to the field and generating field appearance if needed.Sets a value to the field (and fields with the same names) and generates field appearance if needed.Set text field value with given font and size.Sets the field value and the display string.private booleanDistinguish mutually exclusive and regular checkboxes: check all the on states of the widgets, if they are not all equal, then consider that this checkbox is mutually exclusive and do nothing, otherwise regenerate normal appearance with value as on appearance state for all the widgets.voidUpdates DA for Variable text, Push button and choice form fields.(package private) voidupdateFontAndFontSize(PdfFont font, float fontSize) Methods inherited from class AbstractPdfFormField
disableCurrentFieldRegeneration, disableFieldRegeneration, enableCurrentFieldRegeneration, enableFieldRegeneration, equals, getAcroFormObject, getColor, getDocument, getFieldName, getFieldName, getFont, getFontSize, getParent, getParentField, getPdfConformance, getPdfObject, hashCode, isFieldRegenerationEnabled, isTerminalFormField, isWrappedObjectMustBeIndirect, put, remove, retrieveStyles, setColorNoRegenerate, setFont, setFontAndSize, setFontSize, setFontSize, setFontSizeAutoScale, setParent
-
Field Details
-
FF_MULTILINE
public static final int FF_MULTILINEFlag that designates, if set, that the field can contain multiple lines of text. -
FF_PASSWORD
public static final int FF_PASSWORDFlag that designates, if set, that the field's contents must be obfuscated. -
FF_READ_ONLY
public static final int FF_READ_ONLYThe ReadOnly flag, which specifies whether or not the field can be changed. -
FF_REQUIRED
public static final int FF_REQUIREDThe Required flag, which specifies whether or not the field must be filled in. -
FF_NO_EXPORT
public static final int FF_NO_EXPORTThe NoExport flag, which specifies whether or not exporting is forbidden. -
FORM_FIELD_KEYS
-
LOGGER
private static final org.slf4j.Logger LOGGER -
text
-
img
-
form
-
checkType
-
displayValue
-
childFields
-
-
Constructor Details
-
PdfFormField
Creates a form field as a wrapper object around aPdfDictionary. ThisPdfDictionarymust be an indirect object.- Parameters:
pdfObject- the dictionary to be wrapped, must have an indirect reference.
-
PdfFormField
Creates a minimalPdfFormField.- Parameters:
pdfDocument- ThePdfDocumentinstance.
-
PdfFormField
Creates a form field as a parent of aPdfWidgetAnnotation.- Parameters:
widget- The widget which will be a kid of thePdfFormField.pdfDocument- ThePdfDocumentinstance.
-
-
Method Details
-
createKids
-
makeFormField
Creates a (subtype of)PdfFormFieldobject. The type of the object depends on theFTentry in thepdfObjectparameter.- Parameters:
pdfObject- assumed to be either aPdfDictionary, or aPdfIndirectReferenceto aPdfDictionary.document- thePdfDocumentto create the field in.- Returns:
- a new
PdfFormField, ornullifpdfObjectis not a form field.
-
makeFormFieldOrAnnotation
public static AbstractPdfFormField makeFormFieldOrAnnotation(PdfObject pdfObject, PdfDocument document) - Parameters:
pdfObject- assumed to be either aPdfDictionary, or aPdfIndirectReferenceto aPdfDictionary.document- thePdfDocumentto create the field in.- Returns:
- a new
AbstractPdfFormField, ornullifpdfObjectis not a form field and is not a widget annotation.
-
makeFieldFlag
public static int makeFieldFlag(int bitPosition) Makes a field flag by bit position. Bit positions are numbered 1 to 32. But position 0 corresponds to flag 1, position 3 corresponds to flag 4 etc.- Parameters:
bitPosition- bit position of a flag in range 1 to 32 from the pdf specification.- Returns:
- corresponding field flag.
-
isFormField
Checks if dictionary contains any of the form field keys.- Parameters:
dict- field dictionary to check.- Returns:
- true if it is a form field dictionary, false otherwise.
-
getFormFieldKeys
Gets a set of all possible form field keys exceptPdfName.Parent.- Returns:
- a set of form field keys.
-
getFormType
Returns the type of the form field dictionary, or of the parent <PdfDictionary> object.- Parameters:
fieldDict- field dictionary to get its type.- Returns:
- the form type, as a
PdfName.
-
getStringValue
-
applyAccessibilityProperties
public static void applyAccessibilityProperties(PdfFormField formField, IAccessibleElement modelElement, PdfDocument pdfDocument) AppliesAccessibilityPropertiesfor provided form field and its annotation children.- Parameters:
formField-PdfFormFieldthe form field to which the accessibility properties should be appliedmodelElement-IAccessibleElementthe form field layout element with accessibility propertiespdfDocument-PdfDocumentthe document to which the form field belongs
-
getFormType
-
setValue
Sets a value to the field and generating field appearance if needed.- Parameters:
value- of the field.- Returns:
- the field.
-
setValue
Sets a value to the field (and fields with the same names) and generates field appearance if needed.- Parameters:
value- of the field.generateAppearance- if false, appearance won't be regenerated.- Returns:
- the field.
-
setValue
Set text field value with given font and size.- Parameters:
value- text value.font- aPdfFont.fontSize- the size of the font.- Returns:
- the edited field.
-
setValue
Sets the field value and the display string. The display string is used to build the appearance.- Parameters:
value- the field value.displayValue- the string that is used for the appearance. Ifnullthevalueparameter will be used.- Returns:
- the edited field.
-
removeChild
Removes the childField object of this field.- Parameters:
fieldName- aPdfFormField, that needs to be removed from form field children.
-
removeChildren
public void removeChildren()Removes all children from the current field. -
getKids
-
getChildFields
Gets the childFields of this object.- Returns:
- the children of the current field.
-
getChildFormFields
Gets all child form fields of this form field. Annotations are not returned.- Returns:
- a list of
PdfFormField.
-
getAllChildFormFields
Gets all childFields of this object, including the children of the children but not annotations.- Returns:
- the children of the current field and their children.
-
getAllChildFields
Gets all childFields of this object, including the children of the children.- Returns:
- the children of the current field and their children.
-
getChildField
Gets the child field of form field. If there is no child field with such name,nullis returned.- Parameters:
fieldName- aString, name of the received field.- Returns:
- the child of the current field as a
PdfFormField.
-
addKid
Adds a new kid to theKidsarray property from aAbstractPdfFormField. Also sets the kid'sParentproperty to this object.- Parameters:
kid- a newAbstractPdfFormFieldentry for the field'sKidsarray property.- Returns:
- the edited
PdfFormField.
-
addKid
Adds a new kid to theKidsarray property from aAbstractPdfFormField. Also sets the kid'sParentproperty to this object.- Parameters:
kid- a newAbstractPdfFormFieldentry for the field'sKidsarray property.throwExceptionOnError- define whether exception (true) or log (false) is expected in case kid with the same name exists and merge of two kids failed.- Returns:
- the edited
PdfFormField.
-
addKid
Adds a new kid to theKidsarray property from aPdfWidgetAnnotation. Also sets the kid'sParentproperty to this object.- Parameters:
kid- a newPdfWidgetAnnotationentry for the field'sKidsarray property.- Returns:
- the edited
PdfFormField.
-
setFieldName
Changes the name of the field to the specified value.- Parameters:
name- the new field name, as a String.- Returns:
- the edited
PdfFormField.
-
getPartialFieldName
-
setAlternativeName
Changes the alternate name of the field to the specified value. The alternate is a descriptive name to be used by status messages etc.- Parameters:
name- the new alternate name, as a String.- Returns:
- the edited
PdfFormField.
-
getAlternativeName
-
setMappingName
Changes the mapping name of the field to the specified value. The mapping name can be used when exporting the form data in the document.- Parameters:
name- the new alternate name, as a String.- Returns:
- the edited field.
-
getMappingName
-
getFieldFlag
public boolean getFieldFlag(int flag) Checks whether a certain flag, or any of a combination of flags, is set for this form field.- Parameters:
flag- anintinterpreted as a series of a binary flags.- Returns:
- true if any of the flags specified in the parameter is also set in the form field.
-
setFieldFlag
Adds a flag, or combination of flags, for the form field. This method is intended to be used one flag at a time, but this is not technically enforced. To replace the current value, usesetFieldFlags(int).- Parameters:
flag- anintinterpreted as a series of a binary flags.- Returns:
- the edited
PdfFormField.
-
setFieldFlag
Adds or removes a flag, or combination of flags, for the form field. This method is intended to be used one flag at a time, but this is not technically enforced. To replace the current value, usesetFieldFlags(int).- Parameters:
flag- anintinterpreted as a series of a binary flags.value- iftrue, adds the flag(s). iffalse, removes the flag(s).- Returns:
- the edited
PdfFormField.
-
isMultiline
public boolean isMultiline()If true, the field can contain multiple lines of text; if false, the field's text is restricted to a single line.- Returns:
- whether the field can span over multiple lines.
-
isPassword
public boolean isPassword()If true, the field is intended for entering a secure password that should not be echoed visibly to the screen. Characters typed from the keyboard should instead be echoed in some unreadable form, such as asterisks or bullet characters.- Returns:
- whether or not the contents of the field must be obfuscated.
-
setFieldFlags
Sets a flag, or combination of flags, for the form field. This method replaces the previous value. Compare withsetFieldFlag(int)which adds a flag to the existing flags.- Parameters:
flags- anintinterpreted as a series of a binary flags.- Returns:
- the edited
PdfFormField.
-
getFieldFlags
public int getFieldFlags()Gets the current list of PDF form field flags.- Returns:
- the current list of flags, encoded as an
int.
-
getValue
-
getValueAsString
-
getDisplayValue
-
setDefaultValue
Sets the default fallback value for the form field.- Parameters:
value- the default value.- Returns:
- the edited
PdfFormField.
-
getDefaultValue
Gets the default fallback value for the form field.- Returns:
- the default value.
-
setAdditionalAction
Sets an additional action for the form field.- Parameters:
key- the dictionary key to use for storing the action.action- the action.- Returns:
- the edited
PdfFormField.
-
getAdditionalAction
Gets the currently additional action dictionary for the form field.- Returns:
- the additional action dictionary.
-
setOptions
Sets options for the form field. Only to be used for checkboxes and radio buttons.- Parameters:
options- an array ofPdfStringobjects that each represent the 'on' state of one of the choices.- Returns:
- the edited
PdfFormField.
-
getOptions
-
getWidgets
Gets allPdfWidgetAnnotationthat its children refer to.- Returns:
- a list of
PdfWidgetAnnotation.
-
getChildFormAnnotations
Gets all child form field's annotationsPdfFormAnnotationof this form field.- Returns:
- a list of
PdfFormAnnotation.
-
getFirstFormAnnotation
Gets a single child form field's annotationPdfFormAnnotation.- Returns:
PdfFormAnnotationor null if there are no child annotations.
-
getDefaultAppearance
Gets default appearance string containing a sequence of valid page-content graphics or text state operators that define such properties as the field's text size and color.- Specified by:
getDefaultAppearancein classAbstractPdfFormField- Returns:
- the default appearance graphics, as a
PdfString.
-
updateDefaultAppearance
public void updateDefaultAppearance()Updates DA for Variable text, Push button and choice form fields. The resources required for DA will be put to AcroForm's DR. Note, for other form field types DA will be removed. -
getJustification
Gets a code specifying the form of quadding (justification) to be used in displaying the text: 0 Left-justified 1 Centered 2 Right-justified- Returns:
- the current justification attribute.
-
setJustification
Sets a code specifying the form of quadding (justification) to be used in displaying the text: 0 Left-justified 1 Centered 2 Right-justified- Parameters:
justification- the value to set the justification attribute to.- Returns:
- the edited
PdfFormField.
-
getDefaultStyle
-
setDefaultStyle
Sets a default style string, as described in "Rich Text Strings" section of Pdf spec.- Parameters:
defaultStyleString- a new default style for the form field.- Returns:
- the edited
PdfFormField.
-
getRichText
-
setRichText
Sets a rich text string, as described in "Rich Text Strings" section of Pdf spec. It may be eitherPdfStreamorPdfString.- Parameters:
richText- a new rich text value- Returns:
- the edited
PdfFormField
-
setCheckType
Changes the type of graphical marker used to mark a checkbox as 'on'. Notice that in order to complete the change one should callregenerateFieldmethod.- Parameters:
checkType- the new checkbox marker.- Returns:
- the edited
PdfFormField.
-
regenerateField
public boolean regenerateField()This method regenerates appearance stream of the field. Use it if you changed any field parameters and didn't use setValue method which generates appearance by itself.- Specified by:
regenerateFieldin classAbstractPdfFormField- Returns:
- whether or not the regeneration was successful.
-
setReadOnly
Sets the ReadOnly flag, specifying whether or not the field can be changed.- Parameters:
readOnly- iftrue, then the field cannot be changed.- Returns:
- the edited
PdfFormField.
-
isReadOnly
public boolean isReadOnly()Gets the ReadOnly flag, specifying whether or not the field can be changed.- Returns:
trueif the field cannot be changed.
-
setRequired
Sets the Required flag, specifying whether or not the field must be filled in.- Parameters:
required- iftrue, then the field must be filled in.- Returns:
- the edited
PdfFormField.
-
isRequired
public boolean isRequired()Gets the Required flag, specifying whether or not the field must be filled in.- Returns:
trueif the field must be filled in.
-
setNoExport
Sets the NoExport flag, specifying whether or not exporting is forbidden.- Parameters:
noExport- iftrue, then exporting is forbidden- Returns:
- the edited
PdfFormField.
-
isNoExport
public boolean isNoExport()Gets the NoExport attribute.- Returns:
- whether exporting the value following a form action is forbidden.
-
isInReadingMode
public boolean isInReadingMode()Checks if the document that contains the field is created in reading mode.- Returns:
- true if reading mode is used, false otherwise.
-
getAppearanceStates
Gets the appearance state names.- Specified by:
getAppearanceStatesin classAbstractPdfFormField- Returns:
- an array of Strings containing the names of the appearance states.
-
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.- Overrides:
releasein classAbstractPdfFormField
-
setColor
Sets the text color and regenerates appearance stream.- Overrides:
setColorin classAbstractPdfFormField- Parameters:
color- the new value for the Color.- Returns:
- the edited
AbstractPdfFormField.
-
updateFontAndFontSize
- Overrides:
updateFontAndFontSizein classAbstractPdfFormField
-
optionsArrayToString
-
setChildField
Adds a field to the children of the current field.- Parameters:
kid- the field, which should become a child.- Returns:
- the kid itself.
-
replaceKids
Replaces /Kids value with passed kids dictionaries, and keeps old flashed fields there. Also updates childFields array forPdfFormField.- Parameters:
kids- collection of new kids.
-
generateDefaultAppearance
-
getTypeFromParent
-
numberToHorizontalAlignment
-
setFieldValue
-
tryGenerateCheckboxAppearance
Distinguish mutually exclusive and regular checkboxes: check all the on states of the widgets, if they are not all equal, then consider that this checkbox is mutually exclusive and do nothing, otherwise regenerate normal appearance with value as on appearance state for all the widgets.- Parameters:
value- not empty value different from "Off".
-
mergeKidsIfKidWithSuchNameExists
private boolean mergeKidsIfKidWithSuchNameExists(AbstractPdfFormField newKid, boolean throwExceptionOnError) -
hasDefaultAppearance
private boolean hasDefaultAppearance() -
getUniqueFontNameForDR
-
getFontNameFromDR
-
putAcroFormObject
Puts object directly to AcroForm dictionary. It works much faster than consequent invocation ofPdfAcroForm.getAcroForm(PdfDocument, boolean)andPdfObjectWrapper.getPdfObject().Note, this method assume that Catalog already has AcroForm object.
addAcroFormToCatalog()should be called explicitly.- Parameters:
acroFormKey- the key of the object.acroFormObject- the object to add.
-
addAcroFormToCatalog
private void addAcroFormToCatalog() -
getAcroFormKey
-