繁体   English   中英

com.itextpdf.text.exceptions.InvalidPdfException:未找到PDF标头签名

[英]com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found

尝试对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:后立即将其显式关闭stamper:

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

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

我看到了setCrypto()方法,这意味着您没有使用最新版本的iText。 我还看到选项PdfSignatureAppearance.SELF_SIGNED ,这意味着您正在创建的签名不再符合当今的标准。

您能帮个忙,阅读文档吗?

另外:您正在使用与源和目标相同的文件吗? 这不可能。 您至少需要创建一个临时文件或在内存中创建该文件,然后覆盖现有文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM