// 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 )
{
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
acroFormDict.setItem(COSName.DR, 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.getDictionary().setDirect(true);
signatureField.getWidget().setAppearance(ap);
}
else // visual signature
{
// Obtain visual signature object
List<COSObject> cosObjects = visualSignature.getObjects();
boolean annotNotFound = true;
boolean sigFieldNotFound = true;
for ( COSObject cosObject : cosObjects )
{
if (!annotNotFound && !sigFieldNotFound)
{
break;
}
COSBase base = cosObject.getObject();
if (base != null && base instanceof COSDictionary)
{
COSBase ft = ((COSDictionary)base).getItem(COSName.FT);
COSBase type = ((COSDictionary)base).getItem(COSName.TYPE);
COSBase apDict = ((COSDictionary)base).getItem(COSName.AP);
// Search for signature annotation
if (annotNotFound && COSName.ANNOT.equals(type))
{
COSDictionary cosBaseDict = (COSDictionary)base;
// Read and set the Rectangle for visual signature
COSArray rectAry = (COSArray)cosBaseDict.getItem(COSName.RECT);
PDRectangle rect = new PDRectangle(rectAry);
signatureField.getWidget().setRectangle(rect);
annotNotFound = false;
}
// Search for Signature-Field
if (sigFieldNotFound && COSName.SIG.equals(ft) && apDict != null)
{
COSDictionary cosBaseDict = (COSDictionary)base;
// Appearance Dictionary auslesen und setzen
PDAppearanceDictionary ap =
new PDAppearanceDictionary((COSDictionary)cosBaseDict.getItem(COSName.AP));
ap.getDictionary().setDirect(true);
signatureField.getWidget().setAppearance(ap);
// AcroForm DefaultRessource auslesen und setzen
COSBase dr = cosBaseDict.getItem(COSName.DR);
dr.setDirect(true);
dr.setNeedToBeUpdate(true);
acroFormDict.setItem(COSName.DR, dr);
sigFieldNotFound=false;
}
}
}
if (annotNotFound || sigFieldNotFound )
{
throw new SignatureException(SignatureException.VISUAL_SIGNATURE_INVALID,
"Could not read all needed objects from template");
}
}
// Get the annotations of the page and append the signature-annotation to it
if (annotations == null)
{
annotations = new COSArrayList();
page.setAnnotations(annotations);
}
// take care that page and acroforms do not share the same array (if so, we don't need to add it twice)
if (!((annotations instanceof COSArrayList)
&& (acroFormFields instanceof COSArrayList)
&& (((COSArrayList)annotations).toList().equals(((COSArrayList)acroFormFields).toList())))
&& !checkFields)
{
annotations.add(signatureField.getWidget());
}
page.getCOSObject().setNeedToBeUpdate(true);
}