Search code examples
javapdfitextsign

itext5 sign image over context


When I sign a document and I use an image in the signature appearance, I get this result:

enter image description here

However, I want the result to be like this:

enter image description here

This is my code:

PdfSignatureAppearance appearance =  stamper.getSignatureAppearance();
appearance.setReason(info.getReason());
appearance.setLocation(info.getLocation());
appearance.setVisibleSignature(t.getRectangle(), 1, "SIGN");
appearance.setSignatureGraphic(image); 
appearance.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED); 
appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
MakeSignature.signDetached(appearance, digest, signature,chain, null, null, null, 0, MakeSignature.CryptoStandard.CADES);

Solution

  • The visual representation of a digital signature in a PDF document is a widget annotation. A widget annotation is a specific type of annotation.

    In PDF, you have the actual content of a page. This content is stored in a content stream. A page is defined in a page dictionary. There's a /Contents entry in this page dictionary that refers to one of more content streams. Together these content streams contain the syntax that describes the content of a page.

    Annotations are not part of any of those streams. Annotations are referred to from the page dictionary using the /Annots entry. In your case, there is an /Annots entry that refers to a widget annotation with the appearance of your signature.

    All annotations are rendered on top of the actual page content.

    To make a long story short: you have a PDF with some page content and the appearance of an annotation is added on top of that page content. This is normal. This is as described in ISO-32000-1. You are now asking for a PDF viewer to render annotations under the page content. This is not possible according to the PDF specification.

    The short answer to your question is: you are asking something that can't be done. The closest you'll get to a solution is to make the image transparent (but that's another question).