Advertisement
kijato

PDF_SignaturePermissions

Jan 16th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using iText.Kernel.Pdf;
  3. using iText.Signatures;
  4.  
  5. namespace pdf_isClosed
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             if (args.Length > 0)
  12.             {
  13.                 foreach (String SRC in args)
  14.                 {
  15.                     getCertificationLevel(SRC);
  16.                 }
  17.             }
  18.             else {
  19.                 getCertificationLevel(@"tryIt.pdf");
  20.             }
  21.         }
  22.  
  23.         static void getCertificationLevel( String SRC )
  24.         {
  25.             Console.WriteLine(SRC+Environment.NewLine);
  26.  
  27.             PdfReader reader = new PdfReader( SRC );
  28.             PdfDocument doc = new PdfDocument( reader );
  29.             SignatureUtil signUtil = new SignatureUtil(doc);
  30.  
  31.             if (signUtil.GetSignatureNames().Count == 0)
  32.             {
  33.                 Console.WriteLine("There is no signature here.");
  34.             }
  35.             else
  36.             {
  37.                 foreach (var sigName in signUtil.GetSignatureNames())
  38.                 {
  39.                     PdfSignature signature = signUtil.GetSignature(sigName);
  40.  
  41.                     PdfDictionary dict = signUtil.GetSignatureDictionary(sigName); // https://stackoverflow.com/questions/58397100/itext7-c-sharp-check-pdf-was-locked-after-signature/58418033#58418033
  42.  
  43.                     SignaturePermissions perms = new SignaturePermissions(dict, null); // https://kb.itextpdf.com/itext/digital-signatures-chapter-5#Digitalsignatures-chapter5-c5_02_signatureinfo
  44.  
  45.                     Console.Write("\t" + sigName + "\n" +
  46.                                   "\t" + signature.GetDate() + "\n" +
  47.                                   "\t\t" + "IsCertification:      " + perms.IsCertification() + "\n" +
  48.                                   "\t\t" + "IsFillInAllowed:      " + perms.IsFillInAllowed() + "\n" +
  49.                                   "\t\t" + "IsAnnotationsAllowed: " + perms.IsAnnotationsAllowed() + "\n" +
  50.                                   "\t\t" + "GetFieldLocks:        "
  51.                                  );
  52.                     if ( perms.GetFieldLocks().Count != 0 )
  53.                         foreach (SignaturePermissions.FieldLock Lock in perms.GetFieldLocks()) { Console.Write( Lock + " " ); }
  54.                     else
  55.                         Console.Write( "-" );
  56.                     Console.WriteLine("\n");
  57.  
  58.  
  59.                     /*
  60.                     //PdfSigner signer = new PdfSigner(doc); // 'PdfSigner' does not contain a constructor that takes 1 arguments
  61.                     //var signerProperties = signer.GetSignerProperties(); // what creates a connection between PdfReader, PdfDocument, PdfSigner, or SignerProperties...???
  62.  
  63.                     SignerProperties sp = new SignerProperties();
  64.  
  65.                     switch ( sp.GetCertificationLevel() ) // https://api.itextpdf.com/iText/dotnet/9.0.0/namespacei_text_1_1_signatures.html#a996a04f7ae83f1a18712632be0460318
  66.                     {
  67.                         case AccessPermissions.UNSPECIFIED: // 0
  68.                             Console.WriteLine("Unspecified access permissions value which makes signature 'approval' rather than 'certification'.");
  69.                             break;
  70.                         case AccessPermissions.NO_CHANGES_PERMITTED: // 1
  71.                             Console.WriteLine("Access permissions level 1 which indicates that no changes are permitted except for DSS and DTS creation.");
  72.                             break;
  73.                         case AccessPermissions.FORM_FIELDS_MODIFICATION: // 2
  74.                             Console.WriteLine("Access permissions level 2 which indicates that permitted changes, with addition to level 1, are: filling in forms, instantiating page templates, and signing. ");
  75.                             break;
  76.                         case AccessPermissions.ANNOTATION_MODIFICATION: // 3
  77.                             Console.WriteLine("Access permissions level 3 which indicates that permitted changes, with addition to level 2, are: annotation creation, deletion and modification. ");
  78.                             break;
  79.                         default :
  80.                             Console.WriteLine("WTF...?");
  81.                             break;
  82.                     } // switch
  83.                     */
  84.  
  85.  
  86.                 } // foreach
  87.             } // if
  88.         } // getCertificationLevel()
  89.  
  90.  
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement