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

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf.Advanced/PdfInternals.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.Reflection;
 
34
using System.Text;
 
35
using System.IO;
 
36
using PdfSharp.Internal;
 
37
using PdfSharp.Pdf;
 
38
using PdfSharp.Pdf.IO;
 
39
 
 
40
namespace PdfSharp.Pdf.Advanced
 
41
{
 
42
  /// <summary>
 
43
  /// Provides access to the internal document data structures. This class prevents the public
 
44
  /// interfaces from pollution with internal functions.
 
45
  /// </summary>
 
46
  public class PdfInternals  // TODO: PdfDocumentInternals... PdfPageInterals etc.
 
47
  {
 
48
    internal PdfInternals(PdfDocument document)
 
49
    {
 
50
      this.document = document;
 
51
    }
 
52
    PdfDocument document;
 
53
 
 
54
    /// <summary>
 
55
    /// Gets or sets the first document identifier.
 
56
    /// </summary>
 
57
    public string FirstDocumentID
 
58
    {
 
59
      get { return this.document.trailer.GetDocumentID(0); }
 
60
      set { this.document.trailer.SetDocumentID(0, value); }
 
61
    }
 
62
 
 
63
    /// <summary>
 
64
    /// Gets the first document identifier as GUID.
 
65
    /// </summary>
 
66
    public Guid FirstDocumentGuid
 
67
    {
 
68
      get { return GuidFromString(this.document.trailer.GetDocumentID(0)); }
 
69
    }
 
70
 
 
71
    /// <summary>
 
72
    /// Gets or sets the second document identifier.
 
73
    /// </summary>
 
74
    public string SecondDocumentID
 
75
    {
 
76
      get { return this.document.trailer.GetDocumentID(1); }
 
77
      set { this.document.trailer.SetDocumentID(1, value); }
 
78
    }
 
79
 
 
80
    /// <summary>
 
81
    /// Gets the first document identifier as GUID.
 
82
    /// </summary>
 
83
    public Guid SecondDocumentGuid
 
84
    {
 
85
      get { return GuidFromString(this.document.trailer.GetDocumentID(0)); }
 
86
    }
 
87
 
 
88
    Guid GuidFromString(string id)
 
89
    {
 
90
      if (id == null || id.Length != 16)
 
91
        return Guid.Empty;
 
92
 
 
93
      StringBuilder guid = new StringBuilder();
 
94
      for (int idx = 0; idx < 16; idx++)
 
95
        guid.AppendFormat("{0:X2}", (byte)id[idx]);
 
96
 
 
97
      return new Guid(guid.ToString());
 
98
    }
 
99
 
 
100
    /// <summary>
 
101
    /// Gets the catalog dictionary.
 
102
    /// </summary>
 
103
    public PdfCatalog Catalog
 
104
    {
 
105
      get { return this.document.Catalog; }
 
106
    }
 
107
 
 
108
    /// <summary>
 
109
    /// Returns the object with the specified Identifier, or null, if no such object exists.
 
110
    /// </summary>
 
111
    public PdfObject GetObject(PdfObjectID objectID)
 
112
    {
 
113
      return document.irefTable[objectID].Value;
 
114
    }
 
115
 
 
116
    /// <summary>
 
117
    /// Returns the PdfReference of the specified object, or null, if the object is not in the
 
118
    /// document's object table.
 
119
    /// </summary>
 
120
    public static PdfReference GetReference(PdfObject obj)
 
121
    {
 
122
      if (obj == null)
 
123
        throw new ArgumentNullException("obj");
 
124
      return obj.Reference;
 
125
    }
 
126
 
 
127
    /// <summary>
 
128
    /// Gets the object identifier of the specified object.
 
129
    /// </summary>
 
130
    public static PdfObjectID GetObjectID(PdfObject obj)
 
131
    {
 
132
      if (obj == null)
 
133
        throw new ArgumentNullException("obj");
 
134
      return obj.ObjectID;
 
135
    }
 
136
 
 
137
    /// <summary>
 
138
    /// Gets the object number of the specified object.
 
139
    /// </summary>
 
140
    public static int GetObjectNumber(PdfObject obj)
 
141
    {
 
142
      if (obj == null)
 
143
        throw new ArgumentNullException("obj");
 
144
      return obj.ObjectNumber;
 
145
    }
 
146
 
 
147
    /// <summary>
 
148
    /// Gets the generation number of the specified object.
 
149
    /// </summary>
 
150
    public static int GenerationNumber(PdfObject obj)
 
151
    {
 
152
      if (obj == null)
 
153
        throw new ArgumentNullException("obj");
 
154
      return obj.GenerationNumber;
 
155
    }
 
156
 
 
157
    /// <summary>
 
158
    /// Gets all indirect objects ordered by their object identifier.
 
159
    /// </summary>
 
160
    public PdfObject[] AllObjects
 
161
    {
 
162
      get
 
163
      {
 
164
        PdfReference[] irefs = this.document.irefTable.AllReferences;
 
165
        int count = irefs.Length;
 
166
        PdfObject[] objects = new PdfObject[count];
 
167
        for (int idx = 0; idx < count; idx++)
 
168
          objects[idx] = irefs[idx].Value;
 
169
        return objects;
 
170
      }
 
171
    }
 
172
 
 
173
    /// <summary>
 
174
    /// Creates the indirect object of the specified type, adds it to the document, and
 
175
    /// returns the object.
 
176
    /// </summary>
 
177
    public T CreateIndirectObject<T>() where T : PdfObject
 
178
    {
 
179
      T result = null;
 
180
      ConstructorInfo ctorInfo = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.ExactBinding,
 
181
        null, new Type[] { typeof(PdfDocument) }, null);
 
182
      if (ctorInfo != null)
 
183
      {
 
184
        result = (T)ctorInfo.Invoke(new object[] { this.document });
 
185
        Debug.Assert(result != null);
 
186
        AddObject(result);
 
187
      }
 
188
      Debug.Assert(result != null, "CreateIndirectObject failed with type " + typeof(T).FullName);
 
189
      return result;
 
190
    }
 
191
 
 
192
    /// <summary>
 
193
    /// Adds an object to the PDF document. This operation and only this operation makes the object 
 
194
    /// an indirect object owned by this document.
 
195
    /// </summary>
 
196
    public void AddObject(PdfObject obj)
 
197
    {
 
198
      if (obj == null)
 
199
        throw new ArgumentNullException("obj");
 
200
      if (obj.Owner == null)
 
201
        obj.Document = this.document;
 
202
      else if (obj.Owner != this.document)
 
203
        throw new InvalidOperationException("Object does not belong to this document.");
 
204
      this.document.irefTable.Add(obj);
 
205
    }
 
206
 
 
207
    /// <summary>
 
208
    /// Removes an object from the PDF document.
 
209
    /// </summary>
 
210
    public void RemoveObject(PdfObject obj)
 
211
    {
 
212
      if (obj == null)
 
213
        throw new ArgumentNullException("obj");
 
214
      if (obj.Reference == null)
 
215
        throw new InvalidOperationException("Only indirect objects can be removed.");
 
216
      if (obj.Owner != this.document)
 
217
        throw new InvalidOperationException("Object does not belong to this document.");
 
218
 
 
219
      this.document.irefTable.Remove(obj.Reference);
 
220
    }
 
221
 
 
222
    /// <summary>
 
223
    /// Returns an array containing the specified object as first element follows by its transitive
 
224
    /// closure. The closure of an object are all objects that can be reached by indirect references. 
 
225
    /// The transitive closure is the result of applying the calculation of the closure to a closure
 
226
    /// as long as no new objects came along. This is e.g. useful for getting all objects belonging 
 
227
    /// to the resources of a page.
 
228
    /// </summary>
 
229
    public PdfObject[] GetClosure(PdfObject obj) // TODO: "..., bool transitive)"
 
230
    {
 
231
      PdfReference[] references = this.document.irefTable.TransitiveClosure(obj);
 
232
      int count = references.Length + 1;
 
233
      PdfObject[] objects = new PdfObject[count];
 
234
      objects[0] = obj;
 
235
      for (int idx = 1; idx < count; idx++)
 
236
        objects[idx] = references[idx - 1].Value;
 
237
      return objects;
 
238
    }
 
239
 
 
240
    /// <summary>
 
241
    /// Writes a PdfItem into the specified stream.
 
242
    /// </summary>
 
243
    // This function exists to keep PdfWriter and PdfItem.WriteObject internal.
 
244
    public void WriteObject(Stream stream, PdfItem item)
 
245
    {
 
246
      // Never write an encrypted object
 
247
      PdfWriter writer = new PdfWriter(stream, null);
 
248
      writer.Options = PdfWriterOptions.OmitStream;
 
249
      item.WriteObject(writer);
 
250
    }
 
251
 
 
252
    /// <summary>
 
253
    /// The name of the custuom value key.
 
254
    /// </summary>
 
255
    public string CustomValueKey = "/PdfSharp.CustomValue";
 
256
  }
 
257
}