Class PDAcroForm
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm
-
- All Implemented Interfaces:
COSObjectable
public final class PDAcroForm extends java.lang.Object implements COSObjectable
An interactive form, also known as an AcroForm.
-
-
Field Summary
Fields Modifier and Type Field Description private COSDictionary
dictionary
private PDDocument
document
private java.util.Map<java.lang.String,PDField>
fieldCache
private static int
FLAG_APPEND_ONLY
private static int
FLAG_SIGNATURES_EXIST
private static org.apache.commons.logging.Log
LOG
private ScriptingHandler
scriptingHandler
-
Constructor Summary
Constructors Constructor Description PDAcroForm(PDDocument doc)
Constructor.PDAcroForm(PDDocument doc, COSDictionary form)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.Map<COSDictionary,java.util.Set<COSDictionary>>
buildPagesWidgetsMap(java.util.List<PDField> fields)
FDFDocument
exportFDF()
This will export all FDF form data.private void
fillPagesAnnotationMap(java.util.Map<COSDictionary,java.util.Set<COSDictionary>> pagesAnnotationsMap, PDPage page, PDAnnotationWidget widget)
void
flatten()
This will flatten all form fields.void
flatten(java.util.List<PDField> fields, boolean refreshAppearances)
This will flatten the specified form fields.COSDictionary
getCOSObject()
Convert this standard java object to a COS object.java.lang.String
getDefaultAppearance()
Get the default appearance.PDResources
getDefaultResources()
This will get the default resources for the AcroForm.(package private) PDDocument
getDocument()
This will get the document associated with this form.PDField
getField(java.lang.String fullyQualifiedName)
This will get a field by name, possibly using the cache if setCache is true.java.util.Iterator<PDField>
getFieldIterator()
Returns an iterator which walks all fields in the field tree, in order.java.util.List<PDField>
getFields()
This will return all of the documents root fields.PDFieldTree
getFieldTree()
Return the field tree representing all form fieldsboolean
getNeedAppearances()
True if the viewing application should construct the appearances of all field widgets.int
getQ()
This will get the document-wide default value for the quadding/justification of variable text fields.ScriptingHandler
getScriptingHandler()
Set a handler to support JavaScript actions in the form.private java.awt.geom.Rectangle2D
getTransformedAppearanceBBox(PDAppearanceStream appearanceStream)
Calculate the transformed appearance box.PDXFAResource
getXFA()
Get the XFA resource, the XFA resource is only used for PDF 1.5+ forms.boolean
hasXFA()
This will tell if the AcroForm has XFA content.void
importFDF(FDFDocument fdf)
This method will import an entire FDF document into the PDF document that this acroform is part of.boolean
isAppendOnly()
Determines if AppendOnly is set.boolean
isCachingFields()
This will tell if this acro form is caching the fields.boolean
isSignaturesExist()
Determines if SignaturesExist is set.private boolean
isVisibleAnnotation(PDAnnotation annotation)
void
refreshAppearances()
Refreshes the appearance streams and appearance dictionaries for the widget annotations of all fields.void
refreshAppearances(java.util.List<PDField> fields)
Refreshes the appearance streams and appearance dictionaries for the widget annotations of the specified fields.private void
removeFields(java.util.List<PDField> fields)
private Matrix
resolveTransformationMatrix(PDAnnotation annotation, PDAppearanceStream appearanceStream)
void
setAppendOnly(boolean appendOnly)
Set the AppendOnly bit.void
setCacheFields(boolean cache)
This will tell this form to cache the fields into a Map structure for fast access via the getField method.void
setDefaultAppearance(java.lang.String daValue)
Set the default appearance.void
setDefaultResources(PDResources dr)
This will set the default resources for the acroform.void
setFields(java.util.List<PDField> fields)
Set the documents root fields.void
setNeedAppearances(java.lang.Boolean value)
Set the NeedAppearances value.void
setQ(int q)
This will set the document-wide default value for the quadding/justification of variable text fields.void
setScriptingHandler(ScriptingHandler scriptingHandler)
Set a handler to support JavaScript actions in the form.void
setSignaturesExist(boolean signaturesExist)
Set the SignaturesExist bit.void
setXFA(PDXFAResource xfa)
Set the XFA resource, this is only used for PDF 1.5+ forms.boolean
xfaIsDynamic()
This will tell if the AcroForm is a dynamic XFA form.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
FLAG_SIGNATURES_EXIST
private static final int FLAG_SIGNATURES_EXIST
- See Also:
- Constant Field Values
-
FLAG_APPEND_ONLY
private static final int FLAG_APPEND_ONLY
- See Also:
- Constant Field Values
-
document
private final PDDocument document
-
dictionary
private final COSDictionary dictionary
-
fieldCache
private java.util.Map<java.lang.String,PDField> fieldCache
-
scriptingHandler
private ScriptingHandler scriptingHandler
-
-
Constructor Detail
-
PDAcroForm
public PDAcroForm(PDDocument doc)
Constructor.- Parameters:
doc
- The document that this form is part of.
-
PDAcroForm
public PDAcroForm(PDDocument doc, COSDictionary form)
Constructor. Side effect: /Helv and /ZaDb fonts added with update mark.- Parameters:
doc
- The document that this form is part of.form
- The existing acroForm.
-
-
Method Detail
-
getDocument
PDDocument getDocument()
This will get the document associated with this form.- Returns:
- The PDF document.
-
getCOSObject
public COSDictionary getCOSObject()
Description copied from interface:COSObjectable
Convert this standard java object to a COS object.- Specified by:
getCOSObject
in interfaceCOSObjectable
- Returns:
- The cos object that matches this Java object.
-
importFDF
public void importFDF(FDFDocument fdf) throws java.io.IOException
This method will import an entire FDF document into the PDF document that this acroform is part of.- Parameters:
fdf
- The FDF document to import.- Throws:
java.io.IOException
- If there is an error doing the import.
-
exportFDF
public FDFDocument exportFDF() throws java.io.IOException
This will export all FDF form data.- Returns:
- An FDF document used to export the document.
- Throws:
java.io.IOException
- If there is an error when exporting the document.
-
flatten
public void flatten() throws java.io.IOException
This will flatten all form fields.Flattening a form field will take the current appearance and make that part of the pages content stream. All form fields and annotations associated are removed.
Invisible and hidden fields will be skipped and will not become part of the page content stream
The appearances for the form fields widgets will not be generated
- Throws:
java.io.IOException
-
flatten
public void flatten(java.util.List<PDField> fields, boolean refreshAppearances) throws java.io.IOException
This will flatten the specified form fields.Flattening a form field will take the current appearance and make that part of the pages content stream. All form fields and annotations associated are removed.
Invisible and hidden fields will be skipped and will not become part of the page content stream
- Parameters:
fields
-refreshAppearances
- if set to true the appearances for the form field widgets will be updated- Throws:
java.io.IOException
-
isVisibleAnnotation
private boolean isVisibleAnnotation(PDAnnotation annotation)
-
refreshAppearances
public void refreshAppearances() throws java.io.IOException
Refreshes the appearance streams and appearance dictionaries for the widget annotations of all fields.- Throws:
java.io.IOException
-
refreshAppearances
public void refreshAppearances(java.util.List<PDField> fields) throws java.io.IOException
Refreshes the appearance streams and appearance dictionaries for the widget annotations of the specified fields.- Parameters:
fields
-- Throws:
java.io.IOException
-
getFields
public java.util.List<PDField> getFields()
This will return all of the documents root fields. A field might have children that are fields (non-terminal field) or does not have children which are fields (terminal fields). The fields within an AcroForm are organized in a tree structure. The documents root fields might either be terminal fields, non-terminal fields or a mixture of both. Non-terminal fields mark branches which contents can be retrieved usingPDNonTerminalField.getChildren()
.- Returns:
- A list of the documents root fields, never null. If there are no fields then this method returns an empty list.
-
setFields
public void setFields(java.util.List<PDField> fields)
Set the documents root fields.- Parameters:
fields
- The fields that are part of the documents root fields.
-
getFieldIterator
public java.util.Iterator<PDField> getFieldIterator()
Returns an iterator which walks all fields in the field tree, in order.
-
getFieldTree
public PDFieldTree getFieldTree()
Return the field tree representing all form fields
-
setCacheFields
public void setCacheFields(boolean cache)
This will tell this form to cache the fields into a Map structure for fast access via the getField method. The default is false. You would want this to be false if you were changing the COSDictionary behind the scenes, otherwise setting this to true is acceptable.- Parameters:
cache
- A boolean telling if we should cache the fields.
-
isCachingFields
public boolean isCachingFields()
This will tell if this acro form is caching the fields.- Returns:
- true if the fields are being cached.
-
getField
public PDField getField(java.lang.String fullyQualifiedName)
This will get a field by name, possibly using the cache if setCache is true.- Parameters:
fullyQualifiedName
- The name of the field to get.- Returns:
- The field with that name of null if one was not found.
-
getDefaultAppearance
public java.lang.String getDefaultAppearance()
Get the default appearance.- Returns:
- the DA element of the dictionary object
-
setDefaultAppearance
public void setDefaultAppearance(java.lang.String daValue)
Set the default appearance.- Parameters:
daValue
- a string describing the default appearance
-
getNeedAppearances
public boolean getNeedAppearances()
True if the viewing application should construct the appearances of all field widgets. The default value is false.- Returns:
- the value of NeedAppearances, false if the value isn't set
-
setNeedAppearances
public void setNeedAppearances(java.lang.Boolean value)
Set the NeedAppearances value. If this is false, PDFBox will create appearances for all field widget.- Parameters:
value
- the value for NeedAppearances
-
getDefaultResources
public PDResources getDefaultResources()
This will get the default resources for the AcroForm.- Returns:
- The default resources or null if there is none.
-
setDefaultResources
public void setDefaultResources(PDResources dr)
This will set the default resources for the acroform.- Parameters:
dr
- The new default resources.
-
hasXFA
public boolean hasXFA()
This will tell if the AcroForm has XFA content.- Returns:
- true if the AcroForm is an XFA form
-
xfaIsDynamic
public boolean xfaIsDynamic()
This will tell if the AcroForm is a dynamic XFA form.- Returns:
- true if the AcroForm is a dynamic XFA form
-
getXFA
public PDXFAResource getXFA()
Get the XFA resource, the XFA resource is only used for PDF 1.5+ forms.- Returns:
- The xfa resource or null if it does not exist.
-
setXFA
public void setXFA(PDXFAResource xfa)
Set the XFA resource, this is only used for PDF 1.5+ forms.- Parameters:
xfa
- The xfa resource.
-
getQ
public int getQ()
This will get the document-wide default value for the quadding/justification of variable text fields.0 - Left(default)
1 - Centered
2 - Right
See the QUADDING constants ofPDVariableText
.- Returns:
- The justification of the variable text fields.
-
setQ
public void setQ(int q)
This will set the document-wide default value for the quadding/justification of variable text fields. See the QUADDING constants ofPDVariableText
.- Parameters:
q
- The justification of the variable text fields.
-
isSignaturesExist
public boolean isSignaturesExist()
Determines if SignaturesExist is set.- Returns:
- true if the document contains at least one signature.
-
setSignaturesExist
public void setSignaturesExist(boolean signaturesExist)
Set the SignaturesExist bit.- Parameters:
signaturesExist
- The value for SignaturesExist.
-
isAppendOnly
public boolean isAppendOnly()
Determines if AppendOnly is set.- Returns:
- true if the document contains signatures that may be invalidated if the file is saved.
-
setAppendOnly
public void setAppendOnly(boolean appendOnly)
Set the AppendOnly bit.- Parameters:
appendOnly
- The value for AppendOnly.
-
getScriptingHandler
public ScriptingHandler getScriptingHandler()
Set a handler to support JavaScript actions in the form.- Returns:
- scriptingHandler
-
setScriptingHandler
public void setScriptingHandler(ScriptingHandler scriptingHandler)
Set a handler to support JavaScript actions in the form.- Parameters:
scriptingHandler
-
-
resolveTransformationMatrix
private Matrix resolveTransformationMatrix(PDAnnotation annotation, PDAppearanceStream appearanceStream)
-
getTransformedAppearanceBBox
private java.awt.geom.Rectangle2D getTransformedAppearanceBBox(PDAppearanceStream appearanceStream)
Calculate the transformed appearance box. Apply the Matrix (or an identity transform) to the BBox of the appearance stream- Parameters:
appearanceStream
-- Returns:
- the transformed rectangle
-
buildPagesWidgetsMap
private java.util.Map<COSDictionary,java.util.Set<COSDictionary>> buildPagesWidgetsMap(java.util.List<PDField> fields) throws java.io.IOException
- Throws:
java.io.IOException
-
fillPagesAnnotationMap
private void fillPagesAnnotationMap(java.util.Map<COSDictionary,java.util.Set<COSDictionary>> pagesAnnotationsMap, PDPage page, PDAnnotationWidget widget)
-
removeFields
private void removeFields(java.util.List<PDField> fields)
-
-