一个允许重用代码的工作示例com.itextpdf.text.pdf.PdfSignatureAppearance
(例如,自动计算字体大小):
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);
}
}
结果将是这样的:

要使用它,您只需按setVisibleSignatureRotated
原样复制方法并将调用替换为com.itextpdf.text.pdf.PdfSignatureAppearance#setVisibleSignature
调用setVisibleSignatureRotated
。