在iTextSharp中对PDF签名后如何隐藏“有效性未知”符号 [英] How to hide Validity unknown symbol after signing the PDF in iTextSharp

查看:399
本文介绍了在iTextSharp中对PDF签名后如何隐藏“有效性未知”符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C#.Net中使用iTextSharp创建了签名的PDF。
在签名的PDF中,我希望有一个有效性符号,以便当用户在Adobe Reader中打开它时,它会显示一个绿色的勾号及其签名。

I have created a signed PDF using iTextSharp in C# .Net. In the signed PDF I want to have a validity symbol so that when a user opens it in Adobe Reader it shows a green tick mark along with its signature.

但是在我的Web应用程序(带画布的html页面)中,我想从PDF中删除该问号,以使其不会在此屏幕中显示:

But in my web application (a html page with canvas) I want to remove that question mark from the PDF so that it does not show like in this screen:

所以我想保留其中 signatureappearance.Acro6Layers = false; 的PDF的原始字节。代码以获取此符号。但是在将它显示在查看器(带有画布的html页面)中之前,我想修改字节并删除该黄色标记,以使其不显示签名未验证。

So I want to keep the original bytes of the PDF in which signatureappearance.Acro6Layers = false; is added in code to get this symbol. But before showing it in my viewer (html page with canvas) I want to modify bytes and remove this yellow mark, so that it does not show "Signature Not Verified".

推荐答案

我没有使用Aspose PDF到图像的渲染经验,但是看起来它可能只是简单地渲染了PDF中的签名外观。

I have no experiences with the Aspose PDF-to-image rendering, but it looks like it probably simply renders the signature appearance as it is in the PDF. This, by the way, would be the correct thing to do.

由于Acrobat 6之前的所有额外层都已绘制在已保存文件的签名外观中,因此您必须清除它们。您可以这样操作:

As the extra layers from before Acrobat 6 are all drawn in the signature appearance in the saved file, you have to clear them. You can do that like this:

using (PdfReader pdfReader = new PdfReader(source))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(dest, FileMode.Create, FileAccess.Write), '\0', true))
{
    AcroFields fields = pdfStamper.AcroFields;
    List<string> names = fields.GetSignatureNames();
    foreach (string name in names)
    {
        PdfDictionary normal = PdfReader.GetPdfObject(fields.GetNormalAppearance(name)) as PdfDictionary;
        PdfDictionary frm = normal?.GetAsDict(PdfName.RESOURCES)?.GetAsDict(PdfName.XOBJECT)?.GetAsStream(PdfName.FRM);
        PdfDictionary frmResources = frm?.GetAsDict(PdfName.RESOURCES);
        PdfDictionary frmXobjectResources = frmResources?.GetAsDict(PdfName.XOBJECT);
        if (frmXobjectResources != null)
        {
            Console.WriteLine("Found XObject resources of FRM XObject");
            clearLayer(pdfStamper.Writer, frmXobjectResources, PdfName.N1);
            clearLayer(pdfStamper.Writer, frmXobjectResources, PdfName.N3);
            clearLayer(pdfStamper.Writer, frmXobjectResources, PdfName.N4);
            pdfStamper.MarkUsed(frmXobjectResources);
            pdfStamper.MarkUsed(frmResources);
            pdfStamper.MarkUsed(frm);
        }
    }
}

使用以下辅助方法:

void clearLayer(PdfWriter writer, PdfDictionary frmXobjectResources, PdfName layerName)
{
    PdfStream existingLayer = frmXobjectResources.GetAsStream(layerName);
    if (existingLayer != null)
    {
        PdfArray bBox = existingLayer.GetAsArray(PdfName.BBOX);
        PdfTemplate newLayer = PdfTemplate.CreateTemplate(writer, 0, 0);
        newLayer.BoundingBox = PdfReader.GetNormalizedRectangle(bBox);
        frmXobjectResources.Put(layerName, newLayer.IndirectReference);
    }
}

在不同的渲染器中,原始示例文档的签名外观上面的代码生成的文档如下所示:

In different renderers the signature appearance of your original example document and the document resulting from the above code appear as follows:


  • 按原样渲染器(我使用的是Chrome):

  • an "as is" renderer (I used Chrome):

Acrobat 9.5(德语区域设置)不信任您的发行人

Acrobat 9.5 (German locale) not trusting your issuer

Acrobat DC信任您的发行人

Acrobat DC trusting your issuer

不过要提一个警告:对于带有认证签名的文档,不仅是批准签名,尤其是带有不允许更改的认证签名的文档,Acrobat很可能不会喜欢结果。

A word of warning, though: In case of documents with certification signatures, not merely approval signatures, in particular with certification signatures with no changes allowed, Acrobat most likely will not like the result.

这篇关于在iTextSharp中对PDF签名后如何隐藏“有效性未知”符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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