问题描述
我正在尝试使用Aspose pdf java对文档进行数字签名。这是我的代码
public ByteArrayOutputStream signDocument(Document doc,String signedBy) throws Exception {
PdfFileSignature pdfSignSingle = new PdfFileSignature();
pdfSignSingle.bindPdf(doc);
pdfSignSingle.setCertificate(prop.getSigningKeyStorePath(),prop.getKeystorePassword());
PKCS7 signature = new PKCS7(prop.getSigningKeyStorePath(),prop.getKeystorePassword());
pdfSignSingle.setSignatureAppearance(prop.getSimploudlogo());
signature.setAuthority("Authority");
signature.setDate(new Date());
signature.setContactInfo("email");
signature.setLocation("Location");
signature.setReason("reason");
pdfSignSingle.sign(1,true,new java.awt.Rectangle(100,100,200,200),signature);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfSignSingle.save(baos);
pdfSignSingle.dispose();
doc.dispose();
return baos;
}
在picture中显示了签名在adobeReader中的外观。
如您所见,图像和授权均未显示。我试过将图片同时设为pdf和png格式。我还尝试过使其比矩形区域更小。至于权限,我真的需要它可以自定义,以便图片中第一行中的文本可以 由“ customParameter”签名
解决方法
API提供了另一个类,即SignatureCustomAppearance
,该类可以进一步用于设置签名的此类属性,例如DateSigned,Reason,Location等。请检查以下符合您要求的完整代码段: / p>
String inputFile = "doc.pdf";
String outSignedFile = "out_20.9.pdf";
// Create PdfFileSignature instance
com.aspose.pdf.facades.PdfFileSignature pdfSignSingle = new com.aspose.pdf.facades.PdfFileSignature();
// Bind the source PDF by reading contents of Stream
pdfSignSingle.bindPdf(inputFile);
PKCS7 pkcs = new PKCS7("mykey2.pfx","pass");
pkcs.setAuthority("Authority");
pkcs.setDate(new Date());
pkcs.setContactInfo("email");
pkcs.setLocation("Location");
pkcs.setReason("reason");
pkcs.setImage(new FileInputStream("simpleLogo.png"));
SignatureCustomAppearance sca = new SignatureCustomAppearance();
sca.setDateSignedAtLabel(null);
sca.setDigitalSignedLabel(null);
sca.setShowReason(true);
sca.setShowLocation(true);
sca.setShowContactInfo(true);
pkcs.setCustomAppearance(sca);
pdfSignSingle.sign(1,true,new java.awt.Rectangle(100,100,200,200),pkcs);
// Set image for signature appearance
//pdfSignSingle.setSignatureAppearance("simpleLogo.png");
// Save final output
pdfSignSingle.save(outSignedFile);
如问题下方的评论中所述,同一询问也发布在Aspose.PDF官方论坛的“ Aspose pdf java PdfFileSignature setAuthority not working”上,并且在那里也提供了解决方案。