forked from EvotecIT/OfficeIMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordPageSizes.cs
334 lines (303 loc) · 13.3 KB
/
WordPageSizes.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
/// <summary>
/// PaperSizes used in Microsoft Office
/// </summary>
public enum WordPageSize {
/// <summary>
/// Custom/unknown paper size that is not defined within OfficeIMO
/// </summary>
Unknown,
/// <summary>
/// Letter is part of the North American loose paper size series.
/// It is the standard for business and academic documents, and measures 216 × 279 mm or 8.5 × 11 inches.
/// </summary>
Letter,
/// <summary>
/// Legal is part of the North American loose paper size series.
/// It is used to make legal pads, and measures 216 × 356 mm or 8.5 × 14 inches.
/// </summary>
Legal,
/// <summary>
/// Statement paper size 5.5 x 8.5 inches
/// </summary>
Statement,
/// <summary>
/// Executive paper size 7.25 x 10.50 inches.
/// </summary>
Executive,
/// <summary>
/// An A3 piece of paper measures 297 × 420 mm or 11.7 × 16.5 inches.
/// </summary>
A3,
/// <summary>
/// An A4 piece of paper measures 210 × 297 mm or 8.3 × 11.7 inches
/// </summary>
A4,
/// <summary>
/// An A5 piece of paper measures 148 × 210 mm or 5.8 × 8.3 inches.
/// </summary>
A5,
/// <summary>
/// An A6 piece of paper measures 105 × 148 mm or 4.1 × 5.8 inches.
/// </summary>
A6,
/// <summary>
/// A B5 piece of paper measures 176 × 250 mm or 6.9 × 9.8 inches.
/// </summary>
B5
}
public class WordPageSizes {
private readonly WordSection _section;
private readonly WordDocument _document;
/// <summary>
/// This element specifies the properties (size and orientation) for all pages in the current section.
/// </summary>
public WordPageSize? PageSize {
get {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
foreach (WordPageSize wordPageSize in Enum.GetValues(typeof(WordPageSize))) {
if (wordPageSize == WordPageSize.Unknown) {
continue;
}
var pageSizeBuiltin = GetDefault(wordPageSize);
if ((pageSizeBuiltin.Width == null && pageSize.Height == null) &&
(pageSizeBuiltin.Height == null && pageSize.Height == null) &&
(pageSizeBuiltin.Code == null && pageSize.Code == null)) {
return wordPageSize;
}
// lets check for standard size
if (pageSizeBuiltin.Width != null && pageSize.Width != null &&
pageSizeBuiltin.Height != null && pageSize.Height != null &&
pageSizeBuiltin.Code != null && pageSize.Code != null &&
pageSizeBuiltin.Width == pageSize.Width &&
pageSizeBuiltin.Height == pageSize.Height &&
pageSizeBuiltin.Code == pageSize.Code) {
return wordPageSize;
}
// lets check for standard size, but with changed orientation
if (pageSizeBuiltin.Width != null && pageSize.Width != null &&
pageSizeBuiltin.Height != null && pageSize.Height != null &&
pageSizeBuiltin.Code != null && pageSize.Code != null &&
pageSizeBuiltin.Width == pageSize.Height &&
pageSizeBuiltin.Height == pageSize.Width &&
pageSizeBuiltin.Code == pageSize.Code) {
return wordPageSize;
}
}
// page size is set but we don't know what it is
return WordPageSize.Unknown;
} else {
// not set page size
return null;
}
}
set => SetPageSize(value);
}
private void SetPageSize(WordPageSize? wordPageSize) {
if (wordPageSize == null) {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
pageSize.Remove();
}
} else {
var pageSizeSettings = GetDefault(wordPageSize);
if (pageSizeSettings == null) {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
pageSize.Remove();
}
} else {
var pageSize = _section._sectionProperties.GetFirstChild<PageSize>();
if (pageSize == null) {
_section._sectionProperties.Append(pageSizeSettings);
} else {
// page size already exists, we will be overwriting it, but before we do, we need to make sure to fix page orientation
// since it may be already set
bool requiresPageOrient = false;
PageOrientationValues pageOrientation = PageOrientationValues.Portrait;
if (pageSize.Orient != null && pageSize.Orient.Value != PageOrientationValues.Portrait) {
pageOrientation = pageSize.Orient;
requiresPageOrient = true;
}
// remove page size (faster than reassign all properties).
pageSize.Remove();
_section._sectionProperties.Append(pageSizeSettings);
// we now need to set page orientation back if it was set
if (requiresPageOrient) {
SetOrientation(_section._sectionProperties, pageOrientation);
}
}
}
}
}
/// <summary>
/// Get or Set section/page Width
/// </summary>
public UInt32Value Width {
get {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
return pageSize.Width.Value;
}
return null;
}
set {
if (_section._sectionProperties != null) {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize == null) {
pageSize = new PageSize();
}
pageSize.Width = value;
}
}
}
/// <summary>
/// Get or Set section/page Height
/// </summary>
public UInt32Value Height {
get {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
return pageSize.Height.Value;
}
return null;
}
set {
if (_section._sectionProperties != null) {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize == null) {
pageSize = new PageSize();
}
pageSize.Height = value;
}
}
}
/// <summary>
/// Get or Set section/page Code
/// </summary>
public UInt16Value Code {
get {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize != null) {
return pageSize.Code;
}
return null;
}
set {
if (_section._sectionProperties != null) {
var pageSize = _section._sectionProperties.ChildElements.OfType<PageSize>().FirstOrDefault();
if (pageSize == null) {
pageSize = new PageSize();
}
pageSize.Code = value;
}
}
}
internal static PageOrientationValues GetOrientation(SectionProperties sectionProperties) {
var pageSize = sectionProperties.GetFirstChild<PageSize>();
if (pageSize == null) {
return PageOrientationValues.Portrait;
}
if (pageSize.Orient != null) {
return pageSize.Orient.Value;
}
return PageOrientationValues.Portrait;
}
internal static void SetOrientation(SectionProperties sectionProperties, PageOrientationValues pageOrientationValue) {
var pageSize = sectionProperties.Descendants<PageSize>().FirstOrDefault();
if (pageSize == null) {
// we need to setup default values for A4
pageSize = WordPageSizes.A4;
pageSize.Orient = PageOrientationValues.Portrait;
sectionProperties.Append(pageSize);
}
if (pageSize.Orient == null) {
pageSize.Orient = PageOrientationValues.Portrait;
}
if (pageSize.Orient != pageOrientationValue) {
// changing orientation is not enough, we need to change width with height and vice versa
var width = pageSize.Width;
var height = pageSize.Height;
pageSize.Width = height;
pageSize.Height = width;
pageSize.Orient = pageOrientationValue;
}
}
/// <summary>
/// Get or Set section/page Orientation
/// </summary>
public PageOrientationValues Orientation {
get => GetOrientation(_section._sectionProperties);
set => SetOrientation(_section._sectionProperties, value);
}
/// <summary>
/// Manipulate section/page settings
/// </summary>
/// <param name="wordDocument"></param>
/// <param name="wordSection"></param>
public WordPageSizes(WordDocument wordDocument, WordSection wordSection) {
_section = wordSection;
_document = wordDocument;
}
private static PageSize GetDefault(WordPageSize? pageSize) {
switch (pageSize) {
case WordPageSize.A3: return A3;
case WordPageSize.A4: return A4;
case WordPageSize.A5: return A5;
case WordPageSize.Executive: return Executive;
case WordPageSize.Unknown: return null;
case WordPageSize.A6: return A6;
case WordPageSize.B5: return B5;
case WordPageSize.Letter: return Letter;
case WordPageSize.Statement: return Statement;
case WordPageSize.Legal: return Legal;
}
throw new ArgumentOutOfRangeException(nameof(pageSize));
}
public static PageSize A3 {
get {
return new PageSize() {
Width = (UInt32Value)16838U,
Height = (UInt32Value)23811U,
Code = (UInt16Value)8U
};
}
}
public static PageSize A4 {
get {
return new PageSize() {
Width = (UInt32Value)11906U,
Height = (UInt32Value)16838U,
Code = (UInt16Value)9U
};
}
}
public static PageSize A5 {
get {
return new PageSize() {
Width = (UInt32Value)8391U,
Height = (UInt32Value)11906U,
Code = (UInt16Value)11U
};
}
}
public static PageSize Executive =>
new PageSize() {
Width = (UInt32Value)10440U,
Height = (UInt32Value)15120U,
Code = (UInt16Value)7U
};
public static PageSize A6 => new PageSize() { Width = (UInt32Value)5953U, Height = (UInt32Value)8391U, Code = (UInt16Value)70U };
public static PageSize B5 => new PageSize() { Width = (UInt32Value)10318U, Height = (UInt32Value)14570U, Code = (UInt16Value)13U };
public static PageSize Statement => new PageSize() { Width = (UInt32Value)7920U, Height = (UInt32Value)12240U, Code = (UInt16Value)6U };
public static PageSize Legal => new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)20160U, Code = (UInt16Value)5U };
public static PageSize Letter => new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U, Code = (UInt16Value)1U };
}
}