I have a simple signature method to sign PDF documents working fine, it signs the documents with the default stamp that shows Name, Location, Date and Reason.
Now my manager asked me if it is possible to add one more field on it. Let's suppose it is an "Email" field as I haven't been told exactly what they want.
I tried searching and apply somethings I found but nothing has worked.
Also, I was asked if it is possible to remove/hide the label of those 4 default fields (Date, Reason, etc...) and I couldn't manage doing it neither find anything about it, I don't know if it can be really customized.
Here I will show how she signature process are working. In the middel there is some code I found on iText site and tried to use to add the "new field" but without any result different from the default stamp been showed.
public String signPdfFirstTime(String src, String dest, PrivateKey pk, Certificate[] chain, String providerName, String conteudoBase64, X509Certificate cert) throws IOException, DocumentException, GeneralSecurityException {
byte[] content = Base64.decode(conteudoBase64);
FileOutputStream fos = new FileOutputStream(path + File.separator + src);
fos.write(content );
fos.close();
File f = new File(path + File.separator + src);
FileInputStream in = new FileInputStream(f);
PdfReader reader = new PdfReader(in);
FileOutputStream os = new FileOutputStream(path + File.separator + dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("SOME REASON");
appearance.setLocation("SOMEWHERE");
// appearance.getAppearance().curveFromTo(98, 120, 46, 94);
Rectangle rectangle = new Rectangle(0, 0, 500, 70);
appearance.setVisibleSignature(rectangle, 1, "SIGNATURE");
//Here I trid to add fields to the stamp
//NOT WORKING AS I EXPECTED...
PdfWriter writer = stamper.getWriter();
PdfFormField field = PdfFormField.createEmpty(writer);
field.setFieldName("TEST 1111");
TextField name = new TextField(writer,rectangle, "TEST 2222");
PdfFormField personal_name = name.getTextField();
field.addKid(personal_name);
TextField password = new TextField(writer, rectangle, "password");
PdfFormField personal_password = password.getTextField();
field.addKid(personal_password);
stamper.addAnnotation(field, 1);
appearance.setCertificate(cert);
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, providerName);
ExternalDigest digest = new BouncyCastleDigest();
List<CrlClient> crlList = new ArrayList<CrlClient>();
crlList.add(new CrlClientOnline());
LtvVerification v = stamper.getLtvVerification();
OcspClient ocspClient = new OcspClientBouncyCastle();
String url = CertificateUtil.getCRLURL(cert);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509CRL crl = (X509CRL) cf.generateCRL(new URL(url).openStream());
System.out.println("CRL valid until: " + crl.getNextUpdate());
System.out.println("Certificate revoked: " + crl.isRevoked(chain[0]));
if (crl.isRevoked(chain[0])) {
throw new GeneralSecurityException("CERTIFICADO REVOGADO!");
}
else {
MakeSignature.processCrl(cert, crlList);
MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS);
os.close();
byte[] b = this.read(f);
return Base64.encodeBytes(b);
}
}
See Question&Answers more detail:
os