-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfSignatureField.cs
More file actions
70 lines (54 loc) · 1.95 KB
/
PdfSignatureField.cs
File metadata and controls
70 lines (54 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using iText.Kernel.Pdf;
using PdfSignabilityCheckerTool.Enums;
namespace PdfSignabilityCheckerTool;
internal class PdfSignatureField(PdfDictionary dict)
{
public PdfDictionary FieldDict { get; } = dict;
public SigFieldPermissions? GetLockDictionary()
{
PdfDictionary lockDict = FieldDict.GetAsDictionary(PdfName.Lock);
if (lockDict != null)
return ExtractPermissionsDictionary(lockDict);
return null;
}
public static SigFieldPermissions ExtractPermissionsDictionary(PdfDictionary dict)
{
SigFieldPermissions sigPerm = new();
// Action
string action = dict.GetAsName(PdfName.Action).GetValue();
sigPerm.Action = GetPdfLockActionFromName(action);
// Fields
List<string> fields = [];
PdfArray fieldsArray = dict.GetAsArray(PdfName.Fields);
if (fieldsArray != null)
{
for (int i = 0; i < fieldsArray.Size(); i++)
{
string? fieldName = fieldsArray.GetAsString(i)?.ToString();
if (fieldName != null)
fields.Add(fieldName);
}
}
sigPerm.Fields = fields;
// Certification permissions
PdfName typeName = dict.GetAsName(PdfName.Type);
if (typeName == PdfName.SigFieldLock)
{
int? num = dict.GetAsInt(PdfName.P);
if (num != null)
sigPerm.CertificationPermission = Helpers.GetCertificationPermissionByNumber(num.Value);
}
return sigPerm;
}
public static PdfLockAction GetPdfLockActionFromName(string? name)
{
return name switch
{
"All" => PdfLockAction.ALL,
"Include" => PdfLockAction.INCLUDE,
"Exclude" => PdfLockAction.EXCLUDE,
null or "" => throw new ArgumentNullException(nameof(name)),
_ => throw new NotSupportedException($"Unknown action name: {name}")
};
}
}