forked from pvginkel/PdfViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdfDocument.cs
145 lines (123 loc) · 4.7 KB
/
PdfDocument.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
namespace PdfViewer
{
/// <summary>
/// Provides functionality to render a PDF document.
/// </summary>
public abstract class PdfDocument : IDisposable
{
private static readonly PdfEngine _defaultEngine;
static PdfDocument()
{
if (HaveAssembly("PDFLibNet.dll"))
_defaultEngine = PdfEngine.XPdf;
else if (HaveAssembly("pdf.dll"))
_defaultEngine = PdfEngine.Chrome;
else
throw new Exception("Cannot resolve default PDF engine");
}
private static bool HaveAssembly(string fileName)
{
if (File.Exists(fileName))
return true;
string assemblyPath = Path.GetDirectoryName(typeof(PdfDocument).Assembly.Location);
return File.Exists(Path.Combine(assemblyPath, fileName));
}
/// <summary>
/// Initializes a new instance of the PdfDocument class with the provided path.
/// </summary>
/// <param name="path"></param>
public static PdfDocument Load(string path)
{
return Load(path, PdfEngine.AutoDetect);
}
/// <summary>
/// Initializes a new instance of the PdfDocument class with the provided path.
/// </summary>
/// <param name="path"></param>
public static PdfDocument Load(string path, PdfEngine engine)
{
if (engine == PdfEngine.AutoDetect)
engine = _defaultEngine;
switch (engine)
{
case PdfEngine.Chrome:
return new Chrome.PdfDocument(path);
case PdfEngine.XPdf:
return new XPdf.PdfDocument(path);
default:
throw new ArgumentOutOfRangeException("engine");
}
}
/// <summary>
/// Initializes a new instance of the PdfDocument class with the provided stream.
/// </summary>
/// <param name="stream"></param>
public static PdfDocument Load(Stream stream)
{
return Load(stream, PdfEngine.AutoDetect);
}
/// <summary>
/// Initializes a new instance of the PdfDocument class with the provided stream.
/// </summary>
/// <param name="stream"></param>
public static PdfDocument Load(Stream stream, PdfEngine engine)
{
if (engine == PdfEngine.AutoDetect)
engine = _defaultEngine;
switch (engine)
{
case PdfEngine.Chrome:
return new Chrome.PdfDocument(stream);
case PdfEngine.XPdf:
return new XPdf.PdfDocument(stream);
default:
throw new ArgumentOutOfRangeException("engine");
}
}
/// <summary>
/// Number of pages in the PDF document.
/// </summary>
public abstract int PageCount { get; }
/// <summary>
/// Renders a page of the PDF document to the provided graphics instance.
/// </summary>
/// <param name="page">Number of the page to render.</param>
/// <param name="graphics">Graphics instance to render the page on.</param>
/// <param name="dpiX">Horizontal DPI.</param>
/// <param name="dpiY">Vertical DPI.</param>
/// <param name="bounds">Bounds to render the page in.</param>
public abstract void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds);
/// <summary>
/// Save the PDF document to the specified location.
/// </summary>
/// <param name="path">Path to save the PDF document to.</param>
public abstract void Save(string path);
/// <summary>
/// Save the PDF document to the specified location.
/// </summary>
/// <param name="stream">Stream to save the PDF document to.</param>
public abstract void Save(Stream stream);
/// <summary>
/// Creates a <see cref="PrintDocument"/> for the PDF document.
/// </summary>
/// <returns></returns>
public PrintDocument CreatePrintDocument()
{
return new PdfPrintDocument(this);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}