pdfcertificateitextdigital-signaturepdfstamper

How to manage a PDF digital signature's display?


I'm using iText Library to sign my PDF documents.
I want to know if there is any method in PdfStamper that could manage the display of the appearance : signature information in one layer, and the image in one other layer, which are now in the same layer:

Solution :

            PdfStamper pdfStamper;
            pdfStamper = PdfStamper.createSignature(pdfReader, null, '\0',
                        outputFile);

            // appearance
            PdfSignatureAppearance appearance = pdfStamper
                        .getSignatureAppearance();
            appearance .setReason("Test");
            appearance .setLocation("Location");                                                                                                                                 
            appearance .setSignatureGraphic(Image.getInstance(RESOURCE)); 
            appearance.setRenderingMode(RenderingMode.GRAPHIC_AND_DESCRIPTION); appearance.setVisibleSignature(new Rectangle(420, 732, 512, 780), 1,"sign");

Refering to this interesting book , I can now create two areas for description and graphics.


Solution

  • Please read Digital Signatures for PDF documents, section 2.4 "Creating different signature appearances." You'll learn about the layers n0 to n4, and discover that using layers n1, n3 and n4 is no longer recommended since 2003 (in other words: don't use them).

    You can use the n0 layer, known as the background layer like this:

    PdfTemplate n0 = appearance.getLayer(0);
    float x = n0.getBoundingBox().getLeft();
    float y = n0.getBoundingBox().getBottom();
    float width = n0.getBoundingBox().getWidth();
    float height = n0.getBoundingBox().getHeight();
    n0.setColorFill(BaseColor.LIGHT_GRAY);
    n0.rectangle(x, y, width, height);
    n0.fill();
    

    This is just a simple example from the book, drawing a light gray rectangle.

    You can use the n2 layer like this:

    PdfTemplate n2 = appearance.getLayer(2);
    ColumnText ct = new ColumnText(n2);
    ct.setSimpleColumn(n2.getBoundingBox());
    Paragraph p = new Paragraph("This document was signed by Bruno Specimen.");
    ct.addElement(p);
    ct.go();
    

    If "drawing text" is too difficult, you can also use convenience methods such as:

    appearance.setLayer2Text("This document was signed by Bruno Specimen");
    appearance.setLayer2Font(new Font(FontFamily.TIMES_ROMAN));
    

    The examples from the book are available on SourceForge.

    Using the above methods, is creating the appearance "the hard way". There are also different ways to let iText do the work:

    Screen shot digital signatures book