在我的应用程序中,我可以选择签署pdf文件。 pdf签名有两种选择,一种是创建新签名,另一种是我需要签署空签名字段。我做了创建一个新签名字段的部分,它工作正常,现在我遇到了签署空签名字段的问题。这是我的代码
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null) ;
//ovo smo ubacili
Enumeration en = ks.aliases();
// String alias = (String)en.nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "password".toCharArray());
java.security.cert.Certificate[] chain = ks.getCertificateChain(alias);
//location of pdf document to sign
PdfReader reader = new PdfReader(jTextField1.getText());
String [] delovi=jTextField1.getText().split("\\\\");
String potisaniFajl=delovi[delovi.length-1];
new File(System.getProperty("user.home") + "\\Desktop\\Potpisani Fajlovi\\").mkdirs();
//signed pdf location
FileOutputStream fout = new FileOutputStream(System.getProperty("user.home") + "\\Desktop\\Potpisani Fajlovi\\"+potisaniFajl);
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0',null,true);
PdfSignatureAppearance appearance = stp.getSignatureAppearance();
appearance.setCrypto(null, chain, null, PdfSignatureAppearance.SELF_SIGNED);
//appearance.setCrypto(key, chain, null,PdfSignatureAppearance.WINCER_SIGNED);
//appearance.setCrypto(null, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
appearance.setReason("Potpis kompenzacije");
appearance.setLocation("Foobar");
//lokacija potpisa
appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, "dva");
appearance.setExternalDigest(new byte[128], null, "RSA");
appearance.preClose();
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(key);
byte buf[] = new byte[8192];
int n;
InputStream inp = appearance.getRangeStream();
while ((n = inp.read(buf)) > 0) {
signature.update(buf, 0, n);
}
PdfPKCS7 sig = appearance.getSigStandard().getSigner();
sig.setExternalDigest(signature.sign(), null, "RSA");
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.CONTENTS,new PdfString(sig.getEncodedPKCS1()).setHexWriting(true));
appearance.close(dic);
此代码添加了一个新签名,我需要做哪些更改才能签署空签名字段名称“GoodSignature”
答案 0 :(得分:2)
我需要做些什么更改来签名空签名字段名称“GoodSignature”
在您当前的代码中,您调用PdfSignatureAppearance.setVisibleSignature
的重载来创建新的签名字段:
appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, "dva");
将其记录为(JavaDocs):
/**
* Sets the signature to be visible. It creates a new visible signature field.
* @param pageRect the position and dimension of the field in the page
* @param page the page to place the field. The fist page is 1
* @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
*/
public void setVisibleSignature(Rectangle pageRect, int page, String fieldName)
对于您的新任务,您必须使用此重载:
/**
* Sets the signature to be visible. An empty signature field with the same name must already exist.
* @param fieldName the existing empty signature field name
*/
public void setVisibleSignature(String fieldName)
在您的情况下,电话会是:
appearance.setVisibleSignature("GoodSignature");
话虽如此,我建议您阅读Digital Signatures for PDF documents, Bruno Lowagie的白皮书(iText软件)。您的代码似乎使用了许多过时的技术,您应该更新