private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField) throws SAXException { PDSignature sig = sigField.getSignature(); if (sig == null) { return; } Map<String, String> vals = new TreeMap<>(); vals.put("name", sig.getName()); vals.put("contactInfo", sig.getContactInfo()); vals.put("location", sig.getLocation()); vals.put("reason", sig.getReason()); Calendar cal = sig.getSignDate(); if (cal != null) { dateFormat.setTimeZone(cal.getTimeZone()); vals.put("date", dateFormat.format(cal.getTime())); } // see if there is any data int nonNull = 0; for (String val : vals.keySet()) { if (val != null && !val.equals("")) { nonNull++; } } // if there is, process it if (nonNull > 0) { xhtml.startElement("li", parentAttributes); AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute("", "type", "type", "CDATA", "signaturedata"); xhtml.startElement("ol", attrs); for (Map.Entry<String, String> e : vals.entrySet()) { if (e.getValue() == null || e.getValue().equals("")) { continue; } attrs = new AttributesImpl(); attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey()); xhtml.startElement("li", attrs); xhtml.characters(e.getValue()); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endElement("li"); } }
public static PDSignatureField swapTextWithSignature(PDField box, List<PDField> fields) { PDSignatureField sig = null; try { PDAcroForm acroForm; acroForm = box.getAcroForm(); acroForm.getDictionary().setDirect(true); PDSignature sigobj = new PDSignature(); sig = new PDSignatureField(acroForm); sig.getDictionary().setDirect(true); sig.setSignature(sigobj); sig.getWidget().setPage(box.getWidget().getPage()); sig.setPartialName(box.getPartialName()); sig.setAlternateFieldName(box.getAlternateFieldName()); sig.getDictionary().setItem(COSName.RECT, box.getDictionary().getItem(COSName.RECT)); sig.getDictionary().setNeedToBeUpdate(true); acroForm.getDictionary().setInt(COSName.SIG_FLAGS, 3); acroForm.getDictionary().setNeedToBeUpdate(true); fields.remove(box); fields.add(sig); } catch (IOException ioe) { } return sig; }