我正在尝试使用pdfbox库来签署PDF文件。
现在我遇到了问题,真的需要帮助。
这是我的代码:
这是我的代码:
private static void signPdf(PDDocument document) throws Exception
{
PDSignature sig = new PDSignature();
sig.setFilter(COSName.ADOBE_PPKLITE);
sig.setSubFilter(COSName.ADBE_PKCS7_DETACHED);
sig.setByteRange(new int[] {'a','a','a','a'});
sig.setContents(new byte[]{(byte) 23, (byte) 23, (byte) 23, (byte) 23});
SignatureOptions options = new SignatureOptions();
document.addSignature(sig, new SignatureInterface() {
public byte[] sign(InputStream content)
throws SignatureException, IOException {
//this should be made MD5 checksum?
return new byte[]{(byte) 'a', (byte) 'a', (byte) 'a', (byte) 'a'};
}
}, options);
}
然后我保存了我的pdf,但有两个问题: 1)我注意到sign方法从未被调用 2)我该将证书附加在哪里?是在sign方法中吗?
pdf:
/Type /Sig
/Filter /Adobe.PPKLite
/SubFilter /adbe.pkcs7.sha1
/Contents <0000000000. a lot of zeros..000>
/ByteRange [0 1000000000 1000000000 1000000000]
我觉得我可能漏掉了什么,但是文档没有提到如何给文件签名。
提前感谢JC。
@Ed
这是我保存pdf文件的方式:
public static void saveFile(COSDocument doc, String out)
throws IOException, COSVisitorException {
java.io.OutputStream os = null;
COSWriter writer = null;
try {
os = new java.io.FileOutputStream(out);
writer = new COSWriter(os);
writer.write(doc);
} finally {
if (os != null) {
os.close();
}
if (writer != null) {
writer.close();
}
}
}