gpt4 book ai didi

java - com.itextpdf.text.exceptions.InvalidPdfException : PDF header signature not found

转载 作者:行者123 更新时间:2023-12-01 14:51:12 31 4
gpt4 key购买 nike

我在尝试对 PDF 文档进行数字签名时遇到此错误。传递两个 pdf SOURCEPDF 名称和 DESTINATIONPDF 名称(数字签名)pdf 名称。在 SOURCEPDF 上进行第一次数字签名后,我得到了 DESTINATIONPDF 。对于第二次数字签名,我使用 DESTINATIONPDF 作为源 pdf 和目标 pdf。

这是我的代码

try
{
for(int i=1;i<=signature_Count;i++)
{
if(i==1)
{
tmpPdfSource=sourcePdfPath;
}else{
this.tmpPdfSource=destinationPdfPath;
}

int pageNo=Integer.parseInt(ad.readXML(xmlString, rootName,"PageNo-"+i));
String imageSource=ad.readXML(xmlString, rootName,"ImageSource-"+i);
float llx=Float.parseFloat(ad.readXML(xmlString, rootName,"llx-"+i));
float lly=Float.parseFloat(ad.readXML(xmlString, rootName,"lly-"+i));
float urx=Float.parseFloat(ad.readXML(xmlString, rootName,"urx-"+i));
float ury=Float.parseFloat(ad.readXML(xmlString, rootName,"ury-"+i));
String signature=ad.readXML(xmlString, rootName,"SignatureName-"+i);

File dest = new File(destinationPdfPath);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new Fil eInputStream(certificatePath), keystore_password.toCharArray());
String alias = (String) ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias,key_password.toCharArray());
java.security.cert.Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader(tmpPdfSource);
stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true);
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();

appearance.setCrypto(pk, chain, null,PdfSignatureAppearance.SELF_SIGNED);

if (true)
{
appearance.setAcro6Layers(true);
Image img=Image.getInstance(imageSource);
appearance.setImage(img);
appearance.setVisibleSignature(new com.itextpdf.text.Rectangle(llx, lly, urx, ury), pageNo, signature);
}
}//for
stamper.close();
} catch (Exception e) {
GenericLog gl=new GenericLog();
gl.writeWarning("Error Occured in SignPdfDocument ");
gl.writeError(e);
e.printStackTrace();
}

请帮助我修复此错误。

最佳答案

重新格式化代码以使其可读后,问题就出现了:

for(int i=1;i<=signature_Count;i++)
{
if(i==1)
{
tmpPdfSource=sourcePdfPath;
}else{
this.tmpPdfSource=destinationPdfPath;
}
[...]
File dest = new File(destinationPdfPath);
[...]
PdfReader reader = new PdfReader(tmpPdfSource);
stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true);
[...]
}//for
stamper.close();

从第二次迭代开始,您读取了上一次迭代中由PdfStamper生成的文件,您必须在迭代结束时关闭该stamper,而不是在for循环之外:

    stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true);
[...]
stamper.close();
}//for

此外,您最好将new FileOutputStream(dest)放入一个变量中,并在关闭stamper:后立即显式关闭它:

    FileOutputStream fout = new FileOutputStream(dest);
stamper = PdfStamper.createSignature(reader, fout, '\0', null, true);
[...]
stamper.close();
fout.close();
}//for

当然,请遵循 Bruno 的建议,阅读他的 PDF 签名白皮书,并更新您的签名创建代码以生成未弃用类型的签名。

关于java - com.itextpdf.text.exceptions.InvalidPdfException : PDF header signature not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14831418/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com