javaitextsignpdfstamper

iTextPDF Signature: how to not show signature fields, but show an image


I use itextpdf-5 for signing on PDF. I would like to sign the document, but to make invisible all the fields (reason, location) except the image. I can do this in a third-party program (I attach an example of such a PDF: the signature stores all the data, but does not display). I did not find anything like this in the documentation to do so in my program (I am enclosing code example)

Example PDF with hide data

 private static void emptySignature(String src, String dest, String sigName, int page, int x, int y) {
    try {
        Image image = Image.getInstance(new File(currentPatient.getCurrentSign().getLocalFilePath()).toURL());;
        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);
        PdfStamper stamper = PdfStamper.createSignature(reader,os,'\0',new File("data/results/temp"),true);

        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setVisibleSignature(new Rectangle(x,y,x+80,y-60),page,sigName); 
        //Hide it!
        appearance.setReason("Nikita");
        appearance.setLocation("Sanitas");
        //No hide
        appearance.setImage(image);

        ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
        MakeSignature.signExternalContainer(appearance,external,8192);
        os.close();
        reader.close();
        stamper.close();
    } catch (Exception ex) {}
}

Solution

  • PdfSignatureAppearance has a setter setRenderingMode with a RenderingMode parameter. RenderingMode is an enumeration with these values:

    public enum RenderingMode {
        /**
         * The rendering mode is just the description.
         */
        DESCRIPTION,
        /**
         * The rendering mode is the name of the signer and the description.
         */
        NAME_AND_DESCRIPTION,
        /**
         * The rendering mode is an image and the description.
         */
        GRAPHIC_AND_DESCRIPTION,
        /**
         * The rendering mode is just an image.
         */
        GRAPHIC
    }
    

    To make invisible all the fields (reason, location) except the image, therefore, simply set the rendering mode to GRAPHIC:

    appearance.setRenderingMode(RenderingMode.GRAPHIC);