Gilberto Torrezan Gilberto Torrezan - 1 year ago 165
Java Question

Rotate a PdfSignatureAppearance with iText

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.setSignatureGraphic("my signature image", null));

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.


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download