~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf.Annotations/PdfAnnotations.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Collections;
 
33
using System.Text;
 
34
using System.IO;
 
35
using PdfSharp.Pdf.Advanced;
 
36
using PdfSharp.Pdf.IO;
 
37
 
 
38
namespace PdfSharp.Pdf.Annotations
 
39
{
 
40
  /// <summary>
 
41
  /// Represents the annotations array of a page.
 
42
  /// </summary>
 
43
  public sealed class PdfAnnotations : PdfArray
 
44
  {
 
45
    internal PdfAnnotations(PdfDocument document)
 
46
      : base(document)
 
47
    {
 
48
    }
 
49
 
 
50
    internal PdfAnnotations(PdfArray array)
 
51
      : base(array)
 
52
    {
 
53
    }
 
54
 
 
55
    /// <summary>
 
56
    /// Adds the specified annotation.
 
57
    /// </summary>
 
58
    /// <param name="annotation">The annotation.</param>
 
59
    public void Add(PdfAnnotation annotation)
 
60
    {
 
61
      annotation.Document = this.Owner;
 
62
      this.Owner.irefTable.Add(annotation);
 
63
      Elements.Add(annotation.Reference);
 
64
    }
 
65
 
 
66
    /// <summary>
 
67
    /// Removes an annotation from the document.
 
68
    /// </summary>
 
69
    public void Remove(PdfAnnotation annotation)
 
70
    {
 
71
      if (annotation.Owner != Owner)
 
72
        throw new InvalidOperationException("The annotation does not belong to this document.");
 
73
 
 
74
      Owner.Internals.RemoveObject(annotation);
 
75
      Elements.Remove(annotation.Reference);
 
76
    }
 
77
 
 
78
    /// <summary>
 
79
    /// Removes all the annotations from the current page.
 
80
    /// </summary>
 
81
    public void Clear()
 
82
    {
 
83
      for (int idx = Count - 1; idx >= 0; idx--)
 
84
        Page.Annotations.Remove(page.Annotations[idx]);
 
85
    }
 
86
 
 
87
    //public void Insert(int index, PdfAnnotation annotation)
 
88
    //{
 
89
    //  annotation.Document = this.Document;
 
90
    //  this.annotations.Insert(index, annotation);
 
91
    //}
 
92
 
 
93
    /// <summary>
 
94
    /// Gets the number of annoations in this collection.
 
95
    /// </summary>
 
96
    public int Count
 
97
    {
 
98
      get { return Elements.Count; }
 
99
    }
 
100
 
 
101
    /// <summary>
 
102
    /// Gets the <see cref="PdfSharp.Pdf.Annotations.PdfAnnotation"/> at the specified index.
 
103
    /// </summary>
 
104
    public PdfAnnotation this[int index]
 
105
    {
 
106
      get
 
107
      {
 
108
        PdfReference iref = null;
 
109
        PdfDictionary dict = null;
 
110
        PdfItem item = Elements[index];
 
111
        if ((iref = item as PdfReference) != null)
 
112
        {
 
113
          Debug.Assert(iref.Value is PdfDictionary, "Reference to dictionary expected.");
 
114
          dict = (PdfDictionary)iref.Value;
 
115
        }
 
116
        else
 
117
        {
 
118
          Debug.Assert(item is PdfDictionary, "Dictionary expected.");
 
119
          dict = (PdfDictionary)item;
 
120
        }
 
121
        PdfAnnotation annotation = dict as PdfAnnotation;
 
122
        if (annotation == null)
 
123
        {
 
124
          annotation = new PdfGenericAnnotation(dict);
 
125
          if (iref == null)
 
126
            Elements[index] = annotation;
 
127
        }
 
128
        return annotation;
 
129
      }
 
130
    }
 
131
 
 
132
    //public PdfAnnotation this[int index]
 
133
    //{
 
134
    //  get 
 
135
    //  {
 
136
    //      //DMH 6/7/06
 
137
    //      //Broke this out to simplfy debugging
 
138
    //      //Use a generic annotation to access the Meta data
 
139
    //      //Assign this as the parent of the annotation
 
140
    //      PdfReference r = Elements[index] as PdfReference;
 
141
    //      PdfDictionary d = r.Value as PdfDictionary;
 
142
    //      PdfGenericAnnotation a = new PdfGenericAnnotation(d);
 
143
    //      a.Collection = this;
 
144
    //      return a;
 
145
    //  }
 
146
    //}
 
147
 
 
148
    /// <summary>
 
149
    /// Gets the page the annotations belongs to.
 
150
    /// </summary>
 
151
    internal PdfPage Page
 
152
    {
 
153
      get { return this.page; }
 
154
      set { this.page = value; }
 
155
    }
 
156
    PdfPage page;
 
157
 
 
158
    /// <summary>
 
159
    /// Fixes the /P element in imported annotation.
 
160
    /// </summary>
 
161
    internal static void FixImportedAnnotation(PdfPage page)
 
162
    {
 
163
      PdfArray annots = page.Elements.GetArray(PdfPage.Keys.Annots);
 
164
      if (annots != null)
 
165
      {
 
166
        int count = annots.Elements.Count;
 
167
        for (int idx=0;idx<count;idx++)
 
168
        {
 
169
          PdfDictionary annot = annots.Elements.GetDictionary(idx);
 
170
          if (annot != null && annot.Elements.Contains("/P"))
 
171
            annot.Elements["/P"] = page.Reference;
 
172
        }
 
173
      }
 
174
    }
 
175
 
 
176
    /// <summary>
 
177
    /// Returns an enumerator that iterates through a collection.
 
178
    /// </summary>
 
179
    public override IEnumerator GetEnumerator()
 
180
    {
 
181
      return new AnnotationsIterator(this);
 
182
    }
 
183
 
 
184
    class AnnotationsIterator : IEnumerator
 
185
    {
 
186
      PdfAnnotations annotations;
 
187
      int index;
 
188
 
 
189
      public AnnotationsIterator(PdfAnnotations annotations)
 
190
      {
 
191
        this.annotations = annotations;
 
192
        this.index = -1;
 
193
      }
 
194
 
 
195
      public object Current
 
196
      {
 
197
        get { return this.annotations[this.index]; }
 
198
      }
 
199
 
 
200
      public bool MoveNext()
 
201
      {
 
202
        return ++this.index < this.annotations.Count;
 
203
      }
 
204
 
 
205
      public void Reset()
 
206
      {
 
207
        this.index = -1;
 
208
      }
 
209
    }
 
210
  }
 
211
}