1
#region PDFsharp - A .NET library for processing PDF
4
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
8
// http://www.pdfsharp.com
9
// http://sourceforge.net/projects/pdfsharp
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:
18
// The above copyright notice and this permission notice shall be included
19
// in all copies or substantial portions of the Software.
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.
31
using System.Diagnostics;
32
using System.Collections;
35
using PdfSharp.Pdf.Advanced;
36
using PdfSharp.Pdf.IO;
38
namespace PdfSharp.Pdf.Annotations
41
/// Represents the annotations array of a page.
43
public sealed class PdfAnnotations : PdfArray
45
internal PdfAnnotations(PdfDocument document)
50
internal PdfAnnotations(PdfArray array)
56
/// Adds the specified annotation.
58
/// <param name="annotation">The annotation.</param>
59
public void Add(PdfAnnotation annotation)
61
annotation.Document = this.Owner;
62
this.Owner.irefTable.Add(annotation);
63
Elements.Add(annotation.Reference);
67
/// Removes an annotation from the document.
69
public void Remove(PdfAnnotation annotation)
71
if (annotation.Owner != Owner)
72
throw new InvalidOperationException("The annotation does not belong to this document.");
74
Owner.Internals.RemoveObject(annotation);
75
Elements.Remove(annotation.Reference);
79
/// Removes all the annotations from the current page.
83
for (int idx = Count - 1; idx >= 0; idx--)
84
Page.Annotations.Remove(page.Annotations[idx]);
87
//public void Insert(int index, PdfAnnotation annotation)
89
// annotation.Document = this.Document;
90
// this.annotations.Insert(index, annotation);
94
/// Gets the number of annoations in this collection.
98
get { return Elements.Count; }
102
/// Gets the <see cref="PdfSharp.Pdf.Annotations.PdfAnnotation"/> at the specified index.
104
public PdfAnnotation this[int index]
108
PdfReference iref = null;
109
PdfDictionary dict = null;
110
PdfItem item = Elements[index];
111
if ((iref = item as PdfReference) != null)
113
Debug.Assert(iref.Value is PdfDictionary, "Reference to dictionary expected.");
114
dict = (PdfDictionary)iref.Value;
118
Debug.Assert(item is PdfDictionary, "Dictionary expected.");
119
dict = (PdfDictionary)item;
121
PdfAnnotation annotation = dict as PdfAnnotation;
122
if (annotation == null)
124
annotation = new PdfGenericAnnotation(dict);
126
Elements[index] = annotation;
132
//public PdfAnnotation this[int index]
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;
149
/// Gets the page the annotations belongs to.
151
internal PdfPage Page
153
get { return this.page; }
154
set { this.page = value; }
159
/// Fixes the /P element in imported annotation.
161
internal static void FixImportedAnnotation(PdfPage page)
163
PdfArray annots = page.Elements.GetArray(PdfPage.Keys.Annots);
166
int count = annots.Elements.Count;
167
for (int idx=0;idx<count;idx++)
169
PdfDictionary annot = annots.Elements.GetDictionary(idx);
170
if (annot != null && annot.Elements.Contains("/P"))
171
annot.Elements["/P"] = page.Reference;
177
/// Returns an enumerator that iterates through a collection.
179
public override IEnumerator GetEnumerator()
181
return new AnnotationsIterator(this);
184
class AnnotationsIterator : IEnumerator
186
PdfAnnotations annotations;
189
public AnnotationsIterator(PdfAnnotations annotations)
191
this.annotations = annotations;
195
public object Current
197
get { return this.annotations[this.index]; }
200
public bool MoveNext()
202
return ++this.index < this.annotations.Count;