Save This Page
Home » iText-src-2.1.3 » com.lowagie » text » pdf » [javadoc | source]
com.lowagie.text.pdf
public class: AcroFields [javadoc | source]
java.lang.Object
   com.lowagie.text.pdf.AcroFields
Query and change fields in existing documents either by method calls or by FDF merging.
Nested Class Summary:
public static class  AcroFields.Item  The field representations for retrieval and modification. 
Field Summary
 PdfReader reader     
 PdfWriter writer     
 HashMap fields     
public static final  int DA_FONT     
public static final  int DA_SIZE     
public static final  int DA_COLOR     
public static final  int FIELD_TYPE_NONE    A field type invalid or not found. 
public static final  int FIELD_TYPE_PUSHBUTTON    A field type. 
public static final  int FIELD_TYPE_CHECKBOX    A field type. 
public static final  int FIELD_TYPE_RADIOBUTTON    A field type. 
public static final  int FIELD_TYPE_TEXT    A field type. 
public static final  int FIELD_TYPE_LIST    A field type. 
public static final  int FIELD_TYPE_COMBO    A field type. 
public static final  int FIELD_TYPE_SIGNATURE    A field type. 
Constructor:
 AcroFields(PdfReader reader,
    PdfWriter writer) 
Method from com.lowagie.text.pdf.AcroFields Summary:
addSubstitutionFont,   decodeGenericDictionary,   exportAsFdf,   extractRevision,   fill,   getAppearance,   getAppearanceStates,   getBlankSignatureNames,   getField,   getFieldCache,   getFieldItem,   getFieldPositions,   getFieldType,   getFields,   getListOptionDisplay,   getListOptionExport,   getListSelection,   getMKColor,   getNewPushbuttonFromField,   getNewPushbuttonFromField,   getRevision,   getSignatureDictionary,   getSignatureNames,   getSubstitutionFonts,   getTotalRevisions,   getTranslatedFieldName,   getXfa,   isGenerateAppearances,   isInAP,   mergeXfaData,   regenerateField,   removeField,   removeField,   removeFieldsFromPage,   renameField,   replacePushbuttonField,   replacePushbuttonField,   setExtraMargin,   setField,   setField,   setFieldCache,   setFieldProperty,   setFieldProperty,   setFields,   setFields,   setGenerateAppearances,   setListOption,   setSubstitutionFonts,   signatureCoversWholeDocument,   splitDAelements,   verifySignature,   verifySignature
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.lowagie.text.pdf.AcroFields Detail:
 public  void addSubstitutionFont(BaseFont font) 
    Adds a substitution font to the list. The fonts in this list will be used if the original font doesn't contain the needed glyphs.
 public  void decodeGenericDictionary(PdfDictionary merged,
    BaseField tx) throws IOException, DocumentException 
 public  void exportAsFdf(FdfWriter writer) 
    Export the fields as a FDF.
 public InputStream extractRevision(String field) throws IOException 
    Extracts a revision from the document.
  void fill() 
 PdfAppearance getAppearance(PdfDictionary merged,
    String text,
    String fieldName) throws IOException, DocumentException 
 public String[] getAppearanceStates(String fieldName) 
    Gets the list of appearance names. Use it to get the names allowed with radio and checkbox fields. If the /Opt key exists the values will also be included. The name 'Off' may also be valid even if not returned in the list.
 public ArrayList getBlankSignatureNames() 
    Gets the field names that have blank signatures.
 public String getField(String name) 
    Gets the field value.
 public HashMap getFieldCache() 
    Gets the appearances cache.
 public AcroFields.Item getFieldItem(String name) 
    Gets the field structure.
 public float[] getFieldPositions(String name) 
    Gets the field box positions in the document. The return is an array of float multiple of 5. For each of this groups the values are: [page, llx, lly, urx, ury]. The coordinates have the page rotation in consideration.
 public int getFieldType(String fieldName) 
    Gets the field type. The type can be one of: FIELD_TYPE_PUSHBUTTON, FIELD_TYPE_CHECKBOX, FIELD_TYPE_RADIOBUTTON, FIELD_TYPE_TEXT, FIELD_TYPE_LIST, FIELD_TYPE_COMBO or FIELD_TYPE_SIGNATURE.

    If the field does not exist or is invalid it returns FIELD_TYPE_NONE.

 public HashMap getFields() 
    Gets all the fields. The fields are keyed by the fully qualified field name and the value is an instance of AcroFields.Item.
 public String[] getListOptionDisplay(String fieldName) 
    Gets the list of display option values from fields of type list or combo. If the field doesn't exist or the field type is not list or combo it will return null.
 public String[] getListOptionExport(String fieldName) 
    Gets the list of export option values from fields of type list or combo. If the field doesn't exist or the field type is not list or combo it will return null.
 public String[] getListSelection(String name) 
    Gets the field values of a Choice field.
 Color getMKColor(PdfArray ar) 
 public PushbuttonField getNewPushbuttonFromField(String field) 
    Creates a new pushbutton from an existing field. If there are several pushbuttons with the same name only the first one is used. This pushbutton can be changed and be used to replace an existing one, with the same name or other name, as long is it is in the same document. To replace an existing pushbutton call #replacePushbuttonField(String,PdfFormField) .
 public PushbuttonField getNewPushbuttonFromField(String field,
    int order) 
    Creates a new pushbutton from an existing field. This pushbutton can be changed and be used to replace an existing one, with the same name or other name, as long is it is in the same document. To replace an existing pushbutton call #replacePushbuttonField(String,PdfFormField,int) .
 public int getRevision(String field) 
    Gets this field revision.
 public PdfDictionary getSignatureDictionary(String name) 
    Gets the signature dictionary, the one keyed by /V.
 public ArrayList getSignatureNames() 
    Gets the field names that have signatures and are signed.
 public ArrayList getSubstitutionFonts() 
    Gets the list of substitution fonts. The list is composed of BaseFont and can be null. The fonts in this list will be used if the original font doesn't contain the needed glyphs.
 public int getTotalRevisions() 
    Gets the total number of revisions this document has.
 public String getTranslatedFieldName(String name) 
    Gets the long XFA translated name.
 public XfaForm getXfa() 
    Gets the XFA form processor.
 public boolean isGenerateAppearances() 
    Gets the property generateAppearances.
 boolean isInAP(PdfDictionary dic,
    PdfName check) 
 public  void mergeXfaData(Node n) throws IOException, DocumentException 
    Merges an XML data structure into this form.
 public boolean regenerateField(String name) throws IOException, DocumentException 
    Regenerates the field appearance. This is useful when you change a field property, but not its value, for instance form.setFieldProperty("f", "bgcolor", Color.BLUE, null); This won't have any effect, unless you use regenerateField("f") after changing the property.
 public boolean removeField(String name) 
    Removes a field from the document.
 public boolean removeField(String name,
    int page) 
    Removes a field from the document. If page equals -1 all the fields with this name are removed from the document otherwise only the fields in that particular page are removed.
 public boolean removeFieldsFromPage(int page) 
    Removes all the fields from page.
 public boolean renameField(String oldName,
    String newName) 
    Renames a field. Only the last part of the name can be renamed. For example, if the original field is "ab.cd.ef" only the "ef" part can be renamed.
 public boolean replacePushbuttonField(String field,
    PdfFormField button) 
    Replaces the first field with a new pushbutton. The pushbutton can be created with #getNewPushbuttonFromField(String) from the same document or it can be a generic PdfFormField of the type pushbutton.
 public boolean replacePushbuttonField(String field,
    PdfFormField button,
    int order) 
    Replaces the designated field with a new pushbutton. The pushbutton can be created with #getNewPushbuttonFromField(String,int) from the same document or it can be a generic PdfFormField of the type pushbutton.
 public  void setExtraMargin(float extraMarginLeft,
    float extraMarginTop) 
    Sets extra margins in text fields to better mimic the Acrobat layout.
 public boolean setField(String name,
    String value) throws IOException, DocumentException 
    Sets the field value.
 public boolean setField(String name,
    String value,
    String display) throws IOException, DocumentException 
    Sets the field value and the display string. The display string is used to build the appearance in the cases where the value is modified by Acrobat with JavaScript and the algorithm is known.
 public  void setFieldCache(HashMap fieldCache) 
    Sets a cache for field appearances. Parsing the existing PDF to create a new TextField is time expensive. For those tasks that repeatedly fill the same PDF with different field values the use of the cache has dramatic speed advantages. An example usage:

    String pdfFile = ...;// the pdf file used as template
    ArrayList xfdfFiles = ...;// the xfdf file names
    ArrayList pdfOutFiles = ...;// the output file names, one for each element in xpdfFiles
    HashMap cache = new HashMap();// the appearances cache
    PdfReader originalReader = new PdfReader(pdfFile);
    for (int k = 0; k < xfdfFiles.size(); ++k) {
    PdfReader reader = new PdfReader(originalReader);
    XfdfReader xfdf = new XfdfReader((String)xfdfFiles.get(k));
    PdfStamper stp = new PdfStamper(reader, new FileOutputStream((String)pdfOutFiles.get(k)));
    AcroFields af = stp.getAcroFields();
    af.setFieldCache(cache);
    af.setFields(xfdf);
    stp.close();
    }
    
 public boolean setFieldProperty(String field,
    String name,
    Object value,
    int[] inst) 
    Sets a field property. Valid property names are:

    • textfont - sets the text font. The value for this entry is a BaseFont.
    • textcolor - sets the text color. The value for this entry is a java.awt.Color.
    • textsize - sets the text size. The value for this entry is a Float.
    • bgcolor - sets the background color. The value for this entry is a java.awt.Color. If null removes the background.
    • bordercolor - sets the border color. The value for this entry is a java.awt.Color. If null removes the border.
 public boolean setFieldProperty(String field,
    String name,
    int value,
    int[] inst) 
    Sets a field property. Valid property names are:

    • flags - a set of flags specifying various characteristics of the field's widget annotation. The value of this entry replaces that of the F entry in the form's corresponding annotation dictionary.
    • setflags - a set of flags to be set (turned on) in the F entry of the form's corresponding widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 1.
    • clrflags - a set of flags to be cleared (turned off) in the F entry of the form's corresponding widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 0.
    • fflags - a set of flags specifying various characteristics of the field. The value of this entry replaces that of the Ff entry in the form's corresponding field dictionary.
    • setfflags - a set of flags to be set (turned on) in the Ff entry of the form's corresponding field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 1.
    • clrfflags - a set of flags to be cleared (turned off) in the Ff entry of the form's corresponding field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 0.
 public  void setFields(FdfReader fdf) throws IOException, DocumentException 
    Sets the fields by FDF merging.
 public  void setFields(XfdfReader xfdf) throws IOException, DocumentException 
    Sets the fields by XFDF merging.
 public  void setGenerateAppearances(boolean generateAppearances) 
    Sets the option to generate appearances. Not generating appearances will speed-up form filling but the results can be unexpected in Acrobat. Don't use it unless your environment is well controlled. The default is true.
 public boolean setListOption(String fieldName,
    String[] exportValues,
    String[] displayValues) 
    Sets the option list for fields of type list or combo. One of exportValues or displayValues may be null but not both. This method will only set the list but will not set the value or appearance. For that, calling setField() is required.

    An example:

    PdfReader pdf = new PdfReader("input.pdf");
    PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("output.pdf"));
    AcroFields af = stp.getAcroFields();
    af.setListOption("ComboBox", new String[]{"a", "b", "c"}, new String[]{"first", "second", "third"});
    af.setField("ComboBox", "b");
    stp.close();
    
 public  void setSubstitutionFonts(ArrayList substitutionFonts) 
    Sets a list of substitution fonts. The list is composed of BaseFont and can also be null. The fonts in this list will be used if the original font doesn't contain the needed glyphs.
 public boolean signatureCoversWholeDocument(String name) 
    Checks is the signature covers the entire document or just part of it.
 public static Object[] splitDAelements(String da) 
 public PdfPKCS7 verifySignature(String name) 
    Verifies a signature. An example usage is:

    KeyStore kall = PdfPKCS7.loadCacertsKeyStore();
    PdfReader reader = new PdfReader("my_signed_doc.pdf");
    AcroFields af = reader.getAcroFields();
    ArrayList names = af.getSignatureNames();
    for (int k = 0; k < names.size(); ++k) {
    String name = (String)names.get(k);
    System.out.println("Signature name: " + name);
    System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
    PdfPKCS7 pk = af.verifySignature(name);
    Calendar cal = pk.getSignDate();
    Certificate pkc[] = pk.getCertificates();
    System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate()));
    System.out.println("Document modified: " + !pk.verify());
    Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
    if (fails == null)
    System.out.println("Certificates verified against the KeyStore");
    else
    System.out.println("Certificate failed: " + fails[1]);
    }
    
 public PdfPKCS7 verifySignature(String name,
    String provider) 
    Verifies a signature. An example usage is:

    KeyStore kall = PdfPKCS7.loadCacertsKeyStore();
    PdfReader reader = new PdfReader("my_signed_doc.pdf");
    AcroFields af = reader.getAcroFields();
    ArrayList names = af.getSignatureNames();
    for (int k = 0; k < names.size(); ++k) {
    String name = (String)names.get(k);
    System.out.println("Signature name: " + name);
    System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
    PdfPKCS7 pk = af.verifySignature(name);
    Calendar cal = pk.getSignDate();
    Certificate pkc[] = pk.getCertificates();
    System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate()));
    System.out.println("Document modified: " + !pk.verify());
    Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
    if (fails == null)
    System.out.println("Certificates verified against the KeyStore");
    else
    System.out.println("Certificate failed: " + fails[1]);
    }