简体   繁体   中英

itext5 sign PDF containing only image

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:

https://drive.google.com/drive/folders/1hnROu5UVXECi-hy9FY5ZXJLDK_jdwjch?usp=sharing normal.pdf and photo.pdf are the files before signing. normal_pre.pdf and photo.pre.pdf are pre-signed.

The sign will be seen as "broken". It is normal as the pdfs contain only container and not the signature itself.

The problem with your example PDF with images only is that you positioned the signature off-page.

In detail : That PDF has a single page with this crop box:

/CropBox [ 0.0 0.0 841.5 594.75 ] 

Ie its lower left corner is the coordinate system origin (0, 0) and its upper right corner is (841.5, 594.75).

Your signature on that page, though, is at

/Rect[10 810 130 840]

Ie between (10, 810) and (130, 840). These coordinates clearly are above the crop box.

If you set Adobe Reader to show the pages continuously and zoom out a bit, you can actually see the annotation:

截屏

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM