简体   繁体   English

为什么使用 iText 7.1.11 在某些 PDF 文档中看不到数字签名?

[英]Why is the Digital Signature not visible in some PDF documents using iText 7.1.11?

My project digitally signs PDF documents using a digital certificate and displays the signature at the bottom left of the document.我的项目使用数字证书对 PDF 文档进行数字签名,并在文档的左下方显示签名。 It had always worked well until now, there are some documents that are digitally signed but it is not shown although it is recognized that the rectangle where it is visible is.直到现在它一直运行良好,有一些文件是数字签名的,但它没有显示,尽管它被认为是可见的矩形。 Could someone help with this, I leave a snippet that handles the digital signature.有人可以帮忙吗,我留下了一个处理数字签名的片段。 Here I leave a screenshot of how the digital signature is in a PDF document, I show it in the lower left corner of the document. 在这里我留下了数字签名在PDF文档中的截图,我在文档的左下角展示了它。

EXAMPLES例子

In this link I have shared example pdf documents of the problem and without it, I will detail them below:在此链接中,我分享了该问题的示例 pdf 文档,如果没有它,我将在下面详细说明:

  • ok_unsigned.pdf file: it is a document that when passing through my project is signed well, becoming the ok_signed.pdf file (this is the norm so far) ok_unsigned.pdf文件:它是一个文件,通过我的项目时签名良好,成为ok_signed.pdf文件(这是目前的规范)
  • ok_signed.pdf file: it is the digitally signed ok_unsigned.pdf file, it is a case of success ok_signed.pdf文件:是数字签名的ok_unsigned.pdf文件,成功案例
  • bad_unsigned.pdf file: it is a blank document that when digitally signed ( bad_signed.pdf ) the issue in question appears bad_unsigned.pdf文件:这是一个空白文档,当数字签名 ( bad_signed.pdf ) 出现问题时
  • bad_signed.pdf file: it is a document with the issue in question, digitally signed but without visually seeing the signature field. bad_signed.pdf文件:它是一个有问题的文档,经过数字签名,但没有直观地看到签名字段。

CODE代码

        try {
            BouncyCastleProvider providerBC = new BouncyCastleProvider();
            Security.addProvider(providerBC);

            KeyStore ks = KeyStore.getInstance("pkcs12");
            ks.load(new FileInputStream(keystore), password);
            String alias = ks.aliases().nextElement();
            Certificate[] chain = ks.getCertificateChain(alias);
            PrivateKey pk = (PrivateKey) ks.getKey(alias, password);

            PdfReader reader = new PdfReader(src);
            FileOutputStream fos = new FileOutputStream(new File(dest));
            PdfSigner signer = new PdfSigner(reader, fos, new StampingProperties());
            Rectangle rect = new Rectangle(10, 10, 150, 50);
            PdfSignatureAppearance appearance = signer.getSignatureAppearance();
            
            appearance.setPageRect(rect)
                    .setCertificate(chain[0])
                    .setReasonCaption("")
                    .setLocationCaption("")
                    .setSignatureCreator("SignerJAGC - iText 7.1.11")
                    .setPageNumber(1);
            signer.setFieldName("Banca en Línea - Envío de Documentos");
            signer.setSignDate(new GregorianCalendar());
            signer.setCertificationLevel(PdfSigner.CERTIFIED_NO_CHANGES_ALLOWED);

            IExternalDigest digest = new BouncyCastleDigest();
            IExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, providerBC.getName());

            signer.signDetached(digest, signature, chain, null, null, null, 0, SUBFILTER);
            System.out.println("SIGNED");
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }```

As @mkl said your coordinates might be outside of the page visible area as not all PDF pages have the bottom left corner at (0, 0).正如@mkl 所说,您的坐标可能在页面可见区域之外,因为并非所有 PDF 页面的左下角都位于 (0, 0)。

Try creating the signature rectangle like this:尝试像这样创建签名矩形:

Rectangle rect = new Rectangle(
    yourPageCropBoxLowerLeftX + 10, 
    yourPageCropBoxLowerLeftY + 10, 
    yourPageCropBoxLowerLeftX + 10 + yourSignatureWidth, 
    yourPageCropBoxLowerLeftY + 10 + yourSignatureHeight);

You just have to see how you can read the page's crop box coordinates, the lower left corner, as I'm not familiar with iText API.你只需要看看如何读取页面的裁剪框坐标,左下角,因为我不熟悉 iText API。

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

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