Package com.itextpdf.forms.fields
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
Modifier and TypeFieldDescriptionprotected NullableContainer<CheckBoxType>
static final int
Flag that designates, if set, that the field can contain multiple lines of text.static final int
The NoExport flag, which specifies whether or not exporting is forbidden.static final int
Flag that designates, if set, that the field's contents must be obfuscated.static final int
The ReadOnly flag, which specifies whether or not the field can be changed.static final int
The Required flag, which specifies whether or not the field must be filled in.protected PdfFormXObject
protected ImageData
protected String
Fields inherited from class com.itextpdf.forms.fields.AbstractPdfFormField
color, DEFAULT_FONT_SIZE, font, fontSize, MIN_FONT_SIZE, parent, pdfConformance
-
Constructor Summary
ModifierConstructorDescriptionprotected
PdfFormField
(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
.protected
PdfFormField
(PdfDocument pdfDocument) Creates a minimalPdfFormField
. -
Method Summary
Modifier and TypeMethodDescriptionAdds a new kid to theKids
array property from aAbstractPdfFormField
.addKid
(AbstractPdfFormField kid, boolean throwExceptionOnError) Adds a new kid to theKids
array property from aAbstractPdfFormField
.Adds a new kid to theKids
array property from aPdfWidgetAnnotation
.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 annotationsPdfFormAnnotation
of 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.boolean
getFieldFlag
(int flag) Checks whether a certain flag, or any of a combination of flags, is set for this form field.int
Gets the current list of PDF form field flags.Gets a single child form field's annotationPdfFormAnnotation
.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 wrappedobject. static PdfName
getFormType
(PdfDictionary fieldDict) Returns the type of the form field dictionary, or of the parentobject. 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.getValue()
Gets the current value contained in the form field.Gets the current value contained in the form field.Gets allPdfWidgetAnnotation
that its children refer to.static boolean
isFormField
(PdfDictionary dict) Checks if dictionary contains any of the form field keys.boolean
Checks if the document that contains the field is created in reading mode.boolean
If true, the field can contain multiple lines of text; if false, the field's text is restricted to a single line.boolean
Gets the NoExport attribute.boolean
If true, the field is intended for entering a secure password that should not be echoed visibly to the screen.boolean
Gets the ReadOnly flag, specifying whether or not the field can be changed.boolean
Gets the Required flag, specifying whether or not the field must be filled in.static int
makeFieldFlag
(int bitPosition) Makes a field flag by bit position.static PdfFormField
makeFormField
(PdfObject pdfObject, PdfDocument document) Creates a (subtype of)PdfFormField
object.static AbstractPdfFormField
makeFormFieldOrAnnotation
(PdfObject pdfObject, PdfDocument document) boolean
This method regenerates appearance stream of the field.void
release()
Releases underlying pdf object and other pdf entities used by wrapper.void
removeChild
(AbstractPdfFormField fieldName) Removes the childField object of this field.void
Removes all children from the current field.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'.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.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.void
Updates DA for Variable text, Push button and choice form fields.Methods inherited from class com.itextpdf.forms.fields.AbstractPdfFormField
disableCurrentFieldRegeneration, disableFieldRegeneration, enableCurrentFieldRegeneration, enableFieldRegeneration, equals, getColor, getDocument, getFieldName, getFont, getFontSize, getParent, getParentField, getPdfConformance, getPdfObject, hashCode, isFieldRegenerationEnabled, isTerminalFormField, isWrappedObjectMustBeIndirect, put, remove, setFont, setFontAndSize, setFontSize, setFontSize, setFontSizeAutoScale, setParent
Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, flush, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setModified, setPdfObject, unsetForbidRelease
-
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. -
text
-
img
-
form
-
checkType
-
-
Constructor Details
-
PdfFormField
Creates a form field as a wrapper object around aPdfDictionary
. ThisPdfDictionary
must be an indirect object.- Parameters:
-
pdfObject
- the dictionary to be wrapped, must have an indirect reference.
-
PdfFormField
Creates a minimalPdfFormField
.- Parameters:
-
pdfDocument
- ThePdfDocument
instance.
-
PdfFormField
Creates a form field as a parent of aPdfWidgetAnnotation
.- Parameters:
-
widget
- The widget which will be a kid of thePdfFormField
. -
pdfDocument
- ThePdfDocument
instance.
-
-
Method Details
-
makeFormField
Creates a (subtype of)PdfFormField
object. The type of the object depends on theFT
entry in thepdfObject
parameter.- Parameters:
-
pdfObject
- assumed to be either aPdfDictionary
, or aPdfIndirectReference
to aPdfDictionary
. -
document
- thePdfDocument
to create the field in. - Returns:
-
a new
PdfFormField
, ornull
ifpdfObject
is not a form field.
-
makeFormFieldOrAnnotation
public static AbstractPdfFormField makeFormFieldOrAnnotation(PdfObject pdfObject, PdfDocument document) - Parameters:
-
pdfObject
- assumed to be either aPdfDictionary
, or aPdfIndirectReference
to aPdfDictionary
. -
document
- thePdfDocument
to create the field in. - Returns:
-
a new
AbstractPdfFormField
, ornull
ifpdfObject
is 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 parentobject. - Parameters:
-
fieldDict
- field dictionary to get its type. - Returns:
-
the form type, as a
PdfName
.
-
getFormType
Returns the type of the parent form field, or of the wrappedobject. - Returns:
-
the form type, as a
PdfName
.
-
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. Ifnull
thevalue
parameter 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
Gets the kids of this object.- Returns:
-
contents of the dictionary's
Kids
property, as aPdfArray
.
-
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,null
is 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 theKids
array property from aAbstractPdfFormField
. Also sets the kid'sParent
property to this object.- Parameters:
-
kid
- a newAbstractPdfFormField
entry for the field'sKids
array property. - Returns:
-
the edited
PdfFormField
.
-
addKid
Adds a new kid to theKids
array property from aAbstractPdfFormField
. Also sets the kid'sParent
property to this object.- Parameters:
-
kid
- a newAbstractPdfFormField
entry for the field'sKids
array 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 theKids
array property from aPdfWidgetAnnotation
. Also sets the kid'sParent
property to this object.- Parameters:
-
kid
- a newPdfWidgetAnnotation
entry for the field'sKids
array 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
Gets the current field partial name. -
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
Gets the current alternate name. The alternate is a descriptive name to be used by status messages etc.- Returns:
-
the current alternate name, as a
PdfString
.
-
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
Gets the current mapping name. The mapping name can be used when exporting the form data in the document.- Returns:
-
the current mapping name, as a
PdfString
.
-
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
- anint
interpreted 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
- anint
interpreted 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
- anint
interpreted 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
- anint
interpreted 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
Gets the current value contained in the form field.- Returns:
-
the current value, as a
PdfObject
.
-
getValueAsString
Gets the current value contained in the form field.- Returns:
-
the current value, as a
String
.
-
getDisplayValue
Gets the current display value of the form field. -
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 ofPdfString
objects that each represent the 'on' state of one of the choices. - Returns:
-
the edited
PdfFormField
.
-
getOptions
Gets options for the form field. Should only return usable values for checkboxes and radio buttons. -
getWidgets
Gets allPdfWidgetAnnotation
that its children refer to.- Returns:
-
a list of
PdfWidgetAnnotation
.
-
getChildFormAnnotations
Gets all child form field's annotationsPdfFormAnnotation
of this form field.- Returns:
-
a list of
PdfFormAnnotation
.
-
getFirstFormAnnotation
Gets a single child form field's annotationPdfFormAnnotation
.- Returns:
-
PdfFormAnnotation
or 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:
-
getDefaultAppearance
in 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
Gets a default style string, as described in "Rich Text Strings" section of Pdf spec.- Returns:
-
the default style, as a
PdfString
.
-
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
Gets a rich text string, as described in "Rich Text Strings" section of Pdf spec. May be eitherPdfStream
orPdfString
.- Returns:
- the current rich text value.
-
setRichText
Sets a rich text string, as described in "Rich Text Strings" section of Pdf spec. May be eitherPdfStream
orPdfString
.- 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 callregenerateField
method.- 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:
-
regenerateField
in 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:
-
true
if 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:
-
true
if 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:
-
getAppearanceStates
in 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:
-
release
in classAbstractPdfFormField
-
setColor
Sets the text color and regenerates appearance stream.- Overrides:
-
setColor
in classAbstractPdfFormField
- Parameters:
-
color
- the new value for the Color. - Returns:
-
the edited
AbstractPdfFormField
.
-