File: PdfAcroField.cs

package info (click to toggle)
pdfmod 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,196 kB
  • ctags: 9,346
  • sloc: cs: 50,590; xml: 1,177; sh: 709; makefile: 640
file content (475 lines) | stat: -rw-r--r-- 16,035 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
//
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics;
using System.Collections;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Internal;

namespace PdfSharp.Pdf.AcroForms
{
  /// <summary>
  /// Represents the base class for all interactive field dictionaries.
  /// </summary>
  public abstract class PdfAcroField : PdfDictionary
  {
    /// <summary>
    /// Initializes a new instance of PdfAcroField.
    /// </summary>
    internal PdfAcroField(PdfDocument document)
      : base(document)
    { }

    /// <summary>
    /// Initializes a new instance of the <see cref="PdfAcroField"/> class. Used for type transformation.
    /// </summary>
    protected PdfAcroField(PdfDictionary dict)
      : base(dict)
    { }

    /// <summary>
    /// Gets the name of this field.
    /// </summary>
    public string Name
    {
      get
      {
        string name = Elements.GetString(Keys.T);
        return name;
      }
    }

    /// <summary>
    /// Gets the field flags of this instance.
    /// </summary>
    public PdfAcroFieldFlags Flags
    {
      // TODO: This entry is inheritable, thus the implementation is incorrect...
      get { return (PdfAcroFieldFlags)Elements.GetInteger(Keys.Ff); }
    }

    internal PdfAcroFieldFlags SetFlags
    {
      get { return (PdfAcroFieldFlags)Elements.GetInteger(Keys.Ff); }
      set { Elements.SetInteger(Keys.Ff, (int)value); }
    }

    /// <summary>
    /// Gets or sets the value of the field.
    /// </summary>
    public PdfItem Value
    {
      get { return Elements[Keys.V]; }
      set
      {
        if (ReadOnly)
          throw new InvalidOperationException("The field is read only.");
        if (value is PdfString || value is PdfName)
          Elements[Keys.V] = value;
        else
          throw new NotImplementedException("Values other than string cannot be set.");
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether the field is read only.
    /// </summary>
    public bool ReadOnly
    {
      get { return (Flags & PdfAcroFieldFlags.ReadOnly) != 0; }
      set
      {
        if (value)
          SetFlags |= PdfAcroFieldFlags.ReadOnly;
        else
          SetFlags &= ~PdfAcroFieldFlags.ReadOnly;
      }
    }

    /// <summary>
    /// Gets the field with the specified name.
    /// </summary>
    public PdfAcroField this[string name]
    {
      get { return GetValue(name); }
    }

    /// <summary>
    /// Gets a child field by name.
    /// </summary>
    protected virtual PdfAcroField GetValue(string name)
    {
      if (name == null || name.Length == 0)
        return this;
      if (HasKids)
        return Fields.GetValue(name);
      return null;
    }

    /// <summary>
    /// Indicates whether the field has child fields.
    /// </summary>
    public bool HasKids
    {
      get
      {
        PdfItem item = Elements[Keys.Kids];
        if (item == null)
          return false;
        if (item is PdfArray)
          return ((PdfArray)item).Elements.Count > 0;
        return false;
      }
    }

    /// <summary>
    /// Gets all descendant's names of this field.
    /// </summary>
    public string[] DescendantNames
    {
      get
      {
        ArrayList names = new ArrayList();
        if (HasKids)
        {
          PdfAcroFieldCollection fields = Fields;
          fields.GetDescendantNames(ref names, null);
        }
        return (string[])names.ToArray(typeof(string));
      }
    }

    internal virtual void GetDescendantNames(ref ArrayList names, string partialName)
    {
      if (HasKids)
      {
        PdfAcroFieldCollection fields = Fields;
        string t = Elements.GetString(Keys.T);
        Debug.Assert(t != "");
        if (t.Length > 0)
        {
          if (partialName != null && partialName.Length > 0)
            partialName += "." + t;
          else
            partialName = t;
          fields.GetDescendantNames(ref names, partialName);
        }
      }
      else
      {
        string t = Elements.GetString(Keys.T);
        Debug.Assert(t != "");
        if (t.Length > 0)
        {
          if (partialName != null && partialName.Length > 0)
            names.Add(partialName + "." + t);
          else
            names.Add(t);
        }
      }
    }

    /// <summary>
    /// Gets the collection of fields within this field.
    /// </summary>
    public PdfAcroField.PdfAcroFieldCollection Fields
    {
      get
      {
        if (this.fields == null)
        {
          object o = Elements.GetValue(Keys.Kids, VCF.CreateIndirect);
          this.fields = (PdfAcroField.PdfAcroFieldCollection)o;
        }
        return this.fields;
      }
    }
    PdfAcroField.PdfAcroFieldCollection fields;

    /// <summary>
    /// Holds a collection of interactive fields.
    /// </summary>
    public sealed class PdfAcroFieldCollection : PdfArray
    {
      PdfAcroFieldCollection(PdfArray array)
        : base(array)
      {
      }

      /// <summary>
      /// Gets the names of all fields in the collection.
      /// </summary>
      public string[] Names
      {
        get
        {
          int count = Elements.Count;
          string[] names = new string[count];
          for (int idx = 0; idx < count; idx++)
            names[idx] = ((PdfDictionary)((PdfReference)Elements[idx]).Value).Elements.GetString(Keys.T);
          return names;
        }
      }

      /// <summary>
      /// Gets an array of all descendant names.
      /// </summary>
      public string[] DescendantNames
      {
        get
        {
          ArrayList names = new ArrayList();
          GetDescendantNames(ref names, null);
          return (string[])names.ToArray(typeof(string));
        }
      }

      internal void GetDescendantNames(ref ArrayList names, string partialName)
      {
        int count = Elements.Count;
        for (int idx = 0; idx < count; idx++)
        {
          PdfAcroField field = this[idx];
          Debug.Assert(field != null);
          if (field != null)
            field.GetDescendantNames(ref names, partialName);
        }
      }

      /// <summary>
      /// Gets a field from the collection. For your convenience an instance of a derived class like
      /// PdfTextFiled or PdfCheckBoxis returned if PDFsharp can guess the actual type of the dictionary.
      /// If the actual type cannot be guessed by PDFsharp the function returns an instance
      /// of PdfGenericField.
      /// </summary>
      public PdfAcroField this[int index]
      {
        get
        {
          PdfItem item = Elements[index];
          Debug.Assert(item is PdfReference);
          PdfDictionary dict = ((PdfReference)item).Value as PdfDictionary;
          Debug.Assert(dict != null);
          PdfAcroField field = dict as PdfAcroField;
          if (field == null && dict != null)
          {
            // Do type transformation
            field = CreateAcroField(dict);
            //Elements[index] = field.XRef;
          }
          return field;
        }
      }

      /// <summary>
      /// Gets the field with the specified name.
      /// </summary>
      public PdfAcroField this[string name]
      {
        get { return GetValue(name); }
      }

      internal PdfAcroField GetValue(string name)
      {
        if (name == null || name.Length == 0)
          return null;

        int dot = name.IndexOf('.');
        string prefix = dot == -1 ? name : name.Substring(0, dot);
        string suffix = dot == -1 ? "" : name.Substring(dot + 1);

        int count = Elements.Count;
        for (int idx = 0; idx < count; idx++)
        {
          PdfAcroField field = this[idx];
          if (field.Name == prefix)
            return field.GetValue(suffix);
        }
        return null;
      }

      /// <summary>
      /// Create a derived type like PdfTextFiled or PdfCheckBox if possible.
      /// If the actual cannot be guessed by PDFsharp the function returns an instance
      /// of PdfGenericField.
      /// </summary>
      PdfAcroField CreateAcroField(PdfDictionary dict)
      {
        string ft = dict.Elements.GetName(Keys.FT);
        PdfAcroFieldFlags flags = (PdfAcroFieldFlags)dict.Elements.GetInteger(Keys.Ff);
        switch (ft)
        {
          case "/Btn":
            if ((flags & PdfAcroFieldFlags.Pushbutton) != 0)
              return new PdfPushButtonField(dict);
            else if ((flags & PdfAcroFieldFlags.Radio) != 0)
              return new PdfRadioButtonField(dict);
            else
              return new PdfCheckBoxField(dict);

          case "/Tx":
            return new PdfTextField(dict);

          case "/Ch":
            if ((flags & PdfAcroFieldFlags.Combo) != 0)
              return new PdfComboBoxField(dict);
            else
              return new PdfListBoxField(dict);

          case "/Sig":
            return new PdfSignatureField(dict);

          default:
            return new PdfGenericField(dict);
        }
      }
    }

    /// <summary>
    /// Predefined keys of this dictionary. 
    /// The description comes from PDF 1.4 Reference.
    /// </summary>
    public class Keys : KeysBase
    {
      /// <summary>
      /// (Required for terminal fields; inheritable) The type of field that this dictionary
      /// describes:
      ///   Btn           Button
      ///   Tx            Text
      ///   Ch            Choice
      ///   Sig (PDF 1.3) Signature
      /// Note: This entry may be present in a nonterminal field (one whose descendants
      /// are themselves fields) in order to provide an inheritable FT value. However, a
      /// nonterminal field does not logically have a type of its own; it is merely a container
      /// for inheritable attributes that are intended for descendant terminal fields of
      /// any type.
      /// </summary>
      [KeyInfo(KeyType.Name | KeyType.Required)]
      public const string FT = "/FT";

      /// <summary>
      /// (Required if this field is the child of another in the field hierarchy; absent otherwise)
      /// The field that is the immediate parent of this one (the field, if any, whose Kids array
      /// includes this field). A field can have at most one parent; that is, it can be included
      /// in the Kids array of at most one other field.
      /// </summary>
      [KeyInfo(KeyType.Dictionary)]
      public const string Parent = "/Parent";

      /// <summary>
      /// (Optional) An array of indirect references to the immediate children of this field.
      /// </summary>
      [KeyInfo(KeyType.Array | KeyType.Optional, typeof(PdfAcroField.PdfAcroFieldCollection))]
      public const string Kids = "/Kids";

      /// <summary>
      /// (Optional) The partial field name.
      /// </summary>
      [KeyInfo(KeyType.TextString | KeyType.Optional)]
      public const string T = "/T";

      /// <summary>
      /// (Optional; PDF 1.3) An alternate field name, to be used in place of the actual
      /// field name wherever the field must be identified in the user interface (such as
      /// in error or status messages referring to the field). This text is also useful
      /// when extracting the documents contents in support of accessibility to disabled
      /// users or for other purposes.
      /// </summary>
      [KeyInfo(KeyType.TextString | KeyType.Optional)]
      public const string TU = "/TU";

      /// <summary>
      /// (Optional; PDF 1.3) The mapping name to be used when exporting interactive form field 
      /// data from the document.
      /// </summary>
      [KeyInfo(KeyType.TextString | KeyType.Optional)]
      public const string TM = "/TM";

      /// <summary>
      /// (Optional; inheritable) A set of flags specifying various characteristics of the field.
      /// Default value: 0.
      /// </summary>
      [KeyInfo(KeyType.Integer | KeyType.Optional)]
      public const string Ff = "/Ff";

      /// <summary>
      /// (Optional; inheritable) The fields value, whose format varies depending on
      /// the field type; see the descriptions of individual field types for further information.
      /// </summary>
      [KeyInfo(KeyType.Various | KeyType.Optional)]
      public const string V = "/V";

      /// <summary>
      /// (Optional; inheritable) The default value to which the field reverts when a
      /// reset-form action is executed. The format of this value is the same as that of V.
      /// </summary>
      [KeyInfo(KeyType.Various | KeyType.Optional)]
      public const string DV = "/DV";

      /// <summary>
      /// (Optional; PDF 1.2) An additional-actions dictionary defining the fields behavior
      /// in response to various trigger events. This entry has exactly the same meaning as
      /// the AA entry in an annotation dictionary.
      /// </summary>
      [KeyInfo(KeyType.Dictionary | KeyType.Optional)]
      public const string AA = "/AA";

      // ----- Additional entries to all fields containing variable text --------------------------

      /// <summary>
      /// (Required; inheritable) A resource dictionary containing default resources
      /// (such as fonts, patterns, or color spaces) to be used by the appearance stream.
      /// At a minimum, this dictionary must contain a Font entry specifying the resource
      /// name and font dictionary of the default font for displaying the fields text.
      /// </summary>
      [KeyInfo(KeyType.Dictionary | KeyType.Required)]
      public const string DR = "/DR";

      /// <summary>
      /// (Required; inheritable) The default appearance string, containing a sequence of
      /// valid page-content graphics or text state operators defining such properties as
      /// the fields text size and color.
      /// </summary>
      [KeyInfo(KeyType.String | KeyType.Required)]
      public const string DA = "/DA";

      /// <summary>
      /// (Optional; inheritable) A code specifying the form of quadding (justification)
      /// to be used in displaying the text:
      ///   0 Left-justified
      ///   1 Centered
      ///   2 Right-justified
      /// Default value: 0 (left-justified).
      /// </summary>
      [KeyInfo(KeyType.Integer | KeyType.Optional)]
      public const string Q = "/Q";
    }
  }
}