File: XPdfForm.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 (409 lines) | stat: -rw-r--r-- 13,330 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
#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.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
#if GDI
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
#endif
#if WPF
using System.Windows.Media;
#endif
using PdfSharp.Internal;
using PdfSharp.Drawing.Pdf;
using PdfSharp.Fonts.TrueType;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Filters;
using PdfSharp.Pdf.Internal;

namespace PdfSharp.Drawing
{
  /// <summary>
  /// Represents a so called 'PDF form external object', which is typically an imported page of an external
  /// PDF document. XPdfForm objects are used like images to draw an existing PDF page of an external
  /// document in the current document. XPdfForm objects can only be placed in PDF documents. If you try
  /// to draw them using a XGraphics based on an GDI+ context no action is taken if no placeholder image
  /// is specified. Otherwise the place holder is drawn.
  /// </summary>
  public class XPdfForm : XForm
  {
    /// <summary>
    /// Initializes a new instance of the XPdfForm class from the specified path to an external PDF document.
    /// Although PDFsharp internally caches XPdfForm objects it is recommended to reuse PdfXFrom objects
    /// in your code and change the PageNumber property if more than one page is needed form the external
    /// document. Furthermore, because XPdfForm can occupy very much memory, it is recommended to
    /// dispose XPdfForm objects if not needed anymore.
    /// </summary>
    internal XPdfForm(string path)
    {
      int pageNumber;
      path = ExtractPageNumber(path, out pageNumber);

      path = Path.GetFullPath(path);
      if (!File.Exists(path))
        throw new FileNotFoundException(PSSR.FileNotFound(path), path);

      if (PdfReader.TestPdfFile(path) == 0)
        throw new ArgumentException("The specified file has no valid PDF file header.", "path");

      this.path = path;
      if (pageNumber != 0)
        PageNumber = pageNumber;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="XPdfForm"/> class from a stream.
    /// </summary>
    /// <param name="stream">The stream.</param>
    internal XPdfForm(Stream stream)
    {
      // Create a dummy unique path
      this.path = "*" + Guid.NewGuid().ToString("B");

      if (PdfReader.TestPdfFile(stream) == 0)
        throw new ArgumentException("The specified stream has no valid PDF file header.", "stream");

      this.externalDocument = PdfReader.Open(stream);
    }

    /// <summary>
    /// Creates an XPdfForm from a file.
    /// </summary>
    public static new XPdfForm FromFile(string path)
    {
      // TODO: Same file should return same object (that's why the function is static).
      return new XPdfForm(path);
    }

    /// <summary>
    /// Creates an XPdfForm from a stream.
    /// </summary>
    public static XPdfForm FromStream(Stream stream)
    {
      return new XPdfForm(stream);
    }

    void Initialize()
    {
      // ImageFormat has no overridden Equals...
    }

    /// <summary>
    /// Sets the form in the state FormState.Finished.
    /// </summary>
    internal override void Finish()
    {
      if (this.formState == FormState.NotATemplate || this.formState == FormState.Finished)
        return;

      base.Finish();

      //if (this.gfx.metafile != null)
      //  this.image = this.gfx.metafile;

      //Debug.Assert(this.fromState == FormState.Created || this.fromState == FormState.UnderConstruction);
      //this.fromState = FormState.Finished;
      //this.gfx.Dispose();
      //this.gfx = null;

      //if (this.pdfRenderer != null)
      //{
      //  this.pdfForm.Stream = new PdfDictionary.PdfStream(PdfEncoders.RawEncoding.GetBytes(this.pdfRenderer.GetContent()), this.pdfForm);

      //  if (this.document.Options.CompressContentStreams)
      //  {
      //    this.pdfForm.Stream.Value = Filtering.FlateDecode.Encode(this.pdfForm.Stream.Value);
      //    this.pdfForm.Elements["/Filter"] = new PdfName("/FlateDecode");
      //  }
      //  int length = this.pdfForm.Stream.Length;
      //  this.pdfForm.Elements.SetInteger("/Length", length);
      //}
    }

    /// <summary>
    /// TODO:
    /// Frees the memory occupied by the underlying imported PDF document, even if other XPdfForm objects
    /// refer to this document. A reuse of this object doesnt fail, because the underlying PDF document
    /// is re-imported if neccessary. NYI
    /// </summary>
    protected override void Dispose(bool disposing)
    {
      if (!this.disposed)
      {
        this.disposed = true;
        try
        {
          if (disposing)
          {
            //...
          }
          if (this.externalDocument != null)
            PdfDocument.Tls.DetachDocument(this.externalDocument.Handle);
          //...
        }
        finally
        {
          base.Dispose(disposing);
        }
      }
    }
    bool disposed;

    /// <summary>
    /// Gets or sets an image that is used for drawing if the current XGraphics object cannot handle
    /// PDF forms. A place holder is useful for showing a preview of a page on the display, because
    /// PDFsharp cannot render native PDF objects.
    /// </summary>
    public XImage PlaceHolder
    {
      get { return this.placeHolder; }
      set { this.placeHolder = value; }
    }
    XImage placeHolder;

    /// <summary>
    /// Gets the underlying PdfPage (if one exists).
    /// </summary>
    public PdfPage Page
    {
      get
      {
        if (IsTemplate)
          return null;
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return page;
      }
    }

    /// <summary>
    /// Gets the number of pages in the PDF form.
    /// </summary>
    public int PageCount
    {
      get
      {
        if (IsTemplate)
          return 1;
        if (this.pageCount == -1)
          this.pageCount = ExternalDocument.Pages.Count;
        return this.pageCount;
      }
    }
    int pageCount = -1;

    /// <summary>
    /// Gets the width in point of the page identified by the property PageNumber.
    /// </summary>
    [Obsolete("Use either PixelWidth or PointWidth. Temporarily obsolete because of rearrangements for WPF.")]
    public override double Width
    {
      get
      {
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return page.Width;
      }
    }

    /// <summary>
    /// Gets the height in point of the page identified by the property PageNumber.
    /// </summary>
    [Obsolete("Use either PixelHeight or PointHeight. Temporarily obsolete because of rearrangements for WPF.")]
    public override double Height
    {
      get
      {
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return page.Height;
      }
    }

    /// <summary>
    /// Gets the width in point of the page identified by the property PageNumber.
    /// </summary>
    public override double PointWidth
    {
      get
      {
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return page.Width;
      }
    }

    /// <summary>
    /// Gets the height in point of the page identified by the property PageNumber.
    /// </summary>
    public override double PointHeight
    {
      get
      {
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return page.Height;
      }
    }

    /// <summary>
    /// Gets the width in point of the page identified by the property PageNumber.
    /// </summary>
    public override int PixelWidth
    {
      get
      {
        //PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        //return (int)page.Width;
        return DoubleUtil.DoubleToInt(PointWidth);
      }
    }

    /// <summary>
    /// Gets the height in point of the page identified by the property PageNumber.
    /// </summary>
    public override int PixelHeight
    {
      get
      {
        //PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        //return (int)page.Height;
        return DoubleUtil.DoubleToInt(PointHeight);
      }
    }

    /// <summary>
    /// Get the size of the page identified by the property PageNumber.
    /// </summary>
    public override XSize Size
    {
      get
      {
        PdfPage page = ExternalDocument.Pages[this.pageNumber - 1];
        return new XSize(page.Width, page.Height);
      }
    }

    /// <summary>
    /// Gets or sets the transformation matrix.
    /// </summary>
    public override XMatrix Transform
    {
      get { return this.transform; }
      set
      {
        if (this.transform != value)
        {
          // discard PdfFromXObject when Transform changed
          this.pdfForm = null;
          this.transform = value;
        }
      }
    }

    /// <summary>
    /// Gets or sets the page number in the external PDF document this object refers to. The page number
    /// is one-based, i.e. it is in the range from 1 to PageCount. The default value is 1.
    /// </summary>
    public int PageNumber
    {
      get { return this.pageNumber; }
      set
      {
        if (IsTemplate)
          throw new InvalidOperationException("The page number of an XPdfForm template cannot be modified.");

        if (this.pageNumber != value)
        {
          this.pageNumber = value;
          // dispose PdfFromXObject when number has changed
          this.pdfForm = null;
        }
      }
    }
    int pageNumber = 1;

    /// <summary>
    /// Gets the underlying document from which pages are imported.
    /// </summary>
    internal PdfDocument ExternalDocument
    {
      // The problem is that you can ask an XPdfForm about the number of its pages before it was
      // drawn the first time. At this moment the XPdfForm doesn't know the document where it will
      // be later draw on one of its pages. To prevent the import of the same document more than
      // once, all imported documents of a thread are cached. The cache is local to the current 
      // thread and not to the appdomain, because I won't get problems in a multi-thread environment
      // that I don't understand.
      get
      {
        if (IsTemplate)
          throw new InvalidOperationException("This XPdfForm is a template and not an imported PDF page; therefore it has no external document.");

        if (this.externalDocument == null)
          this.externalDocument = PdfDocument.Tls.GetDocument(path);
        return this.externalDocument;
      }
    }
    internal PdfDocument externalDocument;

    /// <summary>
    /// Extracts the page number if the path has the form 'MyFile.pdf#123' and returns
    /// the actual path without the number sign and the following digits.
    /// </summary>
    public static string ExtractPageNumber(string path, out int pageNumber)
    {
      if (path == null)
        throw new ArgumentNullException("path");

      pageNumber = 0;
      int length = path.Length;
      if (length != 0)
      {
        length--;
        if (Char.IsDigit(path, length))
        {
          while (Char.IsDigit(path, length) && length >= 0)
            length--;
          if (length > 0 && path[length] == '#')
          {
            // must have at least one dot left of colon to distinguish from e.g. '#123'
            if (path.IndexOf('.') != -1)
            {
              pageNumber = Int32.Parse(path.Substring(length + 1));
              path = path.Substring(0, length);
            }
          }
        }
      }
      return path;
    }
  }
}