private int numberOfVisibleSignatureFieldsOnSignaturePage(AcroFields readerFields, ArrayList<String> signatureFieldsNames, int signaturePage) { int count = 0; for (String signatureFieldName : signatureFieldsNames) { Item i = readerFields.getFieldItem(signatureFieldName); int page = i.getPage(0); if(page == signaturePage){ PdfDictionary pdct = i.getMerged(0); PdfNumber flags = pdct.getAsNumber(PdfName.F); if ((flags.intValue() & PdfAnnotation.FLAGS_HIDDEN) == 0) { count = count + 1; } } } return count; }
/** * <a href="http://stackoverflow.com/questions/37027579/how-to-associate-a-previous-signature-in-a-new-signature-field"> * How to associate a previous signature in a new signature field * </a> * <br/> * <span>BLANK-signed.pdf, <em>a blank file from elsewhere with an invisible signature.</em></span> * <p> * Quite surprisingly it turns out that changing the signature appearance is possible without * breaking the signature, merely a warning appears which can be hidden by simply signing again. * </p> */ @Test public void testChangeAppearances() throws IOException, DocumentException { try ( InputStream resource = getClass().getResourceAsStream("BLANK-signed.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "BLANK-signed-app.pdf"))) { PdfReader pdfReader = new PdfReader(resource); PdfStamper pdfStamper = new PdfStamper(pdfReader, result, '\0', true); AcroFields acroFields = pdfStamper.getAcroFields(); for (String signatureName : acroFields.getSignatureNames()) { Item field = acroFields.getFieldItem(signatureName); field.writeToAll(PdfName.RECT, new PdfArray(new int[]{100,100,200,200}), Item.WRITE_WIDGET); field.markUsed(acroFields, Item.WRITE_WIDGET); PdfAppearance appearance = PdfAppearance.createAppearance(pdfStamper.getWriter(), 100, 100); appearance.setColorStroke(BaseColor.RED); appearance.moveTo(0, 0); appearance.lineTo(99, 99); appearance.moveTo(0, 99); appearance.lineTo(99, 0); appearance.stroke(); PdfDictionary appDict = new PdfDictionary(); appDict.put(PdfName.N, appearance.getIndirectReference()); field.writeToAll(PdfName.AP, appDict, Item.WRITE_WIDGET); } pdfStamper.close(); } }
/** * When the "signable" aspect is applied, extract the signature fields and add them * to the multivalue property */ public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName) { try { // when the aspect is added, extract the signature fields from the PDF ArrayList<String> signatureFields = new ArrayList<String>(); ContentReader pdfReader = serviceRegistry.getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT); PdfReader reader = new PdfReader(pdfReader.getContentInputStream()); AcroFields form = reader.getAcroFields(); Map<String, Item> fields = form.getFields(); Iterator<String> it = fields.keySet().iterator(); while(it.hasNext()) { String fieldName = it.next(); if(form.getFieldType(fieldName) == AcroFields.FIELD_TYPE_SIGNATURE) { // add this signature field to the list of available fields signatureFields.add(fieldName); } } serviceRegistry.getNodeService().setProperty(nodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS, signatureFields); } catch(IOException ex) { logger.error("Error extracting PDF signature fields from document: " + ex); } }