iText7多重签名 [英] iText7 multiple signature

查看:741
本文介绍了iText7多重签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有iText7的pdf文档中做多个签名,但问题是当我曾经签名时,它有效。如果我签了两次,第一个签名无效。它看起来像这样:

I want to do multiple signatures in a pdf document with iText7 , but the problem is when I have once sign , it works . If I sign twice , the first signature is invalid . It looks like this :

未签名的PDF

签名PDF

这是我的代码:

@Test
public void testMutiSign() {

    iTextSignerUtil1.SignMultPDF(getBytes(unsignedPath), destPath1);

    iTextSignerUtil2.SignMultPDF(getBytes(destPath1), destPath2);

}


 IExternalSignatureContainer externalP7DetachSignatureContainer = new IExternalSignatureContainer() {
    @Override
    public byte[] sign(InputStream data) throws GeneralSecurityException {


        //byte[] hashData = HashUtil.hash(data , "SHA256");

        byte signData = null;
        signData = signUtil.signP7DetachData(data);

        return signData;

    }


    @Override
    public void modifySigningDictionary(PdfDictionary signDic) {
        signDic.put(PdfName.Filter, PdfName.Adobe_PPKLite);
        signDic.put(PdfName.SubFilter, PdfName.Adbe_pkcs7_detached);
    }
};


public void SignMultPDF(byte[] pdfFile , String destPath , String name , String fname , String value){

    boolean success = false;

    int estimatedSize = 300000;

    while (!success) {
        try {

            PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(pdfFile));
            PdfSigner pdfSigner = new PdfSigner(pdfReader, new FileOutputStream(destPath), true);

          pdfSigner.signExternalContainer(externalP7DetachSignatureContainer, estimatedSize);

            success = true;

        } catch (IOException e) {
            e.printStackTrace();
            estimatedSize += 1000;
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
}

以上是我的所有示例代码。

Above is all my sample code .

还有一个问题,我是否错过了创建签名字段?

And one more question , did I miss to create signature field ?

推荐答案

这个问题的原因是原始文件:它有一个空的间接字典,它既用作信息字典又用作轮廓字典。

The cause of this issue is a quirk of the original file: It has an empty indirect dictionary which it uses both as Info dictionary and as Outlines dictionaries.

每当操纵PDF iText更新其信息字典时,特别是 ModDate 很可能每次更改。

Whenever manipulating a PDF iText updates its Info dictionary, in particular the ModDate is likely to change each time.

因此,在每次签名过程中,信息字典都会更改,并且当字典对象被共享时,概述字典。但是,不允许更改已签名PDF的大纲。因此,不允许在创建第二个签名期间更改信息(和大纲),并使第一个签名无效。

Thus, during each signing pass the Info dictionary is changed, and as the dictionary object is shared, so is the Outlines dictionary. But changing the Outlines of an already signed PDF is not allowed. Thus, the Info (and Outlines) changes during the creation of the second signature are not allowed and invalidate the first signature.

此文件原始文件,唯一的区别是它为信息和<使用不同的空字典strong>大纲(因为文件中当前未使用的空间接字典,我只需要将信息大纲的引用更改为此未使用的对象)。两次签署此文件时,Adobe不会再抱怨。

This file is nearly the same as the original file, the only difference is that it uses different empty dictionaries for Info and Outlines (as there was a currently unused empty indirect dictionary in the file, I merely needed to change the reference of Info or Outlines to this unused object). When signing this file twice, Adobe does not complain anymore.

这是iText或PDF中的错误吗?我认为iText在更改 Info 字典时应该使用新的间接对象编号,因为在PDF中通常不禁止对字典进行重复使用。但是使用这种
重复间接对象使用的PDF确实是在寻找麻烦。所以我认为这个问题带来了iText问题和PDF中的问题。

Is this a bug in iText or in the PDF? I think iText should use a new indirect object number when changing the Info dictionary because such duplicate uses of dictionaries is not generally forbidden in PDFs. But a PDF making use of such duplicate indirect object uses is really asking for trouble. So I think this question brings both an iText issue and an issue in the PDF into the light.

这篇关于iText7多重签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆