How to hide Validity unknown symbol after signing the PDF in iTextSharp

我的未来我决定 提交于 2020-06-29 04:13:08

问题


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.

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:

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".


回答1:


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.

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);
        }
    }
}

with this helper method:

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:

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

  • Acrobat 9.5 (German locale) not trusting your issuer

  • Acrobat DC trusting your issuer

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.



来源:https://stackoverflow.com/questions/62187593/how-to-hide-validity-unknown-symbol-after-signing-the-pdf-in-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!