我正在使用 Aspose Pdf java sdk 签署 pdf。对于任何库,前两个步骤可能都是相同的。
如果您还没有证书,请使用 openssl 生成一个 ssl 私钥。显然这会发出警告,因为它没有由 CA 签名。
openssl req -x509 -newkey rsa:2048 -keyout testkey.pem -out testcert.pem -days 365
接下来将您的密钥转换为 pkcs12 格式的证书。
openssl pkcs12 -name test -export -in testcert.pem -inkey testkey.pem -out test.pfx
最后使用 Aspose Pdf 或其他库对 pdf 进行签名
PdfFileSignature pdfSignSingle = new PdfFileSignature();
//Open the pdf
pdfSignSingle.bindPdf(pdfFileInputStream);
// Load the certificate using the com.aspose.pdf.PKCS1 object
PKCS1 pkcs1 = new PKCS1(certificateFileInputStream, certificatePassword);
pkcs1.setAuthority("Sample Person");
pkcs1.setReason("I want to sign");
pkcs1.setContactInfo("some contact info");
pkcs1.setLocation("here");
pkcs1.setShowProperties(false);
// Apply the signature. (0,0) is in the lower left corner.
pdfSignSingle.sign(1, true, new Rectangle(100, 100, 150, 50), pkcs1);
// Apply the signature image
pdfSignSingle.setSignatureAppearanceStream(imageFileInputStream);
pdfSignSingle.save(pdfFileOutputStream);