简体   繁体   English

使用 iText 的 Java PDF 数字签名可见,但不可打印

[英]Java PDF digital signature using iText visible, but not printable

I'm using iText's methods to sign a PDF with digital certificate, generating a signature visible in the document with PdfSignatureAppearance, but I'd like the visible signature not to come out in print.我正在使用 iText 的方法对带有数字证书的 PDF 进行签名,使用 PdfSignatureAppearance 生成在文档中可见的签名,但我不希望可见签名打印出来。 I saw that there is something similar in the PdfAnnotation class, where you can add a flag for this.我看到 PdfAnnotation 类中有类似的东西,您可以为此添加一个标志。 Is there any way to do this with the digital signature?有没有办法用数字签名来做到这一点? My code:我的代码:

PdfStamper stp = null;
try {
    PdfReader reader = new PdfReader(pdfInputFileName);

    stp = PdfStamper.createSignature(reader, fout, '\0');
    PdfSignatureAppearance sap = stp.getSignatureAppearance();

    sap.setCrypto(privateKey, certificateChain, null, PdfSignatureAppearance.WINCER_SIGNED);

    sap.setReason(reason);
    sap.setLocation(location);
    sap.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
    sap.setVisibleSignature(new Rectangle(30, 830, 170, 770), 1, null);

    stp.close();

} catch (DocumentException | IOException e) {

    logger.error("An unknown error accoured while signing the PDF file: " + e.getMessage());
}

This is the link to a PDF signed by this code, when I print it, the signature stamp always comes out in the print: https://s3.amazonaws.com/gxzadminlocal/anexo_28276.pdf这是此代码签名的 PDF 的链接,当我打印它时,打印时总是会出现签名图章: https : //s3.amazonaws.com/gxzadminlocal/anexo_28276.pdf

Current iText 5 versions automatically set the PRINT flag if the signature field does not yet exist and they have to create it.如果签名字段尚不存在并且他们必须创建它,则当前的 iText 5 版本会自动设置 PRINT 标志。 But if you fill an existing signature field, that flag is untouched.但是,如果您填写现有的签名字段,则该标志不会受到影响。 Thus, you might want to add an empty signature field without that PRINT flag in a first step and in a second step sign using that field, eg with the following code:因此,您可能希望在第一步中添加一个没有该 PRINT 标志的空签名字段,并在第二步中使用该字段进行签名,例如使用以下代码:

Preparing the PDF准备PDF

You can prepare a PDF with an empty signature field without setting the PRINT flag like this:您可以准备带有空签名字段的 PDF,而无需设置 PRINT 标志,如下所示:

try (   InputStream resource = SOURCE_STREAM;
        OutputStream os = INTERMEDIATE_OUTPUT_STREAM) {
    PdfReader reader = new PdfReader(resource);
    PdfStamper stamper = new PdfStamper(reader, os);
    PdfFormField field = PdfFormField.createSignature(stamper.getWriter());
    field.setFieldName("Signature");
    field.setWidget(new Rectangle(30, 830, 170, 770), PdfAnnotation.HIGHLIGHT_NONE);
    stamper.addAnnotation(field, 1);
    stamper.close();
}

( CreateSignature test signWidgetNoPrint pass 1) CreateSignature测试signWidgetNoPrint pass 1)

In particular you don't do特别是你不做

field.setFlags(PdfAnnotation.FLAGS_PRINT);

here!这里!

Signing the prepared PDF签署准备好的PDF

Having created that intermediate PDF, you can sign it like this:创建该中间 PDF 后,您可以像这样对其进行签名:

try (   InputStream resource = INTERMEDIATE_INPUT_STREAM;
        OutputStream os = RESULT_STREAM) {
    PdfReader reader = new PdfReader(resource);
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');

    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setReason("reason");
    appearance.setLocation("location");
    appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
    appearance.setVisibleSignature("Signature");

    ExternalSignature pks = new PrivateKeySignature(pk, "SHA512", "BC");
    ExternalDigest digest = new BouncyCastleDigest();
    MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS);
}

( CreateSignature test signWidgetNoPrint pass 2) CreateSignature测试signWidgetNoPrint pass 2)

assuming you have prepared your private key in pk and your certificate chain in chain ;假设你已经准备好了你的私钥pk和你的证书链chain and assuming you have registered Bouncy Castle as security provider.并假设您已将 Bouncy Castle 注册为安全提供商。

In the result PDF the signature visualization appears on screen but not in print.在结果 PDF 中,签名可视化显示在屏幕上但未打印。

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

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