{
page = kids.get(options.getPage()-1);
}
// Get the AcroForm from the Root-Dictionary and append the annotation
PDAcroForm acroForm = root.getAcroForm();
root.getCOSObject().setNeedToBeUpdate(true);
if (acroForm==null)
{
acroForm = new PDAcroForm(this);
root.setAcroForm(acroForm);
}
else
{
acroForm.getCOSObject().setNeedToBeUpdate(true);
}
/*
* For invisible signatures, the annotation has a rectangle array with values [ 0 0 0 0 ].
* This annotation is usually attached to the viewed page when the signature is created.
* Despite not having an appearance, the annotation AP and N dictionaries may be present
* in some versions of Acrobat. If present, N references the DSBlankXObj (blank) XObject.
*/
// Create Annotation / Field for signature
List<PDAnnotation> annotations = page.getAnnotations();
List<PDField> fields = acroForm.getFields();
PDSignatureField signatureField = null;
if(fields == null)
{
fields = new ArrayList();
acroForm.setFields(fields);
}
for ( PDField pdField : fields )
{
if (pdField instanceof PDSignatureField)
{
PDSignature signature = ((PDSignatureField)pdField).getSignature();
if (signature != null && signature.getDictionary().equals(sigObject.getDictionary()))
{
signatureField = (PDSignatureField)pdField;
}
}
}
if (signatureField == null)
{
signatureField = new PDSignatureField(acroForm);
signatureField.setSignature(sigObject); // append the signature object
signatureField.getWidget().setPage(page); // backward linking
}
// Set the AcroForm Fields
List<PDField> acroFormFields = acroForm.getFields();
COSDictionary acroFormDict = acroForm.getDictionary();
acroFormDict.setDirect(true);
acroFormDict.setInt(COSName.SIG_FLAGS, 3);
boolean checkFields = false;
for ( PDField field : acroFormFields )