如何使用pdfbox在Java中签名pdf [英] How to sign pdf in Java using pdfbox
本文介绍了如何使用pdfbox在Java中签名pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用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
返回新字节[] {(byte)'a',(byte)'a',(byte)'a',(byte)'a'};
}
},options);
}
然后Iam保存我的pdf,但:
1)注意,sign方法永远不会调用
2)我应该在哪里附加certyficate?符号方法?
pdf:
/ Type / Sig
/ Filter /Adobe.PPKLite
/ SubFilter /adbe.pkcs7.sha1
/ Contents< 0000000000。很多zeros..000>
/ ByteRange [0 1000000000 1000000000 1000000000]
我认为我错过了一些东西,
@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();
}
}
}
解决方案>
链接的 PDFBox-SignExample.zip 已过期。请改用此示例:
更好地记录并保持最新。
I am trying to sign pdf using pdfbox libraries. I have stuck now and realy need a help.
This is my code:
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);
}
Then Iam saving my pdf, but: 1) I have noticed that sign method is never called 2) Where should I attach certyficate? in sign method?
pdf:
/Type /Sig
/Filter /Adobe.PPKLite
/SubFilter /adbe.pkcs7.sha1
/Contents <0000000000. a lot of zeros..000>
/ByteRange [0 1000000000 1000000000 1000000000]
I think that i miss something, but documentation says nothing about how to sign a file.
Tahnks in advance JC.
@Ed
Here is how I save my 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();
}
}
}
解决方案
The linked PDFBox-SignExample.zip is out of date. Please use this sample instead:
It is better documented and kept up-to-date.
这篇关于如何使用pdfbox在Java中签名pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文