-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWordSection.PrivateMethods.cs
251 lines (227 loc) · 12.7 KB
/
WordSection.PrivateMethods.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
public partial class WordSection {
internal static List<WordParagraph> ConvertRunsToWordParagraphs(WordDocument document, Run run) {
var list = new List<WordParagraph>();
return list;
}
internal static List<WordTable> ConvertTableToWordTable(WordDocument document, IEnumerable<Table> tables) {
var list = new List<WordTable>();
foreach (Table table in tables) {
list.Add(new WordTable(document, table));
}
return list;
}
internal static List<WordParagraph> ConvertParagraphsToWordParagraphs(WordDocument document, IEnumerable<Paragraph> paragraphs) {
var list = new List<WordParagraph>();
Dictionary<BookmarkStart, WordBookmark> bookmarks = new Dictionary<BookmarkStart, WordBookmark>();
foreach (Paragraph paragraph in paragraphs) {
var childElements = paragraph.ChildElements;
if (childElements.Count == 1 && childElements[0] is ParagraphProperties) {
// basically empty, we still want to track it, but that's about it
list.Add(new WordParagraph(document, paragraph));
} else if (childElements.Any()) {
List<Run> runList = new List<Run>();
bool foundField = false;
foreach (var element in paragraph.ChildElements) {
WordParagraph wordParagraph;
if (element is Run) {
var run = (Run)element;
var fieldChar = run.ChildElements.OfType<FieldChar>().FirstOrDefault();
if (foundField == true) {
if (fieldChar != null && fieldChar.FieldCharType == FieldCharValues.End) {
foundField = false;
runList.Add(run);
wordParagraph = new WordParagraph(document, paragraph, runList);
list.Add(wordParagraph);
} else {
runList.Add(run);
}
} else {
if (fieldChar != null) {
if (fieldChar.FieldCharType == FieldCharValues.Begin) {
runList.Add(run);
foundField = true;
}
} else {
wordParagraph = new WordParagraph(document, paragraph, run);
list.Add(wordParagraph);
}
}
} else if (element is Hyperlink) {
wordParagraph = new WordParagraph(document, paragraph, (Hyperlink)element);
list.Add(wordParagraph);
} else if (element is SimpleField) {
wordParagraph = new WordParagraph(document, paragraph, (SimpleField)element);
list.Add(wordParagraph);
} else if (element is BookmarkStart) {
wordParagraph = new WordParagraph(document, paragraph, (BookmarkStart)element);
list.Add(wordParagraph);
} else if (element is BookmarkEnd) {
// not needed, we will search for matching bookmark end in the bookmark (i guess)
} else if (element is DocumentFormat.OpenXml.Math.OfficeMath) {
wordParagraph = new WordParagraph(document, paragraph, (DocumentFormat.OpenXml.Math.OfficeMath)element);
list.Add(wordParagraph);
} else if (element is DocumentFormat.OpenXml.Math.Paragraph) {
wordParagraph = new WordParagraph(document, paragraph, (DocumentFormat.OpenXml.Math.Paragraph)element);
list.Add(wordParagraph);
} else if (element is SdtRun) {
list.Add(new WordParagraph(document, paragraph, (SdtRun)element));
} else if (element is ProofError) {
} else if (element is ParagraphProperties) {
} else {
Debug.WriteLine("Please implement me! " + element.GetType().Name);
}
}
} else {
// add empty word paragraph
list.Add(new WordParagraph(document, paragraph));
}
}
return list;
}
private List<WordParagraph> GetParagraphsList() {
Dictionary<int, List<Paragraph>> dataSections = new Dictionary<int, List<Paragraph>>();
var count = 0;
dataSections[count] = new List<Paragraph>();
var foundCount = -1;
if (_wordprocessingDocument.MainDocumentPart.Document != null) {
if (_wordprocessingDocument.MainDocumentPart.Document.Body != null) {
var listElements = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements;
foreach (var element in listElements) {
if (element is Paragraph) {
Paragraph paragraph = (Paragraph)element;
if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) {
if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) {
foundCount = count;
}
count++;
dataSections[count] = new List<Paragraph>();
} else {
dataSections[count].Add(paragraph);
}
}
}
if (foundCount < 0) {
var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType<SectionProperties>().FirstOrDefault();
if (sectionProperties == _sectionProperties) {
foundCount = count;
}
}
}
}
return ConvertParagraphsToWordParagraphs(_document, dataSections[foundCount]);
}
/// <summary>
/// This method gets all lists for given document (for all sections)
/// </summary>
/// <param name="document"></param>
/// <returns></returns>
internal static List<WordList> GetAllDocumentsLists(WordDocument document) {
var numbering = document._wordprocessingDocument.MainDocumentPart?.NumberingDefinitionsPart?.Numbering;
if (numbering == null) {
return new List<WordList>(0);
}
return numbering.ChildElements.OfType<NumberingInstance>()
.Select(element => new WordList(document, null, element.NumberID))
.ToList();
}
/// <summary>
/// This method gets lists for given section. It's possible that given list spreads thru multiple sections.
/// Therefore sum of all sections lists doesn't necessary match all lists count for a document.
/// </summary>
/// <returns></returns>
private List<WordList> GetLists() {
List<WordList> allLists = GetAllDocumentsLists(_document);
List<WordList> lists = new List<WordList>();
var usedNumbers = this.ParagraphListItemsNumbers;
foreach (var list in allLists) {
if (usedNumbers.Contains(list._numberId)) {
lists.Add(list);
}
}
return lists;
}
private List<WordTable> GetTablesList() {
Dictionary<int, List<WordTable>> dataSections = new Dictionary<int, List<WordTable>>();
var count = 0;
dataSections[count] = new List<WordTable>();
var foundCount = -1;
if (_wordprocessingDocument.MainDocumentPart.Document != null) {
if (_wordprocessingDocument.MainDocumentPart.Document.Body != null) {
var listElements = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements;
foreach (var element in listElements) {
if (element is Paragraph) {
Paragraph paragraph = (Paragraph)element;
if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) {
if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) {
foundCount = count;
}
count++;
dataSections[count] = new List<WordTable>();
}
} else if (element is Table) {
WordTable wordTable = new WordTable(_document, (Table)element);
dataSections[count].Add(wordTable);
}
}
var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType<SectionProperties>().FirstOrDefault();
if (sectionProperties == _sectionProperties) {
foundCount = count;
}
}
}
return dataSections[foundCount];
}
/// <summary>
/// This method moves headers and footers and title page to section before it.
/// It also copies copies all other parts of sections (PageSize,PageMargin and others) to section before it.
/// This is because headers/footers when applied to section apply to the rest of the document
/// unless there are headers/footers on next section.
/// On the other hand page size doesn't apply to other sections
/// and word uses default values.
/// </summary>
/// <param name="sectionProperties"></param>
/// <param name="newSectionProperties"></param>
private static void CopySectionProperties(SectionProperties sectionProperties, SectionProperties newSectionProperties) {
if (newSectionProperties.ChildElements.Count == 0) {
var listSectionEntries = sectionProperties.ChildElements.ToList();
foreach (var element in listSectionEntries) {
if (element is HeaderReference) {
newSectionProperties.Append(element.CloneNode(true));
sectionProperties.RemoveChild(element);
} else if (element is FooterReference) {
newSectionProperties.Append(element.CloneNode(true));
sectionProperties.RemoveChild(element);
//} else if (element is PageSize) {
// newSectionProperties.Append(element.CloneNode(true));
} else if (element is PageMargin) {
newSectionProperties.Append(element.CloneNode(true));
//sectionProperties.RemoveChild(element);
//} else if (element is Columns) {
// newSectionProperties.Append(element.CloneNode(true));
//} else if (element is DocGrid) {
// newSectionProperties.Append(element.CloneNode(true));
//} else if (element is SectionType) {
// newSectionProperties.Append(element.CloneNode(true));
} else if (element is TitlePage) {
newSectionProperties.Append(element.CloneNode(true));
sectionProperties.RemoveChild(element);
} else {
newSectionProperties.Append(element.CloneNode(true));
//throw new NotImplementedException("This isn't implemented yet?");
}
}
//sectionProperties.RemoveAllChildren();
//newSectionProperties.Append(listSectionEntries);
}
}
}
}