I am adding digital signature to my existing pdf, I need advice on how to get the rectangle coordinates to append signature to the new line starting from left 0 of the last page and if the page is full then add a new page. Can I determine if last page has room for my signature? So that I can decide whether to add new page or add to the last page. My pdf will be signed by multiple users and at different times so my plan is to append the first signature at the bottom of last page and then new signatures as they come in. I am using Itext for digital signature and need advice of appending signature to pdf.
public class Signatures {
static void signPdf(SignatureDTO signatureDTO) throws Exception{
KeyStore ks = KeyStore.getInstance("pkcs12", "BC");
ks.load(new FileInputStream(signatureDTO.pks), signatureDTO.pwd.toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, signatureDTO.pwd.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader(signatureDTO.src);
FileOutputStream os = new FileOutputStream(signatureDTO.dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(signatureDTO.reason);
appearance.setLocation(signatureDTO.location);
appearance.setVisibleSignature(signatureDTO.rectangle, signatureDTO.page, signatureDTO.fieldName);
ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS);
}
static class SignatureDTO {
String src,dest,pks,pwd, reason,location;
Rectangle rectangle;
int page;
String fieldName;
}
public static void main(String[] args)
throws Exception {
Security.addProvider(new BouncyCastleProvider());
SignatureDTO signatureDTO= new SignatureDTO();
signatureDTO.dest="resources/OCD-Final-signed1.pdf";
signatureDTO.src="resources/OCD-Final.pdf";
signatureDTO.fieldName="sign1";
signatureDTO.location="VA";
signatureDTO.reason="Approval";
signatureDTO.pks="resources/test1.pfx";
signatureDTO.pwd="test123";
signatureDTO.page=5;
signatureDTO.rectangle= new Rectangle(72, 732, 144, 780);
signPdf(signatureDTO);
signatureDTO.src=signatureDTO.dest;
signatureDTO.dest="resources/OCD-Final-signed2.pdf";
signatureDTO.pks="resources/test5657.pfx";
signatureDTO.fieldName="sign2";
signatureDTO.rectangle= new Rectangle(160, 732, 232, 780);
signPdf(signatureDTO);
}
}
I tried this, find the TextRenderInfo object matching the text I am looking for, but TextRenderInfo does not have any coordinates
PdfReader reader = new PdfReader(signatureDTO.src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
TextMarginFinder finder;
finder= parser.processContent(5, new TextMarginFinder(){
@Override
public void renderText(TextRenderInfo renderInfo) {
super.renderText(renderInfo);
if(renderInfo.getText().equals("Approving Official")){
textRenderInfos.add(renderInfo);
}
}
});
Please advice can I get coordinates from TextRenderInfo
code to find coordinates based on text
PdfReader reader = new PdfReader(signatureDTO.src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
parser.processContent(5, new TextMarginFinder(){
@Override
public void renderText(TextRenderInfo renderInfo) {
super.renderText(renderInfo);
if(mathStr.contains(renderInfo.getText())){
textRenderInfos.put(renderInfo.getText(),renderInfo);
System.out.println(renderInfo.getBaseline().getStartPoint().get(Vector.I1));
System.out.println(renderInfo.getBaseline().getEndPoint().get(Vector.I2));
System.out.println(renderInfo.getBaseline().getEndPoint().get(Vector.I3));
}
}