Home > Software design >  itext5 sign PDF containing only image
itext5 sign PDF containing only image

Time:10-24

I have a code that successfully add visible signature block into a "normal" PDF.

<...>
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setVisibleSignature(new Rectangle(c[0], c[1], c[2], c[3]), 1, field);
createVisigbleSignature(stamper, appearance, signFont, signTxt, img);
<...>
public static void createVisigbleSignature(PdfStamper stamper, PdfSignatureAppearance appearance, Font font, String text, byte[] img) throws Exception {
        PdfTemplate layer2 = appearance.getLayer(2);
        float size = -1;
        final float MARGIN = 2;
        Rectangle dataRect = new Rectangle(MARGIN, MARGIN, appearance.getRect().getWidth() - MARGIN, appearance.getRect().getHeight() - MARGIN);
        Rectangle sr = new Rectangle(dataRect.getWidth(), dataRect.getHeight());
        size = ColumnText.fitText(font, text, sr, 12, appearance.getRunDirection());
        ColumnText ct = new ColumnText(layer2);
        ct.setRunDirection(appearance.getRunDirection());
        ct.setSimpleColumn(new Phrase(text, font), dataRect.getLeft(), dataRect.getBottom(), dataRect.getRight(), dataRect.getTop(), size, Element.ALIGN_LEFT);
        ct.go();
        //image
        Image image = Image.getInstance(img);
        layer2.addImage(image, appearance.getRect().getWidth(), 0, 0, appearance.getRect().getHeight(), 0, 0);
}

But if I try to sign PDF that contains only image (basically it is image exported as pdf), my visible signature block is no longer visible. Acrobate Reader sees the signature container, but user can't see or click the "visible" block.

What can be the reason for that and how to make sure that signature information is visible no matter what?

Here the examples:

screen shot

To fix this, therefore, simply use coordinates new Rectangle(c[0], c[1], c[2], c[3]) for your signature widget that are on-screen.

  • Related