签名后iText7 C#Check PDF被锁定 [英] iText7 C# Check PDF was locked after signature

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

问题描述

我也尝试使用iText7 .Net签名后检查PDF文件是否被锁定. 我们当前的版本是:7.1.8

Hi i was trying too check if PDF file was locked after signature using iText7 .Net. Our current version was : 7.1.8

目前,我正在尝试一段代码,但这对我的研究没有响应:

Currently i'm trying some piece off code but this doesn't respond to my research :

try
{
    //GET READER 
    PdfReader reader = new PdfReader(pdfModeleFile);
    if (reader != null)
    {
        //GET DOCUMENT 
        PdfDocument pdfDoc = new PdfDocument(reader);
        if (pdfDoc != null)
        {
            //GET FORM
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false);
            int signState = form.GetSignatureFlags();
            if (signState == 3)
            {
                //GET SIGNATURE
                SignatureUtil signatureUtil = new SignatureUtil(pdfDoc);
                List<string> signNamePdfAllField = signatureUtil.GetSignatureNames().ToList();
                List<string> signNamePdfBlankField = signatureUtil.GetBlankSignatureNames().ToList();
                SignaturePermissions perms = null;

                if ((signNamePdfAllField.Count() > 0 && signNamePdfBlankField.Count() == 0)){
                    //SIGNATURE WAS OK (ALL SIGNATURE WAS PUT)
                }
                else {
                    //ONE OR MORE SIGNATURE WAS MISING
                }
            }
            else
            {
                //SIGNATURE STATE OFF CURRENT PDF WAS NOT OK
            }
        }
        else
        {
            //PDF FILE HAVE NOT FORM
        }
    }
    else
    {
        //PDF FILE HAVE SOME PROBLEM
    }
}
catch (Exception e)
{
    //PDF FILE HAVE SOME PROBLEM
}

为便于说明,我正在搜索解决方案以获取此信息(例如在pdf adobe reader上):

For explain i'm search solution to get this information (like on pdf adobe reader) :

先谢谢您.

推荐答案

通过签名的 MDP (修改检测和预防)信息表示签名后的文档锁定. iText没有这些信息的显式吸气剂,但是您可以使用通用的低级吸气剂来访问它们.

Locking of the document after a signature is expressed by means of MDP (Modification Detection and Prevention) information of the signature. iText does not have explicit getters for these information but you can access them by using the generic, low level getters.

例如以下代码打印PdfDocument pdfDocument签名的MDP信息:

E.g. the following code prints the MDP information of the signatures of a PdfDocument pdfDocument:

SignatureUtil signatureUtil = new SignatureUtil(pdfDocument);
foreach (string name in signatureUtil.GetSignatureNames())
{
    Console.WriteLine("\nInspecting signature '{0}':", name);
    PdfDictionary dict = signatureUtil.GetSignatureDictionary(name);

    PdfArray referenceArray = dict.GetAsArray(PdfName.Reference);
    if (referenceArray == null | referenceArray.Size() == 0)
    {
        Console.WriteLine("The signature does not apply a transform.");
        continue;
    }

    foreach (PdfObject referenceArrayObject in referenceArray)
    {
        PdfObject referenceObject = referenceArrayObject;
        if (referenceObject.IsIndirectReference())
            referenceObject = ((PdfIndirectReference)referenceObject).GetRefersTo(true);
        if (referenceObject.IsIndirectReference())
        {
            Console.WriteLine("A transform is too deeply nested.");
            continue;
        }
        if (!referenceObject.IsDictionary())
        {
            Console.WriteLine("A transform is not a dictionary.");
            continue;
        }
        PdfDictionary reference = (PdfDictionary)referenceObject;

        PdfName method = reference.GetAsName(PdfName.TransformMethod);
        if (method == null)
        {
            Console.WriteLine("The signature does not provide the name of its transform method. (Invalid!)");
            continue;
        }
        if (new PdfName("UR").Equals(method))
        {
            Console.WriteLine("The signature is a usage rights signature.");
            continue;
        }
        if (PdfName.DocMDP.Equals(method))
        {
            Console.WriteLine("The signature has a DocMDP transform method, it is a certification signature.");
        }
        else if (PdfName.FieldMDP.Equals(method))
        {
            Console.WriteLine("The signature has a FieldMDP transform method.");
        }
        else
        {
            Console.WriteLine("The signature has the unknown '{0}' transform method. (Invalid!)", method);
            continue;
        }

        PdfDictionary transformParams = reference.GetAsDictionary(PdfName.TransformParams);
        if (transformParams == null)
        {
            Console.WriteLine("The transform has no parameters. (Invalid!)");
            continue;
        }

        PdfName action = transformParams.GetAsName(PdfName.Action);
        if (action != null)
        {
            if (PdfName.All.Equals(action))
            {
                Console.WriteLine("The transform locks all form fields.");
            }
            else
            {
                PdfArray fields = transformParams.GetAsArray(PdfName.Fields);
                if (PdfName.Include.Equals(action))
                {
                    if (fields == null)
                        Console.WriteLine("The transform locks all listed form fields but does not provide the list. (Invalid!)");
                    else
                        Console.WriteLine("The transform locks all the listed form fields: {0}", fields);
                }
                else if (PdfName.Exclude.Equals(action))
                {
                    if (fields == null)
                        Console.WriteLine("The transform locks all except listed form fields but does not provide the list. (Invalid!)");
                    else
                        Console.WriteLine("The transform locks all except the listed form fields: {0}", fields);
                }
                else
                {
                    Console.WriteLine("The transform uses the unknown action '{0}' for field locking. (Invalid!)", action);
                }
            }
        }

        PdfNumber p = transformParams.GetAsNumber(PdfName.P);
        if (p != null)
        {
            switch (p.IntValue())
            {
                case 1:
                    Console.WriteLine("The transform locks the document entirely.");
                    break;
                case 2:
                    Console.WriteLine("The transform restricts document manipulation to at most filling in forms, instantiating page templates, and signing.");
                    break;
                case 3:
                    Console.WriteLine("The transform restricts document manipulation to at most filling in forms, instantiating page templates, and signing, as well as annotation creation, deletion, and modification.");
                    break;
                default:
                    Console.WriteLine("The transform access permissions value is unknown: {0}. (Invalid!)", p.IntValue());
                    break;
            }
            Console.WriteLine("In a PAdES or PDF-2 context, addition of validation related information and proofs of existence is additionally allowed.");
        }
    }
}

您似乎对 P 1"The transform locks the document entirely."输出最感兴趣.

You appear to be mostly interested in the "The transform locks the document entirely." output for the P value 1.

有关背景,请研究PDF规范ISO 32000-1和ISO 32000-2以及对其的Adobe和ETSI扩展.

For backgrounds please study the PDF specifications ISO 32000-1 and ISO 32000-2 and the Adobe and ETSI extensions to it.

Java等效项可以在

The Java equivalent can be found in the CheckMdpTransformations test testShowMdpForStep4SignedByAliceBobCarolAndDave.

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

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