|
27 | 27 | // DEALINGS IN THE SOFTWARE.
|
28 | 28 | #endregion
|
29 | 29 |
|
| 30 | +using PdfSharpCore.Drawing; |
| 31 | +using PdfSharpCore.Pdf.Annotations; |
| 32 | +using PdfSharpCore.Pdf.Signatures; |
| 33 | +using System; |
| 34 | + |
30 | 35 | namespace PdfSharpCore.Pdf.AcroForms
|
31 | 36 | {
|
32 | 37 | /// <summary>
|
33 | 38 | /// Represents the signature field.
|
34 | 39 | /// </summary>
|
35 | 40 | public sealed class PdfSignatureField : PdfAcroField
|
36 | 41 | {
|
| 42 | + private bool visible; |
| 43 | + |
| 44 | + public string Reason |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + return Elements.GetDictionary(Keys.V).Elements.GetString(Keys.Reason); |
| 49 | + } |
| 50 | + set |
| 51 | + { |
| 52 | + Elements.GetDictionary(Keys.V).Elements[Keys.Reason] = new PdfString(value); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public string Location |
| 57 | + { |
| 58 | + get |
| 59 | + { |
| 60 | + return Elements.GetDictionary(Keys.V).Elements.GetString(Keys.Location); |
| 61 | + } |
| 62 | + set |
| 63 | + { |
| 64 | + Elements.GetDictionary(Keys.V).Elements[Keys.Location] = new PdfString(value); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public PdfItem Contents |
| 69 | + { |
| 70 | + get |
| 71 | + { |
| 72 | + return Elements.GetDictionary(Keys.V).Elements[Keys.Contents]; |
| 73 | + } |
| 74 | + set |
| 75 | + { |
| 76 | + Elements.GetDictionary(Keys.V).Elements.Add(Keys.Contents, value); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public PdfItem ByteRange |
| 82 | + { |
| 83 | + get |
| 84 | + { |
| 85 | + return Elements.GetDictionary(Keys.V).Elements[Keys.ByteRange]; |
| 86 | + } |
| 87 | + set |
| 88 | + { |
| 89 | + Elements.GetDictionary(Keys.V).Elements.Add(Keys.ByteRange, value); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public PdfRectangle Rectangle |
| 95 | + { |
| 96 | + get |
| 97 | + { |
| 98 | + return (PdfRectangle)Elements[Keys.Rect]; |
| 99 | + } |
| 100 | + set |
| 101 | + { |
| 102 | + Elements.Add(Keys.Rect, value); |
| 103 | + this.visible = !(value.X1 + value.X2 + value.Y1 + value.Y2 == 0); |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public ISignatureAppearanceHandler AppearanceHandler { get; internal set; } |
| 110 | + |
37 | 111 | /// <summary>
|
38 | 112 | /// Initializes a new instance of PdfSignatureField.
|
39 | 113 | /// </summary>
|
40 |
| - internal PdfSignatureField(PdfDocument document) |
41 |
| - : base(document) |
42 |
| - { } |
| 114 | + internal PdfSignatureField(PdfDocument document) : base(document) |
| 115 | + { |
| 116 | + |
| 117 | + |
| 118 | + Elements.Add(Keys.FT, new PdfName("/Sig")); |
| 119 | + Elements.Add(Keys.T, new PdfString("Signature1")); |
| 120 | + Elements.Add(Keys.Ff, new PdfInteger(132)); |
| 121 | + Elements.Add(Keys.DR, new PdfDictionary()); |
| 122 | + Elements.Add(Keys.Type, new PdfName("/Annot")); |
| 123 | + Elements.Add(Keys.Subtype, new PdfName("/Widget")); |
| 124 | + Elements.Add(Keys.P, document.Pages[0]); |
| 125 | + |
| 126 | + |
| 127 | + PdfDictionary sign = new PdfDictionary(document); |
| 128 | + sign.Elements.Add(Keys.Type, new PdfName("/Sig")); |
| 129 | + sign.Elements.Add(Keys.Filter, new PdfName("/Adobe.PPKLite")); |
| 130 | + sign.Elements.Add(Keys.SubFilter, new PdfName("/adbe.pkcs7.detached")); |
| 131 | + sign.Elements.Add(Keys.M, new PdfDate(DateTime.Now)); |
| 132 | + |
| 133 | + document._irefTable.Add(sign); |
| 134 | + document._irefTable.Add(this); |
| 135 | + |
| 136 | + Elements.Add(Keys.V, sign); |
| 137 | + |
| 138 | + } |
43 | 139 |
|
44 | 140 | internal PdfSignatureField(PdfDictionary dict)
|
45 | 141 | : base(dict)
|
46 | 142 | { }
|
47 | 143 |
|
| 144 | + |
| 145 | + internal override void PrepareForSave() |
| 146 | + { |
| 147 | + if (!this.visible) |
| 148 | + return; |
| 149 | + |
| 150 | + if (this.AppearanceHandler == null) |
| 151 | + throw new Exception("AppearanceHandler is null"); |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + PdfRectangle rect = Elements.GetRectangle(PdfAnnotation.Keys.Rect); |
| 156 | + XForm form = new XForm(this._document, rect.Size); |
| 157 | + XGraphics gfx = XGraphics.FromForm(form); |
| 158 | + |
| 159 | + this.AppearanceHandler.DrawAppearance(gfx, rect.ToXRect()); |
| 160 | + |
| 161 | + form.DrawingFinished(); |
| 162 | + |
| 163 | + // Get existing or create new appearance dictionary |
| 164 | + PdfDictionary ap = Elements[PdfAnnotation.Keys.AP] as PdfDictionary; |
| 165 | + if (ap == null) |
| 166 | + { |
| 167 | + ap = new PdfDictionary(this._document); |
| 168 | + Elements[PdfAnnotation.Keys.AP] = ap; |
| 169 | + } |
| 170 | + |
| 171 | + // Set XRef to normal state |
| 172 | + ap.Elements["/N"] = form.PdfForm.Reference; |
| 173 | + } |
| 174 | + |
48 | 175 | /// <summary>
|
49 | 176 | /// Predefined keys of this dictionary.
|
50 | 177 | /// The description comes from PDF 1.4 Reference.
|
51 | 178 | /// </summary>
|
52 | 179 | public new class Keys : PdfAcroField.Keys
|
53 |
| - { |
54 |
| - /// <summary> |
55 |
| - /// (Optional) The type of PDF object that this dictionary describes; if present, |
56 |
| - /// must be Sig for a signature dictionary. |
57 |
| - /// </summary> |
58 |
| - [KeyInfo(KeyType.Name | KeyType.Optional)] |
59 |
| - public const string Type = "/Type"; |
| 180 | + { |
60 | 181 |
|
61 | 182 | /// <summary>
|
62 | 183 | /// (Required; inheritable) The name of the signature handler to be used for
|
@@ -113,6 +234,12 @@ internal PdfSignatureField(PdfDictionary dict)
|
113 | 234 | [KeyInfo(KeyType.TextString | KeyType.Optional)]
|
114 | 235 | public const string Reason = "/Reason";
|
115 | 236 |
|
| 237 | + /// <summary> |
| 238 | + /// (Optional) |
| 239 | + /// </summary> |
| 240 | + [KeyInfo(KeyType.TextString | KeyType.Optional)] |
| 241 | + public const string ContactInfo = "/ContactInfo"; |
| 242 | + |
116 | 243 | /// <summary>
|
117 | 244 | /// Gets the KeysMeta for these keys.
|
118 | 245 | /// </summary>
|
|
0 commit comments