// 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;
}
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;
// read and set Appearance Dictionary
PDAppearanceDictionary ap =
new PDAppearanceDictionary((COSDictionary)cosBaseDict.getDictionaryObject(COSName.AP));
ap.getCOSObject().setDirect(true);
signatureField.getWidget().setAppearance(ap);
// read and set AcroForm DefaultResource
COSBase dr = cosBaseDict.getItem(COSName.DR);
if (dr != null)