int startIndex = Math.max(Math.min(options.getPage(), 0), pageCount - 1);
PDPage page = catalog.getPages().get(startIndex);
// Get the AcroForm from the Root-Dictionary and append the annotation
PDAcroForm acroForm = catalog.getAcroForm();
catalog.getCOSObject().setNeedToBeUpdate(true);
if (acroForm == null)
{
acroForm = new PDAcroForm(this);
catalog.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<PDFieldTreeNode> fields = acroForm.getFields();
PDSignatureField signatureField = null;
if(fields == null)
{
fields = new ArrayList<PDFieldTreeNode>();
acroForm.setFields(fields);
}
for (PDFieldTreeNode 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<PDFieldTreeNode> acroFormFields = acroForm.getFields();
acroForm.getDictionary().setDirect(true);
acroForm.setSignaturesExist(true);
acroForm.setAppendOnly(true);
boolean checkFields = false;
for (PDFieldTreeNode field : acroFormFields)
{
if (field instanceof PDSignatureField)
{
if (((PDSignatureField) field).getCOSObject().equals(signatureField.getCOSObject()))
{
checkFields = true;
signatureField.getCOSObject().setNeedToBeUpdate(true);
break;
}
}
}
if (!checkFields)
{
acroFormFields.add(signatureField);
}
// Get the object from the visual signature
COSDocument visualSignature = options.getVisualSignature();
// Distinction of case for visual and non-visual signature
if (visualSignature == null) // non-visual signature
{
// Set rectangle for non-visual signature to 0 0 0 0
signatureField.getWidget().setRectangle(new PDRectangle()); // rectangle array [ 0 0 0 0 ]
// Clear AcroForm / Set DefaultRessource
acroForm.setDefaultResources(null);
// Set empty Appearance-Dictionary
PDAppearanceDictionary ap = new PDAppearanceDictionary();
COSStream apsStream = getDocument().createCOSStream();
apsStream.createUnfilteredStream();
PDAppearanceStream aps = new PDAppearanceStream(apsStream);
COSDictionary cosObject = (COSDictionary) aps.getCOSObject();
cosObject.setItem(COSName.SUBTYPE, COSName.FORM);
cosObject.setItem(COSName.BBOX, new PDRectangle());
ap.setNormalAppearance(aps);
ap.getCOSObject().setDirect(true);
signatureField.getWidget().setAppearance(ap);
}
else
// visual signature
{
// Obtain visual signature object
List<COSObject> cosObjects = visualSignature.getObjects();
boolean annotNotFound = true;
boolean sigFieldNotFound = true;
COSDictionary acroFormDict = acroForm.getDictionary();
for (COSObject cosObject : cosObjects)
{
if (!annotNotFound && !sigFieldNotFound)
{
break;