-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
WordSection.cs
503 lines (435 loc) · 19.3 KB
/
WordSection.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
public partial class WordSection {
/// <summary>
/// Provides a list of all elements within the section including paragraphs, tables, images, etc.
/// </summary>
public List<WordElement> Elements => GetWordElements();
/// <summary>
/// Provides a list of all elements within the section including paragraphs, tables, images, etc.
/// </summary>
public List<WordElement> ElementsByType => GetWordElementsByType();
/// <summary>
/// Provides a list of all paragraphs within the section
/// </summary>
public List<WordParagraph> Paragraphs => GetParagraphsList();
/// <summary>
/// Provides a list of all paragraphs with page breaks within the section
/// </summary>
public List<WordParagraph> ParagraphsPageBreaks {
get { return Paragraphs.Where(p => p.IsPageBreak).ToList(); }
}
/// <summary>
/// Provides a list of all paragraphs with breaks within the section
/// </summary>
public List<WordParagraph> ParagraphsBreaks {
get { return Paragraphs.Where(p => p.IsBreak).ToList(); }
}
internal List<WordParagraph> ParagraphsIsListItem {
get { return Paragraphs.Where(p => p.IsListItem).ToList(); }
}
internal List<int> ParagraphListItemsNumbers {
get {
var listNumbers = new List<int>();
var listItems = Paragraphs.Where(p => p.IsListItem).ToList();
foreach (var item in listItems) {
listNumbers.Add(item._listNumberId.Value);
}
return listNumbers.Distinct().ToList();
}
}
public List<WordParagraph> ParagraphsHyperLinks {
get { return Paragraphs.Where(p => p.IsHyperLink).ToList(); }
}
public List<WordParagraph> ParagraphsFields {
get { return Paragraphs.Where(p => p.IsField).ToList(); }
}
/// <summary>
/// Provides a list of paragraphs that contain Bookmarks
/// </summary>
public List<WordParagraph> ParagraphsBookmarks {
get { return Paragraphs.Where(p => p.IsBookmark).ToList(); }
}
/// <summary>
/// Provies a list of paragraphs that contain Equations
/// </summary>
public List<WordParagraph> ParagraphsEquations {
get { return Paragraphs.Where(p => p.IsEquation).ToList(); }
}
/// <summary>
/// Provies a list of paragraphs that contain Tabs
/// </summary>
public List<WordParagraph> ParagraphsTabs {
get { return Paragraphs.Where(p => p.IsTab).ToList(); }
}
/// <summary>
/// Provides a list of paragraphs that contain TabStops
/// </summary>
public List<WordParagraph> ParagraphsTabStops {
get { return Paragraphs.Where(p => p.TabStops.Count > 0).ToList(); }
}
/// <summary>
/// Provides a list of paragraphs that contain Structured Document Tags
/// </summary>
public List<WordParagraph> ParagraphsStructuredDocumentTags {
get { return Paragraphs.Where(p => p.IsStructuredDocumentTag).ToList(); }
}
/// <summary>
/// Provides a list of paragraphs that contain Image
/// </summary>
public List<WordParagraph> ParagraphsImages {
get { return Paragraphs.Where(p => p.IsImage).ToList(); }
}
public List<WordParagraph> ParagraphsCharts {
get { return Paragraphs.Where(p => p.IsChart).ToList(); }
}
public List<WordParagraph> ParagraphsEndNotes {
get { return Paragraphs.Where(p => p.IsEndNote).ToList(); }
}
public List<WordParagraph> ParagraphsFootNotes {
get { return Paragraphs.Where(p => p.IsFootNote).ToList(); }
}
public List<WordParagraph> ParagraphsTextBoxes {
get { return Paragraphs.Where(p => p.IsTextBox).ToList(); }
}
public List<WordBreak> PageBreaks {
get {
List<WordBreak> list = new List<WordBreak>();
var paragraphs = Paragraphs.Where(p => p.IsPageBreak).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.PageBreak);
}
return list;
}
}
public List<WordChart> Charts {
get {
List<WordChart> list = new List<WordChart>();
var paragraphs = Paragraphs.Where(p => p.IsChart).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Chart);
}
return list;
}
}
public List<WordBreak> Breaks {
get {
List<WordBreak> list = new List<WordBreak>();
var paragraphs = Paragraphs.Where(p => p.IsBreak).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Break);
}
return list;
}
}
/// <summary>
/// Exposes Images in their Image form for easy access (saving, modifying)
/// </summary>
public List<WordImage> Images {
get {
List<WordImage> list = new List<WordImage>();
var paragraphs = Paragraphs.Where(p => p.IsImage).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Image);
}
return list;
}
}
public List<WordBookmark> Bookmarks {
get {
List<WordBookmark> list = new List<WordBookmark>();
var paragraphs = Paragraphs.Where(p => p.IsBookmark).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Bookmark);
}
return list;
}
}
public List<WordField> Fields {
get {
List<WordField> list = new List<WordField>();
var paragraphs = Paragraphs.Where(p => p.IsField).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Field);
}
return list;
}
}
public List<WordEndNote> EndNotes {
get {
List<WordEndNote> list = new List<WordEndNote>();
var paragraphs = Paragraphs.Where(p => p.IsEndNote).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.EndNote);
}
return list;
}
}
public List<WordFootNote> FootNotes {
get {
List<WordFootNote> list = new List<WordFootNote>();
var paragraphs = Paragraphs.Where(p => p.IsFootNote).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.FootNote);
}
return list;
}
}
public List<WordHyperLink> HyperLinks {
get {
List<WordHyperLink> list = new List<WordHyperLink>();
var paragraphs = Paragraphs.Where(p => p.IsHyperLink).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Hyperlink);
}
foreach (var table in this.Tables) {
foreach (var paragraph in table.Paragraphs) {
if (paragraph.IsHyperLink) {
list.Add(paragraph.Hyperlink);
}
}
}
return list;
}
}
public List<WordTabChar> Tabs {
get {
List<WordTabChar> list = new List<WordTabChar>();
var paragraphs = Paragraphs.Where(p => p.IsTab).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Tab);
}
return list;
}
}
public List<WordTextBox> TextBoxes {
get {
List<WordTextBox> list = new List<WordTextBox>();
var paragraphs = Paragraphs.Where(p => p.IsTextBox).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.TextBox);
}
return list;
}
}
public List<WordEquation> Equations {
get {
List<WordEquation> list = new List<WordEquation>();
var paragraphs = Paragraphs.Where(p => p.IsEquation).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.Equation);
}
return list;
}
}
public List<WordStructuredDocumentTag> StructuredDocumentTags {
get {
List<WordStructuredDocumentTag> list = new List<WordStructuredDocumentTag>();
var paragraphs = Paragraphs.Where(p => p.IsStructuredDocumentTag).ToList();
foreach (var paragraph in paragraphs) {
list.Add(paragraph.StructuredDocumentTag);
}
return list;
}
}
public WordFooters Footer = new WordFooters();
public WordHeaders Header = new WordHeaders();
public WordBorders Borders;
public WordMargins Margins;
public WordPageSizes PageSettings;
/// <summary>
/// Provides a list of all lists within the section
/// </summary>
public List<WordList> Lists => GetLists();
/// <summary>
/// Provides a list of all tables within the section, excluding nested tables
/// </summary>
public List<WordTable> Tables => GetTablesList();
/// <summary>
/// Provides a list of all embedded documents within the section
/// </summary>
public List<WordEmbeddedDocument> EmbeddedDocuments => GetEmbeddedDocumentsList();
/// <summary>
/// Provides a list of all watermarks within the section
/// </summary>
public List<WordWatermark> Watermarks {
get {
var sdtBlockList = GetSdtBlockList();
return WordSection.ConvertStdBlockToWatermark(_document, sdtBlockList);
}
}
/// <summary>
/// Provides a list of all tables within the section, including nested tables
/// </summary>
public List<WordTable> TablesIncludingNestedTables {
get {
List<WordTable> list = new List<WordTable>();
foreach (var table in Tables) {
list.Add(table);
// if (table.NestedTables.Count > 0) {
list.AddRange(table.NestedTables);
//}
}
return list;
}
}
internal WordDocument _document;
internal SectionProperties _sectionProperties;
private WordprocessingDocument _wordprocessingDocument;
private readonly Paragraph _paragraph;
/// <summary>
/// Used to load WordSection withing word document
/// </summary>
/// <param name="wordDocument"></param>
/// <param name="sectionProperties"></param>
/// <param name="paragraph"></param>
/// <exception cref="NotImplementedException"></exception>
internal WordSection(WordDocument wordDocument, SectionProperties sectionProperties = null, Paragraph paragraph = null) {
this._document = wordDocument;
this._wordprocessingDocument = wordDocument._wordprocessingDocument;
this._paragraph = paragraph;
if (sectionProperties != null) {
this._sectionProperties = sectionProperties.MakeSureSectionIsValid();
} else {
sectionProperties = wordDocument._wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType<SectionProperties>().FirstOrDefault();
if (sectionProperties == null) {
// most likely not necessary during load - but lets see
// would require a broken document created by some app
sectionProperties = wordDocument._wordprocessingDocument.AddSectionProperties();
}
this._sectionProperties = sectionProperties;
}
wordDocument.Sections.Add(this);
var listSectionEntries = this._sectionProperties.ChildElements.ToList();
foreach (var element in listSectionEntries) {
if (element is HeaderReference) {
WordHeader wordHeader = new WordHeader(wordDocument, (HeaderReference)element, this);
} else if (element is FooterReference) {
WordFooter wordHeader = new WordFooter(wordDocument, (FooterReference)element, this);
} else if (element is PageSize) {
} else if (element is PageMargin) {
} else if (element is PageBorders) {
} else if (element is Columns) {
} else if (element is DocGrid) {
} else if (element is SectionType) {
} else if (element is TitlePage) {
} else {
Debug.WriteLine($"The section '{element.GetType().Name}' is currently not supported. "
+ "To request support, open an issue at https://github.com/EvotecIT/OfficeIMO/issues");
}
}
this.Margins = new WordMargins(wordDocument, this);
this.Borders = new WordBorders(wordDocument, this);
this.PageSettings = new WordPageSizes(wordDocument, this);
}
/// <summary>
/// Used for creating WordSection in new documents
/// </summary>
/// <param name="wordDocument"></param>
/// <param name="paragraph"></param>
internal WordSection(WordDocument wordDocument, Paragraph paragraph = null) {
this._document = wordDocument;
this._wordprocessingDocument = wordDocument._wordprocessingDocument;
this._paragraph = paragraph;
if (paragraph != null) {
var sectionProperties = paragraph.ParagraphProperties.SectionProperties;
if (sectionProperties == null) {
return;
}
this._sectionProperties = sectionProperties;
} else {
var sectionProperties = wordDocument._wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType<SectionProperties>().FirstOrDefault();
if (sectionProperties == null) {
sectionProperties = wordDocument._wordprocessingDocument.AddSectionProperties();
}
this._sectionProperties = sectionProperties;
}
if (this._document.Sections.Count > 0) {
WordSection lastSection = this._document.Sections[this._document.Sections.Count - 1];
var temporarySectionProperties = lastSection._sectionProperties;
if (temporarySectionProperties != null) {
CopySectionProperties(lastSection._sectionProperties, this._sectionProperties);
var old = this._sectionProperties;
this._sectionProperties = lastSection._sectionProperties;
lastSection._sectionProperties = old;
}
}
this.Margins = new WordMargins(wordDocument, this);
this.Borders = new WordBorders(wordDocument, this);
this.PageSettings = new WordPageSizes(wordDocument, this);
wordDocument.Sections.Add(this);
}
public bool DifferentFirstPage {
get {
var sectionProperties = _sectionProperties;
if (sectionProperties != null) {
var titlePage = sectionProperties.ChildElements.OfType<TitlePage>().FirstOrDefault();
if (titlePage != null) {
return true;
}
}
return false;
}
set {
var sectionProperties = _sectionProperties;
if (sectionProperties == null) {
if (value == false) {
// section properties doesn't exists, so we don't do anything
return;
} else {
throw new InvalidOperationException("Section doesn't exits. Weird :-)");
}
}
sectionProperties = _sectionProperties;
var titlePage = sectionProperties.ChildElements.OfType<TitlePage>().FirstOrDefault();
if (value == false) {
if (titlePage == null) {
return;
} else {
titlePage.Remove();
}
} else {
sectionProperties.Append(new TitlePage());
WordHeadersAndFooters.AddHeaderReference(this._document, this, HeaderFooterValues.First);
WordHeadersAndFooters.AddFooterReference(this._document, this, HeaderFooterValues.First);
}
}
}
public bool DifferentOddAndEvenPages {
get {
var headerReference = WordHeadersAndFooters.GetHeaderReference(this._document, this, HeaderFooterValues.Even);
var footerReference = WordHeadersAndFooters.GetFooterReference(this._document, this, HeaderFooterValues.Even);
var settings = _wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings.ChildElements.OfType<EvenAndOddHeaders>().FirstOrDefault();
if (headerReference == true && footerReference == true && settings != null) {
return true;
}
return false;
}
set {
var sectionProperties = _sectionProperties;
WordHeadersAndFooters.AddHeaderReference(this._document, this, HeaderFooterValues.Even);
WordHeadersAndFooters.AddFooterReference(this._document, this, HeaderFooterValues.Even);
var settings = _wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings.ChildElements.OfType<EvenAndOddHeaders>().FirstOrDefault();
if (value != false) {
if (settings == null) {
_wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings.Append(new EvenAndOddHeaders());
}
}
}
}
internal static HeaderFooterValues GetType(string type) {
if (type == "default") {
return HeaderFooterValues.Default;
} else if (type == "even") {
return HeaderFooterValues.Even;
} else {
return HeaderFooterValues.First;
}
}
}
}