forked from EvotecIT/OfficeIMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordDocument.PublicMethods.cs
459 lines (386 loc) · 21.5 KB
/
WordDocument.PublicMethods.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
public partial class WordDocument {
public WordParagraph AddParagraph(WordParagraph wordParagraph = null) {
if (wordParagraph == null) {
// we create paragraph (and within that add it to document)
wordParagraph = new WordParagraph(this, newParagraph: true, newRun: false);
}
this._wordprocessingDocument.MainDocumentPart.Document.Body.AppendChild(wordParagraph._paragraph);
return wordParagraph;
}
public WordParagraph AddParagraph(string text) {
//return AddParagraph().SetText(text);
return AddParagraph().AddText(text);
}
public WordParagraph AddPageBreak() {
WordParagraph newWordParagraph = new WordParagraph {
_run = new Run(new Break() { Type = BreakValues.Page }),
_document = this
};
newWordParagraph._paragraph = new Paragraph(newWordParagraph._run);
this._document.Body.Append(newWordParagraph._paragraph);
return newWordParagraph;
}
public void AddHeadersAndFooters() {
WordHeadersAndFooters.AddHeadersAndFooters(this);
}
public WordParagraph AddBreak(BreakValues breakType = BreakValues.Page) {
WordParagraph newWordParagraph = new WordParagraph {
_run = new Run(new Break() { Type = breakType }),
_document = this
};
newWordParagraph._paragraph = new Paragraph(newWordParagraph._run);
this._document.Body.Append(newWordParagraph._paragraph);
this.Paragraphs.Add(newWordParagraph);
return newWordParagraph;
}
public WordParagraph AddHyperLink(string text, Uri uri, bool addStyle = false, string tooltip = "", bool history = true) {
return this.AddParagraph().AddHyperLink(text, uri, addStyle, tooltip, history);
}
public WordParagraph AddHyperLink(string text, string anchor, bool addStyle = false, string tooltip = "", bool history = true) {
return this.AddParagraph().AddHyperLink(text, anchor, addStyle, tooltip, history);
}
public WordChart AddBarChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) {
var paragraph = this.AddParagraph();
var barChart = WordBarChart.AddBarChart(this, paragraph, title, roundedCorners, width, height);
return barChart;
}
public WordChart AddLineChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) {
var paragraph = this.AddParagraph();
var lineChart = WordLineChart.AddLineChart(this, paragraph, title, roundedCorners, width, height);
return lineChart;
}
public WordChart AddAreaChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) {
var paragraph = this.AddParagraph();
var lineChart = WordAreaChart.AddAreaChart(this, paragraph, title, roundedCorners, width, height);
return lineChart;
}
//public WordBarChart3D AddBarChart3D() {
// var paragraph = this.AddParagraph();
// var barChart = WordBarChart3D.AddBarChart3D(this, paragraph);
// return barChart;
//}
public WordChart AddPieChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) {
var paragraph = this.AddParagraph();
var pieChart = WordPieChart.AddPieChart(this, paragraph, title, roundedCorners, width, height);
return pieChart;
}
public WordList AddList(WordListStyle style) {
WordList wordList = new WordList(this);
wordList.AddList(style);
return wordList;
}
public WordList AddTableOfContentList(WordListStyle style) {
WordList wordList = new WordList(this, true);
wordList.AddList(style);
return wordList;
}
public WordTable AddTable(int rows, int columns, WordTableStyle tableStyle = WordTableStyle.TableGrid) {
WordTable wordTable = new WordTable(this, rows, columns, tableStyle);
return wordTable;
}
public WordTableOfContent AddTableOfContent(TableOfContentStyle tableOfContentStyle = TableOfContentStyle.Template1) {
WordTableOfContent wordTableContent = new WordTableOfContent(this, tableOfContentStyle);
return wordTableContent;
}
public WordCoverPage AddCoverPage(CoverPageTemplate coverPageTemplate) {
WordCoverPage wordCoverPage = new WordCoverPage(this, coverPageTemplate);
return wordCoverPage;
}
public WordTextBox AddTextBox(string text, WrapTextImage wrapTextImage = WrapTextImage.Square) {
WordTextBox wordTextBox = new WordTextBox(this, text, wrapTextImage);
return wordTextBox;
}
public WordParagraph AddHorizontalLine(BorderValues lineType = BorderValues.Single, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) {
return this.AddParagraph().AddHorizontalLine(lineType, color, size, space);
}
public WordSection AddSection(SectionMarkValues? sectionMark = null) {
//Paragraph paragraph = new Paragraph() { RsidParagraphAddition = "fff0", RsidRunAdditionDefault = "fff0"};
Paragraph paragraph = new Paragraph();
ParagraphProperties paragraphProperties = new ParagraphProperties();
SectionProperties sectionProperties = new SectionProperties();
// SectionProperties sectionProperties = new SectionProperties() { RsidR = "fff0" };
if (sectionMark != null) {
SectionType sectionType = new SectionType() { Val = sectionMark };
sectionProperties.Append(sectionType);
}
paragraphProperties.Append(sectionProperties);
paragraph.Append(paragraphProperties);
this._document.MainDocumentPart.Document.Body.Append(paragraph);
WordSection wordSection = new WordSection(this, paragraph);
return wordSection;
}
public WordParagraph AddBookmark(string bookmarkName) {
return this.AddParagraph().AddBookmark(bookmarkName);
}
public WordParagraph AddField(WordFieldType wordFieldType, WordFieldFormat? wordFieldFormat = null, bool advanced = false, List<String> parameters = null) {
return this.AddParagraph().AddField(wordFieldType, wordFieldFormat, advanced, parameters);
}
public WordEmbeddedDocument AddEmbeddedDocument(string fileName, AlternativeFormatImportPartType? type = null) {
WordEmbeddedDocument embeddedDocument = new WordEmbeddedDocument(this, fileName, type);
return embeddedDocument;
}
private int CombineRuns(WordHeaderFooter wordHeaderFooter) {
int count = 0;
if (wordHeaderFooter != null) {
foreach (var p in this.Header.Default.Paragraphs) count += CombineIdenticalRuns(p._paragraph);
foreach (var table in this.Header.Default.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
return count;
}
/// <summary>
/// This method will combine identical runs in a paragraph.
/// This is useful when you have a paragraph with multiple runs of the same style, that Microsoft Word creates.
/// This feature is *EXPERIMENTAL* and may not work in all cases.
/// It may impact on how your document looks like, please do extensive testing before using this feature.
/// </summary>
/// <returns></returns>
public int CleanupDocument() {
int count = 0;
foreach (var paragraph in this.Paragraphs) {
count += CombineIdenticalRuns(paragraph._paragraph);
}
foreach (var table in this.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
if (this.Header.Default != null) {
foreach (var p in this.Header.Default.Paragraphs) count += CombineIdenticalRuns(p._paragraph);
foreach (var table in this.Header.Default.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
if (this.Header.Even != null) {
this.Header.Even.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
foreach (var table in this.Header.Even.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
if (this.Header.First != null) {
this.Header.First.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
foreach (var table in this.Header.First.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
if (this.Footer.Default != null) {
this.Footer.Default.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
foreach (var table in this.Footer.Default.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
if (this.Footer.Even != null) {
this.Footer.Even.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
foreach (var table in this.Footer.Even.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
if (this.Footer.First != null) {
this.Footer.First.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
foreach (var table in this.Footer.First.Tables) {
table.Paragraphs.ForEach(p => count += CombineIdenticalRuns(p._paragraph));
}
}
return count;
}
public List<WordParagraph> Find(string text, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
int count = 0;
List<WordParagraph> list = FindAndReplaceInternal(text, "", ref count, false, stringComparison);
return list;
}
/// <summary>
/// FindAdnReplace from the whole doc
/// </summary>
/// <param name="textToFind"></param>
/// <param name="textToReplace"></param>
/// <param name="stringComparison"></param>
/// <returns></returns>
public int FindAndReplace(string textToFind, string textToReplace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
int countFind = 0;
FindAndReplaceInternal(textToFind, textToReplace, ref countFind, true, stringComparison);
return countFind;
}
/// <summary>
/// FindAdnReplace from the range parparagraphs
/// </summary>
/// <param name="paragraphs"></param>
/// <param name="textToFind"></param>
/// <param name="textToReplace"></param>
/// <param name="stringComparison"></param>
/// <returns></returns>
public static int FindAndReplace(List<WordParagraph> paragraphs, string textToFind, string textToReplace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
int countFind = 0;
FindAndReplaceNested(paragraphs, textToFind, textToReplace, ref countFind, true, stringComparison);
return countFind;
}
private static List<WordParagraph> FindAndReplaceNested(List<WordParagraph> paragraphs, string textToFind, string textToReplace, ref int count, bool replace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
List<WordParagraph> foundParagraphs = ReplaceText(paragraphs, textToFind, textToReplace, ref count, replace, stringComparison);
return foundParagraphs;
}
/// <summary>
/// Replace text inside each paragraph
/// </summary>
/// <param name="paragraphs"></param>
/// <param name="oldText"></param>
/// <param name="newText"></param>
/// <param name="count"></param>
/// <param name="replace"></param>
/// <param name="stringComparison"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
private static List<WordParagraph> ReplaceText(List<WordParagraph> paragraphs, string oldText, string newText, ref int count, bool replace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
if (string.IsNullOrEmpty(oldText)) {
throw new ArgumentNullException("oldText should not be null");
}
List<WordParagraph> foundParagraphs = new List<WordParagraph>();
var removeParas = new List<int>();
var foundList = SearchText(paragraphs, oldText, new WordPositionInParagraph() { Paragraph = 0 });
if (foundList?.Count > 0) {
count += foundList.Count;
foreach (var ts in foundList) {
if (ts == null)
continue;
if (ts.BeginIndex == ts.EndIndex) {
var p = paragraphs[ts.BeginIndex];
if (p != null) {
if (replace) {
p.Text = p.Text.Replace(oldText, newText);
}
if (foundParagraphs.IndexOf(p) == -1) {
foundParagraphs.Add(p);
}
}
} else {
if (replace) {
var beginPara = paragraphs[ts.BeginIndex];
var endPara = paragraphs[ts.EndIndex];
if (beginPara != null && endPara != null) {
beginPara.Text = beginPara.Text.Replace(beginPara.Text.Substring(ts.BeginChar), newText);
endPara.Text = endPara.Text.Replace(endPara.Text.Substring(0, ts.EndChar + 1), "");
if (foundParagraphs.IndexOf(beginPara) == -1) {
foundParagraphs.Add(beginPara);
}
}
for (int i = ts.EndIndex - 1; i > ts.BeginIndex; i--) {
removeParas.Add(i);
}
}
}
}
}
if (replace) {
if (removeParas.Count > 0) {
removeParas = removeParas.Distinct().OrderByDescending(i => i).ToList();// Need remove by descending
foreach (var index in removeParas) {
paragraphs[index].Remove();//Remove blank paragraph
}
}
}
return foundParagraphs;
}
private static List<WordTextSegment> SearchText(List<WordParagraph> paragraphs, String searched, WordPositionInParagraph startPos, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
var segList = new List<WordTextSegment>();
int startRun = startPos.Paragraph,
startText = startPos.Text,
startChar = startPos.Char;
int beginRunPos = 0, beginCharPos = 0, candCharPos = 0;
bool newList = false;
for (int runPos = startRun; runPos < paragraphs.Count; runPos++) {
int textPos = 0, charPos = 0;
var p = paragraphs[runPos];
if (!string.IsNullOrEmpty(p.Text)) {
if (textPos >= startText) {
string candidate = p.Text;
if (runPos == startRun)
charPos = startChar;
else
charPos = 0;
for (; charPos < candidate.Length; charPos++) {
if (string.Compare(candidate[charPos].ToString(), searched[0].ToString(), stringComparison) == 0 && (candCharPos == 0)) {
beginCharPos = charPos;
beginRunPos = runPos;
newList = true;
}
if (string.Compare(candidate[charPos].ToString(), searched[candCharPos].ToString(), stringComparison) == 0) {
if (candCharPos + 1 < searched.Length) {
candCharPos++;
} else if (newList) {
WordTextSegment segement = new WordTextSegment();
segement.BeginIndex = (beginRunPos);
segement.BeginChar = (beginCharPos);
segement.EndIndex = (runPos);
segement.EndChar = (charPos);
segList.Add(segement);
//Reset
startChar = charPos;
startText = textPos;
startRun = runPos;
newList = false;
candCharPos = 0;
}
} else
candCharPos = 0;
}
}
textPos++;
}
}
return segList;
}
private List<WordParagraph> FindAndReplaceInternal(string textToFind, string textToReplace, ref int count, bool replace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
WordFind wordFind = new WordFind();
List<WordParagraph> list = new List<WordParagraph>();
list.AddRange(FindAndReplaceNested(this.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
if (this.Header.Default != null) {
list.AddRange(FindAndReplaceNested(this.Header.Default.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Header.Default.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
if (this.Header.Even != null) {
list.AddRange(FindAndReplaceNested(this.Header.Even.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Header.Even.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
if (this.Header.First != null) {
list.AddRange(FindAndReplaceNested(this.Header.First.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Header.First.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
if (this.Footer.Default != null) {
list.AddRange(FindAndReplaceNested(this.Footer.Default.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Footer.Default.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
if (this.Footer.Even != null) {
list.AddRange(FindAndReplaceNested(this.Footer.Even.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Footer.Even.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
if (this.Footer.First != null) {
list.AddRange(FindAndReplaceNested(this.Footer.First.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
foreach (var table in this.Footer.First.Tables) {
list.AddRange(FindAndReplaceNested(table.Paragraphs, textToFind, textToReplace, ref count, replace, stringComparison));
}
}
return list;
}
}
}