gpt4 book ai didi

java - 使用 iText 旋转 PdfSignatureAppearance

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:16 25 4
gpt4 key购买 nike

我在尝试在 iText 中旋转 PdfSignatureAppearance(例如 90 度)时遇到了一个大问题。我正在使用 MakeSignature.signDetached 方法签署 PDF,并为外观设置我自己的文本和图像。

这是一些代码:

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

我的问题是“旋转”参数。无论我设置什么,文本和图像都不会旋转。查看 iText 代码(我使用的是 iText 5.3.2),签名层边界框的旋转参数被丢弃,所以,以这种方式设置旋转根本没有效果。

现在的问题是:有没有一种方法可以在不重写整个 PdfSignatureAppearance 和 MakeSignature 类的情况下旋转我的签名层?

澄清一下:对文档进行数字签名的代码工作正常。我唯一的问题是签名的可视层:我无法旋转它。

谢谢。

最佳答案

允许重用 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);
}
}

如果结果是这样的:

rotated visual signature

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

关于java - 使用 iText 旋转 PdfSignatureAppearance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com