MuPDFCore 1.8.0
Multiplatform .NET bindings for MuPDF
Loading...
Searching...
No Matches
MuPDFPage.cs
1/*
2 MuPDFCore - A set of multiplatform .NET Core bindings for MuPDF.
3 Copyright (C) 2020 Giorgio Bianchini
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>
16*/
17
18using System;
19using System.Collections;
20using System.Collections.Generic;
21
22namespace MuPDFCore
23{
24 /// <summary>
25 /// A wrapper over a MuPDF page object, which contains information about the page's boundaries.
26 /// </summary>
27 public class MuPDFPage : IDisposable
28 {
29 /// <summary>
30 /// The page's bounds at 72 DPI. Read-only.
31 /// </summary>
32 public Rectangle Bounds { get; }
33
34 /// <summary>
35 /// The number of this page in the original document.
36 /// </summary>
37 public int PageNumber { get; }
38
39 /// <summary>
40 /// A pointer to the native page object.
41 /// </summary>
42 internal readonly IntPtr NativePage;
43
44 /// <summary>
45 /// The context that owns the document from which this page was extracted.
46 /// </summary>
47 private readonly MuPDFContext OwnerContext;
48
49 /// <summary>
50 /// The document from which the page was extracted.
51 /// </summary>
52 internal readonly MuPDFDocument OwnerDocument;
53
54 /// <summary>
55 /// The page's original bounds. Read-only.
56 /// </summary>
57 internal Rectangle OriginalBounds { get; }
58
59 /// <summary>
60 /// Create a new <see cref="MuPDFPage"/> object from the specified document.
61 /// </summary>
62 /// <param name="context">The context that owns the document.</param>
63 /// <param name="document">The document from which the page should be extracted.</param>
64 /// <param name="number">The number of the page that should be extracted (starting at 0).</param>
65 internal MuPDFPage(MuPDFContext context, MuPDFDocument document, int number)
66 {
67 if (document.EncryptionState == EncryptionState.Encrypted)
68 {
69 throw new DocumentLockedException("A password is necessary to render the document!");
70 }
71
72 this.OwnerContext = context;
73 this.OwnerDocument = document;
74 this.PageNumber = number;
75
76 float x = 0;
77 float y = 0;
78 float w = 0;
79 float h = 0;
80
81 ExitCodes result = (ExitCodes)NativeMethods.LoadPage(context.NativeContext, document.NativeDocument, number, ref NativePage, ref x, ref y, ref w, ref h);
82
83 this.Bounds = new Rectangle(Math.Round(x * document.ImageXRes / 72.0 * 1000) / 1000, Math.Round(y * document.ImageYRes / 72.0 * 1000) / 1000, Math.Round(w * document.ImageXRes / 72.0 * 1000) / 1000, Math.Round(h * document.ImageYRes / 72.0 * 1000) / 1000);
84 this.OriginalBounds = new Rectangle(x, y, w, h);
85
86 switch (result)
87 {
88 case ExitCodes.EXIT_SUCCESS:
89 break;
90 case ExitCodes.ERR_CANNOT_LOAD_PAGE:
91 throw new MuPDFException("Cannot load page", result);
92 case ExitCodes.ERR_CANNOT_COMPUTE_BOUNDS:
93 throw new MuPDFException("Cannot compute bounds", result);
94 default:
95 throw new MuPDFException("Unknown error", result);
96 }
97 }
98
99 private bool disposedValue;
100
101 ///<inheritdoc/>
102 protected virtual void Dispose(bool disposing)
103 {
104 if (!disposedValue)
105 {
106 NativeMethods.DisposePage(OwnerContext.NativeContext, NativePage);
107 disposedValue = true;
108 }
109 }
110
111 ///<inheritdoc/>
112 ~MuPDFPage()
113 {
114 if (NativePage != IntPtr.Zero)
115 {
116 Dispose(disposing: false);
117 }
118 }
119
120 ///<inheritdoc/>
121 public void Dispose()
122 {
123 Dispose(disposing: true);
124 GC.SuppressFinalize(this);
125 }
126 }
127
128 /// <summary>
129 /// A lazy collection of <see cref="MuPDFPage"/>s. Each page is loaded from the document as it is requested for the first time.
130 /// </summary>
131 public class MuPDFPageCollection : IReadOnlyList<MuPDFPage>, IDisposable
132 {
133 /// <summary>
134 /// The internal store of the pages.
135 /// </summary>
136 private readonly MuPDFPage[] Pages;
137
138 /// <summary>
139 /// The context that owns the document from which the pages were extracted.
140 /// </summary>
141 private readonly MuPDFContext OwnerContext;
142
143 /// <summary>
144 /// The document from which the pages were extracted.
145 /// </summary>
146 private readonly MuPDFDocument OwnerDocument;
147
148 /// <summary>
149 /// The number of pages in the collection.
150 /// </summary>
151 public int Length { get { return Pages.Length; } }
152
153 /// <summary>
154 /// The number of pages in the collection.
155 /// </summary>
156 public int Count { get { return Pages.Length; } }
157
158 /// <summary>
159 /// Get a page from the collection.
160 /// </summary>
161 /// <param name="index">The number of the page (starting at 0).</param>
162 /// <returns>The specified <see cref="MuPDFPage"/>.</returns>
163 public MuPDFPage this[int index]
164 {
165 get
166 {
167 if (index < 0 || index > Pages.Length)
168 {
169 throw new IndexOutOfRangeException();
170 }
171
172 if (Pages[index] == null)
173 {
174 Pages[index] = new MuPDFPage(OwnerContext, OwnerDocument, index);
175 }
176
177 return Pages[index];
178 }
179 }
180
181 /// <summary>
182 /// Create a new <see cref="MuPDFPageCollection"/> from the specified document, containing the specified number of pages.
183 /// </summary>
184 /// <param name="context">The context that owns the document.</param>
185 /// <param name="document">The document from which the pages should be extracted.</param>
186 /// <param name="length">The number of pages in the document.</param>
187 internal MuPDFPageCollection(MuPDFContext context, MuPDFDocument document, int length)
188 {
189 Pages = new MuPDFPage[length];
190 OwnerContext = context;
191 OwnerDocument = document;
192 }
193
194 ///<inheritdoc/>
195 public IEnumerator<MuPDFPage> GetEnumerator()
196 {
197 for (int i = 0; i < Pages.Length; i++)
198 {
199 if (Pages[i] == null)
200 {
201 Pages[i] = new MuPDFPage(OwnerContext, OwnerDocument, i);
202 }
203 }
204
205 return ((IEnumerable<MuPDFPage>)Pages).GetEnumerator();
206 }
207
208 ///<inheritdoc/>
209 IEnumerator IEnumerable.GetEnumerator()
210 {
211 return GetEnumerator();
212 }
213
214 private bool disposedValue;
215
216 ///<inheritdoc/>
217 protected virtual void Dispose(bool disposing)
218 {
219 if (!disposedValue)
220 {
221 if (disposing)
222 {
223 for (int i = 0; i < Pages.Length; i++)
224 {
225 Pages[i]?.Dispose();
226 }
227 }
228
229 disposedValue = true;
230 }
231 }
232
233 ///<inheritdoc/>
234 public void Dispose()
235 {
236 Dispose(disposing: true);
237 GC.SuppressFinalize(this);
238 }
239 }
240}
A wrapper around a MuPDF context object, which contains the exception stack and the resource cache st...
Definition: MuPDFContext.cs:26
A wrapper over a MuPDF document object, which contains possibly multiple pages.
A lazy collection of MuPDFPages. Each page is loaded from the document as it is requested for the fir...
Definition: MuPDFPage.cs:132
IEnumerator< MuPDFPage > GetEnumerator()
inheritdoc/>
Definition: MuPDFPage.cs:195
int Length
The number of pages in the collection.
Definition: MuPDFPage.cs:151
int Count
The number of pages in the collection.
Definition: MuPDFPage.cs:156
A wrapper over a MuPDF page object, which contains information about the page's boundaries.
Definition: MuPDFPage.cs:28
int PageNumber
The number of this page in the original document.
Definition: MuPDFPage.cs:37
Rectangle Bounds
The page's bounds at 72 DPI. Read-only.
Definition: MuPDFPage.cs:32
EncryptionState
Possible document encryption states.
Definition: MuPDF.cs:277
ExitCodes
Exit codes returned by native methods describing various errors that can occur.
Definition: MuPDF.cs:32
Represents a rectangle.
Definition: Rectangles.cs:327