Package com.itextpdf.forms.fields
Class PdfFormAnnotation
- java.lang.Object
-
- com.itextpdf.kernel.pdf.PdfObjectWrapper<PdfDictionary>
-
- com.itextpdf.forms.fields.AbstractPdfFormField
-
- com.itextpdf.forms.fields.PdfFormAnnotation
-
public class PdfFormAnnotation extends AbstractPdfFormField
This class represents a single annotation in form fields hierarchy in anAcroForm
.To be able to be wrapped with this
PdfObjectWrapper
thePdfObject
must be indirect.
-
-
Field Summary
Fields Modifier and Type Field Description protected Color
backgroundColor
protected Color
borderColor
protected float
borderWidth
private static float
EPS
private IFormField
formFieldElement
static int
HIDDEN
static int
HIDDEN_BUT_PRINTABLE
private static java.lang.String
LINE_ENDINGS_REGEXP
private static org.slf4j.Logger
LOGGER
static java.lang.String
OFF_STATE_VALUE
Value which represents "off" state of form field.static java.lang.String
ON_STATE_VALUE
Value which represents "on" state of form field.static int
VISIBLE
static int
VISIBLE_BUT_DOES_NOT_PRINT
-
Fields inherited from class com.itextpdf.forms.fields.AbstractPdfFormField
color, DEFAULT_FONT_SIZE, font, fontSize, MIN_FONT_SIZE, parent, pdfConformance
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
PdfFormAnnotation(PdfWidgetAnnotation widget, PdfDocument pdfDocument)
Creates a form field annotation as a wrapper of aPdfWidgetAnnotation
.protected
PdfFormAnnotation(PdfDictionary pdfObject)
Creates a form field annotation as a wrapper object around aPdfDictionary
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.String
appearancePropToCaption(PdfDictionary appearanceCharacteristics)
private static Color
appearancePropToColor(PdfDictionary appearanceCharacteristics, PdfName property)
private Rectangle
applyRotation(int fieldRotation, Rectangle rectangle)
private void
createCheckBox()
(package private) void
createInputButton()
private PdfFormXObject
createN0Layer(float width, float height)
The background layer that is present when creating the signature field.private PdfFormXObject
createN2Layer(float width, float height)
The signature appearance layer that contains information about the signature, e.g.(package private) void
createSigField()
private PdfFormXObject
createTopLayer(float width, float height)
Signature appearance is assembled with the top-level and second-level XObjects and the standard layers.protected void
drawBorder(PdfCanvas canvas, PdfFormXObject xObject, float width, float height)
Draws a border using the borderWidth and borderColor of the form field.protected void
drawCheckBoxAndSaveAppearance(java.lang.String onStateName)
Draw a checkbox and save its appearance.protected void
drawComboBoxAndSaveAppearance()
Draws the appearance of a Combo box form field and saves it into an appearance stream.protected void
drawListFormFieldAndSaveAppearance()
Draws the appearance of a list box form field and saves it into an appearance stream.protected void
drawPushButtonFieldAndSaveAppearance()
Draws the appearance of a push button and saves it into an appearance stream.protected void
drawRadioButtonAndSaveAppearance(java.lang.String value)
Draws the appearance of a radio button with a specified value and saves it into an appearance stream.protected void
drawSignatureFormFieldAndSaveAppearance()
Draws the appearance of a signature field and saves it into an appearance stream.protected void
drawTextFormFieldAndSaveAppearance()
Draws the appearance of a text form field and saves it into an appearance stream.private boolean
fontRequiredForRegeneration()
PdfString
getAlternativeDescription()
Gets the current alternative description.java.lang.String[]
getAppearanceStates()
Gets the appearance state names.Border
getBorder()
Get border object specified in the widget annotation dictionary.float
getBorderWidth()
Gets the border width for the field.private java.lang.String
getCheckBoxValue()
PdfString
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.(package private) float
getFontSize(PdfArray bBox, java.lang.String value)
private java.lang.String
getRadioButtonValue()
protected Rectangle
getRect(PdfDictionary field)
Gets aRectangle
that matches the current size and position of this form field.int
getRotation()
Get rotation property specified in this form annotation.private static PdfArray
getRotationMatrix(int rotation, float height, float width)
PdfWidgetAnnotation
getWidget()
GetsPdfWidgetAnnotation
that this form field refers to.private boolean
isCheckBox()
private boolean
isCombTextFormField()
private static boolean
isFieldInvisible(Rectangle rect)
Gets the visibility status of the signature.static PdfFormAnnotation
makeFormAnnotation(PdfObject pdfObject, PdfDocument document)
Creates aPdfFormAnnotation
object.private void
prepareComboBoxFieldWithCorrectOptionsAndValues(ComboBoxField comboBoxField)
private void
reconstructCheckBoxType()
boolean
regenerateField()
This method regenerates appearance stream of the field.(package private) boolean
regenerateWidget()
(package private) void
retrieveStyles()
PdfFormAnnotation
setAction(PdfAction action)
Sets the action onwidget
of this annotation form field.PdfFormAnnotation
setAlternativeDescription(java.lang.String alternativeDescription)
Changes the alternative description of the annotation to the specified value.PdfFormAnnotation
setAppearance(PdfName appearanceType, java.lang.String appearanceState, PdfStream appearanceStream)
Sets an appearance for (the widgets related to) the form field.PdfFormAnnotation
setBackgroundColor(Color backgroundColor)
Basic setter for thebackgroundColor
property.PdfFormAnnotation
setBorderColor(Color color)
Sets the Border Color.PdfFormAnnotation
setBorderStyle(PdfDictionary style)
Sets the border style for the field.PdfFormAnnotation
setBorderWidth(float borderWidth)
Sets the border width for the field.PdfFormAnnotation
setCaption(java.lang.String caption)
Basic setter for the push button caption.PdfFormAnnotation
setCaption(java.lang.String caption, boolean regenerateField)
Basic setter for the push button caption.PdfFormAnnotation
setCheckBoxAppearanceOnStateName(java.lang.String onStateName)
Sets on state name for the checkbox annotation normal appearance and regenerates widget.PdfFormAnnotation
setFormFieldElement(IFormField element)
This method sets the model element associated with the current annotation and can be useful to take into account when drawing those properties that the annotation does not have.(package private) static void
setMetaInfoToCanvas(Canvas canvas)
private void
setModelElementProperties(Rectangle rectangle)
PdfFormAnnotation
setPage(int pageNum)
Specifies on which page the form field's widget must be shown.PdfFormAnnotation
setRotation(int degRotation)
Basic setter for thedegRotation
property.PdfFormAnnotation
setVisibility(int visibility)
Set the visibility flags of the form field annotation.private void
updateParentFontSize(float fontSize)
-
Methods inherited from class com.itextpdf.forms.fields.AbstractPdfFormField
disableCurrentFieldRegeneration, disableFieldRegeneration, enableCurrentFieldRegeneration, enableFieldRegeneration, equals, getAcroFormObject, getColor, getDocument, getFieldName, getFieldName, getFont, getFontSize, getParent, getParentField, getPdfConformance, getPdfObject, hashCode, isFieldRegenerationEnabled, isTerminalFormField, isWrappedObjectMustBeIndirect, put, release, remove, setColor, setColorNoRegenerate, setFont, setFontAndSize, setFontSize, setFontSize, setFontSizeAutoScale, setParent, updateFontAndFontSize
-
Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, flush, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setModified, setPdfObject, unsetForbidRelease
-
-
-
-
Field Detail
-
HIDDEN
public static final int HIDDEN
- See Also:
- Constant Field Values
-
VISIBLE_BUT_DOES_NOT_PRINT
public static final int VISIBLE_BUT_DOES_NOT_PRINT
- See Also:
- Constant Field Values
-
HIDDEN_BUT_PRINTABLE
public static final int HIDDEN_BUT_PRINTABLE
- See Also:
- Constant Field Values
-
VISIBLE
public static final int VISIBLE
- See Also:
- Constant Field Values
-
OFF_STATE_VALUE
public static final java.lang.String OFF_STATE_VALUE
Value which represents "off" state of form field.- See Also:
- Constant Field Values
-
ON_STATE_VALUE
public static final java.lang.String ON_STATE_VALUE
Value which represents "on" state of form field.- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
LINE_ENDINGS_REGEXP
private static final java.lang.String LINE_ENDINGS_REGEXP
- See Also:
- Constant Field Values
-
EPS
private static final float EPS
- See Also:
- Constant Field Values
-
borderWidth
protected float borderWidth
-
backgroundColor
protected Color backgroundColor
-
borderColor
protected Color borderColor
-
formFieldElement
private IFormField formFieldElement
-
-
Constructor Detail
-
PdfFormAnnotation
protected PdfFormAnnotation(PdfWidgetAnnotation widget, PdfDocument pdfDocument)
Creates a form field annotation as a wrapper of aPdfWidgetAnnotation
.- Parameters:
widget
- The widget which will be a kid of thePdfFormField
pdfDocument
- ThePdfDocument
instance.
-
PdfFormAnnotation
protected PdfFormAnnotation(PdfDictionary pdfObject)
Creates a form field annotation as a wrapper object around aPdfDictionary
. ThisPdfDictionary
must be an indirect object.- Parameters:
pdfObject
- the dictionary to be wrapped, must have an indirect reference.
-
-
Method Detail
-
makeFormAnnotation
public static PdfFormAnnotation makeFormAnnotation(PdfObject pdfObject, PdfDocument document)
Creates aPdfFormAnnotation
object.- Parameters:
pdfObject
- assumed to be either aPdfDictionary
, or aPdfIndirectReference
to aPdfDictionary
.document
- thePdfDocument
to create the field in.- Returns:
- a new
PdfFormAnnotation
, ornull
ifpdfObject
is not a widget annotation.
-
getWidget
public PdfWidgetAnnotation getWidget()
GetsPdfWidgetAnnotation
that this form field refers to.- Returns:
PdfWidgetAnnotation
.
-
getDefaultAppearance
public PdfString 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
.
-
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.
-
getAppearanceStates
public java.lang.String[] getAppearanceStates()
Gets the appearance state names.- Specified by:
getAppearanceStates
in classAbstractPdfFormField
- Returns:
- an array of Strings containing the names of the appearance states.
-
retrieveStyles
void retrieveStyles()
- Overrides:
retrieveStyles
in classAbstractPdfFormField
-
setBackgroundColor
public PdfFormAnnotation setBackgroundColor(Color backgroundColor)
Basic setter for thebackgroundColor
property. Regenerates the field appearance after setting the new value.- Parameters:
backgroundColor
- The new color to be set ornull
if no background needed.- Returns:
- The edited
PdfFormAnnotation
.
-
setCaption
public PdfFormAnnotation setCaption(java.lang.String caption)
Basic setter for the push button caption. Regenerates the field appearance after setting the new caption.- Parameters:
caption
- button caption to be set.- Returns:
- The edited
PdfFormAnnotation
.
-
setCaption
public PdfFormAnnotation setCaption(java.lang.String caption, boolean regenerateField)
Basic setter for the push button caption. Regenerates the field appearance after setting the new caption if corresponding parameter is specified.- Parameters:
caption
- button caption to be set.regenerateField
- true if field should be regenerated, false otherwise.- Returns:
- The edited
PdfFormAnnotation
.
-
getRotation
public int getRotation()
Get rotation property specified in this form annotation.- Returns:
int
value which represents field's rotation
-
setRotation
public PdfFormAnnotation setRotation(int degRotation)
Basic setter for thedegRotation
property. Regenerates the field appearance after setting the new value.- Parameters:
degRotation
- The new degRotation to be set- Returns:
- The edited
PdfFormAnnotation
.
-
setAction
public PdfFormAnnotation setAction(PdfAction action)
Sets the action onwidget
of this annotation form field.- Parameters:
action
- The action.- Returns:
- The edited
PdfFormAnnotation
.
-
setVisibility
public PdfFormAnnotation setVisibility(int visibility)
Set the visibility flags of the form field annotation. Options are: HIDDEN, HIDDEN_BUT_PRINTABLE, VISIBLE, VISIBLE_BUT_DOES_NOT_PRINT.- Parameters:
visibility
- visibility option.- Returns:
- The edited
PdfFormAnnotation
.
-
getBorderWidth
public float getBorderWidth()
Gets the border width for the field.- Returns:
- the current border width.
-
setBorderWidth
public PdfFormAnnotation setBorderWidth(float borderWidth)
Sets the border width for the field.- Parameters:
borderWidth
- The new border width.- Returns:
- The edited
PdfFormAnnotation
.
-
getBorder
public Border getBorder()
Get border object specified in the widget annotation dictionary.- Returns:
Border
specified in the widget annotation dictionary
-
setBorderStyle
public PdfFormAnnotation setBorderStyle(PdfDictionary style)
Sets the border style for the field.- Parameters:
style
- the new border style.- Returns:
- The edited
PdfFormAnnotation
.
-
setBorderColor
public PdfFormAnnotation setBorderColor(Color color)
Sets the Border Color.- Parameters:
color
- the new value for the Border Color.- Returns:
- The edited
PdfFormAnnotation
.
-
setPage
public PdfFormAnnotation setPage(int pageNum)
Specifies on which page the form field's widget must be shown.- Parameters:
pageNum
- the page number.- Returns:
- The edited
PdfFormAnnotation
.
-
setFormFieldElement
public PdfFormAnnotation setFormFieldElement(IFormField element)
This method sets the model element associated with the current annotation and can be useful to take into account when drawing those properties that the annotation does not have. Note that annotation properties will take precedence, so such properties cannot be overridden by using this method (e.g. background, text color, etc.).Also note that the model element won't be used for annotations for choice form field.
- Parameters:
element
- model element to set- Returns:
- this
PdfFormAnnotation
-
setAppearance
public PdfFormAnnotation setAppearance(PdfName appearanceType, java.lang.String appearanceState, PdfStream appearanceStream)
Sets an appearance for (the widgets related to) the form field.- Parameters:
appearanceType
- the type of appearance stream to be added- PdfName.N: normal appearance
- PdfName.R: rollover appearance
- PdfName.D: down appearance
appearanceState
- the state of the form field that needs to be true for the appearance to be used. Differentiates between several streams of the same type.appearanceStream
- the appearance instructions, as aPdfStream
.- Returns:
- The edited
PdfFormAnnotation
.
-
setCheckBoxAppearanceOnStateName
public PdfFormAnnotation setCheckBoxAppearanceOnStateName(java.lang.String onStateName)
Sets on state name for the checkbox annotation normal appearance and regenerates widget.- Parameters:
onStateName
- the new appearance name representing on state.- Returns:
- The edited
PdfFormAnnotation
.
-
setAlternativeDescription
public PdfFormAnnotation setAlternativeDescription(java.lang.String alternativeDescription)
Changes the alternative description of the annotation to the specified value.- Parameters:
alternativeDescription
- string to be used as alternative description.- Returns:
- The edited
PdfFormAnnotation
.
-
getAlternativeDescription
public PdfString getAlternativeDescription()
Gets the current alternative description.- Returns:
- the current alternative description.
-
getRect
protected Rectangle getRect(PdfDictionary field)
Gets aRectangle
that matches the current size and position of this form field.- Parameters:
field
- current form field.- Returns:
- a
Rectangle
that matches the current size and position of this form field annotation.
-
drawBorder
protected void drawBorder(PdfCanvas canvas, PdfFormXObject xObject, float width, float height)
Draws a border using the borderWidth and borderColor of the form field.- Parameters:
canvas
- ThePdfCanvas
on which to drawxObject
- The PdfFormXObjectwidth
- The width of the rectangle to drawheight
- The height of the rectangle to draw
-
drawPushButtonFieldAndSaveAppearance
protected void drawPushButtonFieldAndSaveAppearance()
Draws the appearance of a push button and saves it into an appearance stream.
-
drawSignatureFormFieldAndSaveAppearance
protected void drawSignatureFormFieldAndSaveAppearance()
Draws the appearance of a signature field and saves it into an appearance stream.
-
drawRadioButtonAndSaveAppearance
protected void drawRadioButtonAndSaveAppearance(java.lang.String value)
Draws the appearance of a radio button with a specified value and saves it into an appearance stream.- Parameters:
value
- the value of the radio button.
-
drawListFormFieldAndSaveAppearance
protected void drawListFormFieldAndSaveAppearance()
Draws the appearance of a list box form field and saves it into an appearance stream.
-
drawTextFormFieldAndSaveAppearance
protected void drawTextFormFieldAndSaveAppearance()
Draws the appearance of a text form field and saves it into an appearance stream.
-
drawComboBoxAndSaveAppearance
protected void drawComboBoxAndSaveAppearance()
Draws the appearance of a Combo box form field and saves it into an appearance stream.
-
prepareComboBoxFieldWithCorrectOptionsAndValues
private void prepareComboBoxFieldWithCorrectOptionsAndValues(ComboBoxField comboBoxField)
-
drawCheckBoxAndSaveAppearance
protected void drawCheckBoxAndSaveAppearance(java.lang.String onStateName)
Draw a checkbox and save its appearance.- Parameters:
onStateName
- the name of the appearance state for the checked state
-
setMetaInfoToCanvas
static void setMetaInfoToCanvas(Canvas canvas)
-
regenerateWidget
boolean regenerateWidget()
-
fontRequiredForRegeneration
private boolean fontRequiredForRegeneration()
-
createInputButton
void createInputButton()
-
createSigField
void createSigField()
-
getFontSize
float getFontSize(PdfArray bBox, java.lang.String value)
-
isCombTextFormField
private boolean isCombTextFormField()
-
getRadioButtonValue
private java.lang.String getRadioButtonValue()
-
getCheckBoxValue
private java.lang.String getCheckBoxValue()
-
reconstructCheckBoxType
private void reconstructCheckBoxType()
-
createCheckBox
private void createCheckBox()
-
setModelElementProperties
private void setModelElementProperties(Rectangle rectangle)
-
getRotationMatrix
private static PdfArray getRotationMatrix(int rotation, float height, float width)
-
appearancePropToColor
private static Color appearancePropToColor(PdfDictionary appearanceCharacteristics, PdfName property)
-
appearancePropToCaption
private static java.lang.String appearancePropToCaption(PdfDictionary appearanceCharacteristics)
-
isCheckBox
private boolean isCheckBox()
-
isFieldInvisible
private static boolean isFieldInvisible(Rectangle rect)
Gets the visibility status of the signature.- Returns:
- the visibility status of the signature
-
createTopLayer
private PdfFormXObject createTopLayer(float width, float height)
Signature appearance is assembled with the top-level and second-level XObjects and the standard layers. The AP dictionary’s N attribute references a top-level XObject. This top-level XObject is necessary to properly resize signature appearances when these appearances are referred to by more than one signature field. The top-level XObject performs a Do on the second-level XObjects (n0 and n2 painted in sequence). The matrix may change if the signature is resized.- Parameters:
width
- the width of the annotation rectangle.height
- the height of the annotation rectangle.- Returns:
- top layer xObject (/FRM).
- See Also:
- Adobe Pdf Digital Signature Appearances
-
createN0Layer
private PdfFormXObject createN0Layer(float width, float height)
The background layer that is present when creating the signature field. This layer renders the background and border of the annotation. The Matrix of this XObject is unity and the BBox is that of the original annotation.In the default itext implementation n0 layer is either a blank xObject or normal appearance of the existed field (in case signature field was created but not signed) when reuseAppearance property is true, but user can modify n0 layer manually.
- Parameters:
width
- the width of the annotation rectangle.height
- the height of the annotation rectangle.- Returns:
- n0 layer xObject.
-
createN2Layer
private PdfFormXObject createN2Layer(float width, float height)
The signature appearance layer that contains information about the signature, e.g. the line art for the handwritten signature, the text giving the signer’s name, date, reason, location and so on. The content of this layer can be dynamically created when the signature is created, but thereafter it remains static. Specifically, it remains static when the validity state is changed. All appearance handlers that render text honor the font type and color defaults that were set for the signature annotation. So, this layer is the main layer where signature appearance should be drawn in the current itext implementation.- Parameters:
width
- the width of the annotation rectangle.height
- the height of the annotation rectangle.- Returns:
- n2 layer xObject.
-
updateParentFontSize
private void updateParentFontSize(float fontSize)
-
-