-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathWordParagraph.cs
579 lines (497 loc) · 18.4 KB
/
WordParagraph.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Office2016.Drawing.Charts;
using Break = DocumentFormat.OpenXml.Wordprocessing.Break;
using Hyperlink = DocumentFormat.OpenXml.Wordprocessing.Hyperlink;
using OfficeMath = DocumentFormat.OpenXml.Math.OfficeMath;
using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;
using ParagraphProperties = DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties;
using Picture = DocumentFormat.OpenXml.Wordprocessing.Picture;
using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
using RunProperties = DocumentFormat.OpenXml.Wordprocessing.RunProperties;
using TabStop = DocumentFormat.OpenXml.Wordprocessing.TabStop;
using Text = DocumentFormat.OpenXml.Wordprocessing.Text;
namespace OfficeIMO.Word {
public partial class WordParagraph {
internal WordDocument _document;
internal Paragraph _paragraph;
/// <summary>
/// This allows to know where the paragraph is located. Useful for hyperlinks or other stuff.
/// </summary>
internal string TopParent {
get {
var test = _paragraph.Parent;
if (test is Body) {
return "body";
}
if (test is Header) {
return "header";
}
if (test is Footer) {
return "footer";
}
var parent = test;
do {
parent = parent.Parent;
} while (!(parent is Header) && !(parent is Footer) && !(parent is Body));
if (parent is Body) {
return "body";
}
if (parent is Footer) {
return "footer";
}
if (parent is Header) {
return "header";
}
throw new InvalidOperationException("Please open an issue and describe your situation with Parent");
}
}
public bool IsLastRun {
get {
var runs = _run.Parent.ChildElements.OfType<Run>();
return runs.Last() == _run;
}
}
public bool IsFirstRun {
get {
var runs = _run.Parent.ChildElements.OfType<Run>();
return runs.First() == _run;
}
}
internal RunProperties _runProperties {
get {
if (_run != null) {
return _run.RunProperties;
}
return null;
}
}
internal Text _text {
get {
if (_run != null) {
return _run.ChildElements.OfType<Text>().FirstOrDefault();
}
return null;
}
}
internal Run _run;
internal ParagraphProperties _paragraphProperties {
get {
if (_paragraph != null && _paragraph.ParagraphProperties != null) {
return _paragraph.ParagraphProperties;
}
return null;
}
}
public WordImage Image {
get {
if (_run != null) {
var drawing = _run.ChildElements.OfType<Drawing>().FirstOrDefault();
if (drawing != null) {
if (drawing.Inline != null) {
if (drawing.Inline.Graphic != null) {
if (drawing.Inline.Graphic.GraphicData != null) {
var picture = drawing.Inline.Graphic.GraphicData.ChildElements.OfType<DocumentFormat.OpenXml.Drawing.Pictures.Picture>().FirstOrDefault();
if (picture != null) {
return new WordImage(_document, drawing);
}
}
}
} else if (drawing.Anchor != null) {
var anchorGraphic = drawing.Anchor.OfType<Graphic>().FirstOrDefault();
if (anchorGraphic != null) {
return new WordImage(_document, drawing);
}
}
}
}
return null;
}
}
public bool IsListItem {
get {
if (_paragraphProperties != null && _paragraphProperties.NumberingProperties != null) {
return true;
} else {
return false;
}
}
}
public int? ListItemLevel {
get {
if (_paragraphProperties != null && _paragraphProperties.NumberingProperties != null) {
return _paragraphProperties.NumberingProperties.NumberingLevelReference.Val;
} else {
return null;
}
}
set {
if (_paragraphProperties != null && _paragraphProperties.NumberingProperties != null) {
if (_paragraphProperties.NumberingProperties.NumberingLevelReference != null) {
_paragraphProperties.NumberingProperties.NumberingLevelReference.Val = value;
}
} else {
// should throw?
}
}
}
internal int? _listNumberId {
get {
if (_paragraphProperties != null && _paragraphProperties.NumberingProperties != null) {
return _paragraphProperties.NumberingProperties.NumberingId.Val;
} else {
return null;
}
}
}
public WordParagraphStyles? Style {
get {
if (_paragraphProperties != null && _paragraphProperties.ParagraphStyleId != null) {
return WordParagraphStyle.GetStyle(_paragraphProperties.ParagraphStyleId.Val);
}
return null;
}
set {
if (value != null) {
if (_paragraphProperties == null) {
_paragraph.ParagraphProperties = new ParagraphProperties();
}
if (_paragraphProperties.ParagraphStyleId == null) {
_paragraphProperties.ParagraphStyleId = new ParagraphStyleId();
}
_paragraphProperties.ParagraphStyleId.Val = value.Value.ToStringStyle();
}
}
}
internal WordList _list;
internal List<Run> _runs;
internal Hyperlink _hyperlink;
internal SimpleField _simpleField;
internal BookmarkStart _bookmarkStart;
internal readonly OfficeMath _officeMath;
internal readonly SdtRun _stdRun;
internal readonly DocumentFormat.OpenXml.Math.Paragraph _mathParagraph;
/// <summary>
/// Get or set a text within Paragraph
/// </summary>
public string Text {
get {
if (_text == null) {
return "";
}
return _text.Text;
}
set {
VerifyText();
_text.Text = value;
}
}
/// <summary>
/// Get PageBreaks within Paragraph
/// </summary>
public WordBreak PageBreak {
get {
if (_run != null) {
var brake = _run.ChildElements.OfType<Break>().FirstOrDefault();
if (brake != null && brake.Type != null && brake.Type.Value == BreakValues.Page) {
return new WordBreak(_document, _paragraph, _run);
}
}
return null;
}
}
/// <summary>
/// Get Breaks within Paragraph
/// </summary>
public WordBreak Break {
get {
if (_run != null) {
var brake = _run.ChildElements.OfType<Break>().FirstOrDefault();
if (brake != null) {
return new WordBreak(_document, _paragraph, _run);
}
}
return null;
}
}
public WordTabChar Tab {
get {
if (_run != null) {
var tabChar = _run.ChildElements.OfType<TabChar>().FirstOrDefault();
if (tabChar != null) {
return new WordTabChar(_document, _paragraph, _run);
}
}
return null;
}
}
public WordParagraph(WordDocument document = null, bool newParagraph = true, bool newRun = true) {
this._document = document;
if (newParagraph) {
this._paragraph = new Paragraph();
this._paragraph.AppendChild(new ParagraphProperties());
if (newRun) {
this._run = new Run();
this._paragraph.AppendChild(_run);
}
}
}
internal WordParagraph(WordDocument document, Paragraph paragraph, bool newRun = true) {
this._document = document;
this._paragraph = paragraph;
if (newRun) {
this._run = new Run();
this._paragraph.AppendChild(_run);
}
}
public WordParagraph(WordDocument document, Paragraph paragraph) {
this._document = document;
this._paragraph = paragraph;
}
public WordParagraph(WordDocument document, Paragraph paragraph, Run run) {
_document = document;
_paragraph = paragraph;
_run = run;
}
internal WordParagraph(WordDocument document, Paragraph paragraph, Hyperlink hyperlink) {
_document = document;
_paragraph = paragraph;
_hyperlink = hyperlink;
//this.Hyperlink = new WordHyperLink(document, paragraph, hyperlink);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, List<Run> runs) {
_document = document;
_paragraph = paragraph;
_runs = runs;
//this.Field = new WordField(document, paragraph, runs);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, SimpleField simpleField) {
_document = document;
_paragraph = paragraph;
_simpleField = simpleField;
// this.Field = new WordField(document, paragraph, simpleField);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, BookmarkStart bookmarkStart) {
_document = document;
_paragraph = paragraph;
_bookmarkStart = bookmarkStart;
// this.Bookmark = new WordBookmark(document, paragraph, bookmarkStart);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, DocumentFormat.OpenXml.Math.OfficeMath officeMath) {
_document = document;
_paragraph = paragraph;
_officeMath = officeMath;
//this.Equation = new WordEquation(document, paragraph, officeMath);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, SdtRun stdRun) {
_document = document;
_paragraph = paragraph;
_stdRun = stdRun;
//this.StructuredDocumentTag = new WordStructuredDocumentTag(document, paragraph, stdRun);
}
internal WordParagraph(WordDocument document, Paragraph paragraph, DocumentFormat.OpenXml.Math.Paragraph mathParagraph) {
_document = document;
_paragraph = paragraph;
_mathParagraph = mathParagraph;
// this.Equation = new WordEquation(document, paragraph, mathParagraph);
}
internal WordStructuredDocumentTag StructuredDocumentTag {
get {
if (_stdRun != null) {
return new WordStructuredDocumentTag(_document, _paragraph, _stdRun);
}
return null;
}
}
public WordBookmark Bookmark {
get {
if (_bookmarkStart != null) {
return new WordBookmark(_document, _paragraph, _bookmarkStart);
}
return null;
}
}
public WordEquation Equation {
get {
if (_officeMath != null || _mathParagraph != null) {
return new WordEquation(_document, _paragraph, _officeMath, _mathParagraph);
}
return null;
}
}
public WordField Field {
get {
if (_simpleField != null || _runs != null) {
return new WordField(_document, _paragraph, _simpleField, _runs);
}
return null;
}
}
public WordChart Chart {
get {
if (_run != null) {
var drawing = _run.ChildElements.OfType<Drawing>().FirstOrDefault();
if (drawing != null) {
if (drawing.Inline != null) {
if (drawing.Inline.Graphic != null) {
if (drawing.Inline.Graphic.GraphicData != null) {
var chart = drawing.Inline.Graphic.GraphicData.ChildElements.OfType<DocumentFormat.OpenXml.Drawing.Charts.ChartReference>().FirstOrDefault();
if (chart != null) {
return new WordChart(_document, _paragraph, drawing);
}
}
}
}
}
}
return null;
}
}
public WordHyperLink Hyperlink {
get {
if (_hyperlink != null) {
return new WordHyperLink(_document, _paragraph, _hyperlink);
}
return null;
}
}
public WordFootNote FootNote {
get {
if (_run != null && _runProperties != null) {
var footReference = _run.ChildElements.OfType<FootnoteReference>().FirstOrDefault();
if (footReference != null) {
return new WordFootNote(_document, _paragraph, _run);
}
}
return null;
}
}
public WordEndNote EndNote {
get {
if (_run != null && _runProperties != null) {
var endNoteReference = _run.ChildElements.OfType<EndnoteReference>().FirstOrDefault();
if (endNoteReference != null) {
return new WordEndNote(_document, _paragraph, _run);
}
}
return null;
}
}
public bool IsHyperLink {
get {
if (this.Hyperlink != null) {
return true;
}
return false;
}
}
public bool IsField {
get {
if (this.Field != null && this.Field.Field != null) {
return true;
}
return false;
}
}
public bool IsBookmark {
get {
if (this.Bookmark != null && this.Bookmark.Name != null) {
return true;
}
return false;
}
}
public bool IsEquation {
get {
if (this.Equation != null) {
return true;
}
return false;
}
}
public bool IsStructuredDocumentTag {
get {
if (this.StructuredDocumentTag != null) {
return true;
}
return false;
}
}
public bool IsImage {
get {
if (this.Image != null) {
return true;
}
return false;
}
}
public bool IsTab {
get {
if (this.Tab != null) {
return true;
}
return false;
}
}
public bool IsChart {
get {
if (this.Chart != null) {
return true;
}
return false;
}
}
public bool IsEndNote {
get {
if (this.EndNote != null) {
return true;
}
return false;
}
}
public bool IsFootNote {
get {
if (this.FootNote != null) {
return true;
}
return false;
}
}
public List<WordTabStop> TabStops {
get {
List<WordTabStop> list = new List<WordTabStop>();
if (_paragraph != null && _paragraphProperties != null) {
if (_paragraphProperties.Tabs != null) {
foreach (TabStop tab in _paragraphProperties.Tabs) {
list.Add(new WordTabStop(this, tab));
}
}
}
return list;
}
}
public WordTextBox TextBox {
get {
if (_run != null && _runProperties != null) {
var wordTextboxes = _run.ChildElements.OfType<AlternateContent>().FirstOrDefault();
if (wordTextboxes != null) {
return new WordTextBox(_document, _paragraph, _run);
}
}
return null;
}
}
public bool IsTextBox {
get {
if (this.TextBox != null) {
return true;
}
return false;
}
}
}
}