0

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));
                }
            }
user884424
  • 573
  • 1
  • 12
  • 33
  • This is a duplicate of http://stackoverflow.com/questions/21143938/find-location-last-object-on-pdf-page-using-itextsharp/21156347#21156347 and http://stackoverflow.com/questions/13021193/image-positioning-in-itext-java/13029840#13029840 and http://stackoverflow.com/questions/14336374/merge-content-of-pdf-files-using-itextsharp/14355016#14355016 – Bruno Lowagie Mar 04 '14 at 14:54
  • the example shows http://itextpdf.com/examples/iia.php?id=280 about adding margin on top of the page, My question is how to find coordinates of my text, I created my pdf using jasper reports , My pdf has Official tittle and a blank line for his signature, now I want to find the coordinates of this line get its coordinates and pass it to PdfSignatureAppearance so that my signature appears on the line. If I cannot find line can I find the text coordinates of String Official tittle? Please advice. – user884424 Mar 04 '14 at 23:50
  • There also are many questions on so dealing with how to find the coordinates of a given text. Essentially they use text extraction with custom strategies. – mkl Mar 05 '14 at 05:26
  • can you please guide me where to find text extraction strategies? – user884424 Mar 05 '14 at 12:38
  • Your edit shows that you have found code to extract coordinates with. You probably should not only add that code but also some information what you still are missing. – mkl Mar 05 '14 at 21:56
  • *can you please guide me where to find text extraction strategies?* - You can e.g. find the current version of the LocationTextExtractionStrategy [here](http://svn.code.sf.net/p/itext/code/trunk/itext/src/main/java/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java). – mkl Mar 05 '14 at 22:01

0 Answers0