Question

I'm with a big problem trying to rotate a PdfSignatureAppearance in iText (90 degrees, for instance). I'm signing a PDF using the MakeSignature.signDetached method, and setting my own text and image for the appearance.

Here is some code:

PdfReader reader = new PdfReader("my input file");
FileOutputStream fout = new FileOutputStream("my output file");

PdfStamper stamper = PdfStamper.createSignature(reader, fout, '\0');
PdfSignatureAppearance sap = stamper.getSignatureAppearance();

sap.setLayer2Text("Signed by someone");
sap.setAcro6Layers(true);
sap.setSignatureGraphic("my signature image", null));
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION);

Rectangle pageSize = reader.getPageSize(1); //the page to sign: 1 is the 1st one

Rectangle rect = new Rectangle(llx, lly, urx, ury, rotation);
//llx, lly ... come from a GUI. They are working fine, but the rotation is not considered

sap.setVisibleSignature(rect, 1, null); //1 is the page to sign

MakeSignature.signDetached(sap, ...); //sign the document

My problem is the "rotation" argument. No matter what I set, the text and the image never rotate. Looking at iText code (I'm using iText 5.3.2), the rotation argument of the bounding box of the signature layer is discarded, so, well, setting the rotation this way have no effect at all.

Now the question: Is there a way to rotate my signature layer without rewriting the entire PdfSignatureAppearance and MakeSignature classes?

Just to clarify: the code that digitally signs the document is working fine. My only problem is with the visual layer of the signature: I can't rotate it.

Thanks.

Was it helpful?

Solution

Currently setting the rotation isn't supported when creating a signature using convenience methods such as setRenderingMode(), setLayer2Text(), setSignatureGraphic(), etc...

So you have two options: 1. Either ask us to provide that functionality. The draft of the first 90 pages of the white paper on digital signatures has just been released to the subscribers of the news letter, so we are working on these classes. However: signing with smart cards, refactoring the verification process, etc... has absolute priority for the moment, so you may have to wait for a while. 2. Draw the content in any direction you want by using the getLayer() method. That would be getLayer(0) for the image in the background; and getLayer(2) for the text.

Note that there used to be layers 1, 3, 4 too, but they will only work if acro6Layers equals true; this is the case for you, but the use of acro6Layers is discouraged (it has become obsolete: you shouldn't use it anymore). As a matter of fact, I'll deprecate that method right now and set the value to false by default.

OTHER TIPS

A working example that allows for reuse of the code in com.itextpdf.text.pdf.PdfSignatureAppearance (e.g. to calculate font size automatically):

public class RotateVisualSignature {
    public static void main(String[] args) throws IOException, DocumentException, GeneralSecurityException {
        // Loading private key and certificates.
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(new FileInputStream("signer.p12"), "secret".toCharArray());
        String alias = keyStore.aliases().nextElement();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, "secret".toCharArray());
        Certificate[] certificateChain = keyStore.getCertificateChain(alias);

        PdfReader reader = new PdfReader("sample.pdf");
        FileOutputStream os = new FileOutputStream("sample-signed.pdf");
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setCertificate(certificateChain[0]);

        // This method has to be called as an alternative to 'com.itextpdf.text.pdf.PdfSignatureAppearance.setVisibleSignature'.
        setVisibleSignatureRotated(stamper, appearance, new Rectangle(120, 650, 170, 770), 1, null);

        // Perform the signature.
        ExternalSignature externalSignature = new PrivateKeySignature(privateKey, "SHA-256", null);
        ExternalDigest externalDigest = new BouncyCastleDigest();
        MakeSignature.signDetached(appearance, externalDigest, externalSignature, certificateChain, null, null, null, 0, MakeSignature.CryptoStandard.CMS);
    }

    private static void setVisibleSignatureRotated(PdfStamper stamper, PdfSignatureAppearance appearance, Rectangle pageRect, int page, String fieldName) throws DocumentException, IOException {
        float height = pageRect.getHeight();
        float width = pageRect.getWidth();
        float llx = pageRect.getLeft();
        float lly = pageRect.getBottom();
        // Visual signature is configured as if it were going to be a regular horizontal visual signature.
        appearance.setVisibleSignature(new Rectangle(llx, lly, llx + height, lly + width), page, null);
        // We trigger premature appearance creation, so independent parts of it can be modified right away.
        appearance.getAppearance();
        // Now we correct the width and height.
        appearance.setVisibleSignature(new Rectangle(llx, lly, llx + width, lly + height), page, fieldName);
        appearance.getTopLayer().setWidth(width);
        appearance.getTopLayer().setHeight(height);
        PdfTemplate n2Layer = appearance.getLayer(2);
        n2Layer.setWidth(width);
        n2Layer.setHeight(height);
        // Then we rotate the n2 layer. See http://developers.itextpdf.com/question/how-rotate-paragraph.
        PdfTemplate t = PdfTemplate.createTemplate(stamper.getWriter(), height, width);
        ByteBuffer internalBuffer = t.getInternalBuffer();
        internalBuffer.write(n2Layer.toString().getBytes());
        n2Layer.reset();
        Image textImg = Image.getInstance(t);
        textImg.setInterpolation(true);
        textImg.scaleAbsolute(height, width);
        textImg.setRotationDegrees((float) 90);
        textImg.setAbsolutePosition(0, 0);
        n2Layer.addImage(textImg);
    }
}

Where the result would be something like this:

rotated visual signature

For using this, you just need to copy the setVisibleSignatureRotated method as is and replace calls to com.itextpdf.text.pdf.PdfSignatureAppearance#setVisibleSignature with calls to setVisibleSignatureRotated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top