-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_BaseClasses.cs
More file actions
310 lines (275 loc) · 11.4 KB
/
_BaseClasses.cs
File metadata and controls
310 lines (275 loc) · 11.4 KB
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
using System;
using System.IO.Packaging;
using System.Linq;
using System.Xml.Linq;
namespace Novacode
{
/// <summary>
/// All DocX types are derived from DocXElement.
/// This class contains properties which every element of a DocX must contain.
/// </summary>
public abstract class DocXElement
{
internal PackagePart mainPart;
public PackagePart PackagePart { get { return mainPart; } set { mainPart = value; } }
/// <summary>
/// This is the actual Xml that gives this element substance.
/// For example, a Paragraphs Xml might look something like the following
/// <p>
/// <r>
/// <t>Hello World!</t>
/// </r>
/// </p>
/// </summary>
public XElement Xml { get; set; }
/// <summary>
/// This is a reference to the DocX object that this element belongs to.
/// Every DocX element is connected to a document.
/// </summary>
internal DocX Document { get; set; }
/// <summary>
/// Store both the document and xml so that they can be accessed by derived types.
/// </summary>
/// <param name="document">The document that this element belongs to.</param>
/// <param name="xml">The Xml that gives this element substance</param>
public DocXElement(DocX document, XElement xml)
{
this.Document = document;
this.Xml = xml;
}
}
/// <summary>
/// This class provides functions for inserting new DocXElements before or after the current DocXElement.
/// Only certain DocXElements can support these functions without creating invalid documents, at the moment these are Paragraphs and Table.
/// </summary>
public abstract class InsertBeforeOrAfter : DocXElement
{
public InsertBeforeOrAfter(DocX document, XElement xml) : base(document, xml) { }
public virtual void InsertPageBreakBeforeSelf()
{
XElement p = new XElement
(
XName.Get("p", DocX.w.NamespaceName),
new XElement
(
XName.Get("r", DocX.w.NamespaceName),
new XElement
(
XName.Get("br", DocX.w.NamespaceName),
new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
)
)
);
Xml.AddBeforeSelf(p);
}
public virtual void InsertPageBreakAfterSelf()
{
XElement p = new XElement
(
XName.Get("p", DocX.w.NamespaceName),
new XElement
(
XName.Get("r", DocX.w.NamespaceName),
new XElement
(
XName.Get("br", DocX.w.NamespaceName),
new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
)
)
);
Xml.AddAfterSelf(p);
}
public virtual Paragraph InsertParagraphBeforeSelf(Paragraph p)
{
Xml.AddBeforeSelf(p.Xml);
XElement newlyInserted = Xml.ElementsBeforeSelf().First();
p.Xml = newlyInserted;
return p;
}
public virtual Paragraph InsertParagraphAfterSelf(Paragraph p)
{
Xml.AddAfterSelf(p.Xml);
XElement newlyInserted = Xml.ElementsAfterSelf().First();
//Dmitchern
if (this as Paragraph != null)
{
return new Paragraph(Document, newlyInserted, (this as Paragraph).endIndex);
}
p.Xml = newlyInserted; //IMPORTANT: I think we have return new paragraph in any case, but I dont know what to put as startIndex parameter into Paragraph constructor
return p;
}
public virtual Paragraph InsertParagraphBeforeSelf(string text)
{
return InsertParagraphBeforeSelf(text, false, new Formatting());
}
public virtual Paragraph InsertParagraphAfterSelf(string text)
{
return InsertParagraphAfterSelf(text, false, new Formatting());
}
public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
{
return InsertParagraphBeforeSelf(text, trackChanges, new Formatting());
}
public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
{
return InsertParagraphAfterSelf(text, trackChanges, new Formatting());
}
public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
Xml.AddBeforeSelf(newParagraph);
XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
return new Paragraph(Document, newlyInserted, -1);
}
public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
Xml.AddAfterSelf(newParagraph);
XElement newlyInserted = Xml.ElementsAfterSelf().First();
Paragraph p = new Paragraph(Document, newlyInserted, -1);
return p;
}
public virtual Table InsertTableAfterSelf(int rowCount, int columnCount)
{
XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount);
Xml.AddAfterSelf(newTable);
XElement newlyInserted = Xml.ElementsAfterSelf().First();
return new Table(Document, newlyInserted) { mainPart = mainPart };
}
public virtual Table InsertTableAfterSelf(Table t)
{
Xml.AddAfterSelf(t.Xml);
XElement newlyInserted = Xml.ElementsAfterSelf().First();
//Dmitchern
return new Table(Document, newlyInserted) { mainPart = mainPart }; //return new table, dont affect parameter table
}
public virtual Table InsertTableBeforeSelf(int rowCount, int columnCount)
{
XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount);
Xml.AddBeforeSelf(newTable);
XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
return new Table(Document, newlyInserted) { mainPart = mainPart };
}
public virtual Table InsertTableBeforeSelf(Table t)
{
Xml.AddBeforeSelf(t.Xml);
XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
//Dmitchern
return new Table(Document, newlyInserted) { mainPart=mainPart}; //return new table, dont affect parameter table
}
}
public static class XmlTemplateBases
{
#region TocXml
public const string TocXmlBase = @"<w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
<w:sdtPr>
<w:docPartObj>
<w:docPartGallery w:val='Table of Contents'/>
<w:docPartUnique/>
</w:docPartObj>\
</w:sdtPr>
<w:sdtEndPr>
<w:rPr>
<w:rFonts w:asciiTheme='minorHAnsi' w:cstheme='minorBidi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>
<w:color w:val='auto'/>
<w:sz w:val='22'/>
<w:szCs w:val='22'/>
<w:lang w:eastAsia='en-US'/>
</w:rPr>
</w:sdtEndPr>
<w:sdtContent>
<w:p>
<w:pPr>
<w:pStyle w:val='{0}'/>
</w:pPr>
<w:r>
<w:t>{1}</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val='TOC1'/>
<w:tabs>
<w:tab w:val='right' w:leader='dot' w:pos='{2}'/>
</w:tabs>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:pPr>
<w:r>
<w:fldChar w:fldCharType='begin' w:dirty='true'/>
</w:r>
<w:r>
<w:instrText xml:space='preserve'> {3} </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType='separate'/>
</w:r>
</w:p>
<w:p>
<w:r>
<w:rPr>
<w:b/>
<w:bCs/>
<w:noProof/>
</w:rPr>
<w:fldChar w:fldCharType='end'/>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
";
public const string TocHeadingStyleBase = @"<w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
<w:name w:val='TOC Heading'/>
<w:basedOn w:val='Heading1'/>
<w:next w:val='Normal'/>
<w:uiPriority w:val='39'/>
<w:semiHidden/>
<w:unhideWhenUsed/>
<w:qFormat/>
<w:rsid w:val='00E67AA6'/>
<w:pPr>
<w:outlineLvl w:val='9'/>
</w:pPr>
<w:rPr>
<w:lang w:eastAsia='nb-NO'/>
</w:rPr>
</w:style>
";
public const string TocElementStyleBase = @" <w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
<w:name w:val='{1}' />
<w:basedOn w:val='Normal' />
<w:next w:val='Normal' />
<w:autoRedefine />
<w:uiPriority w:val='39' />
<w:unhideWhenUsed />
<w:pPr>
<w:spacing w:after='100' />
<w:ind w:left='440' />
</w:pPr>
</w:style>
";
public const string TocHyperLinkStyleBase = @" <w:style w:type='character' w:styleId='Hyperlink' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
<w:name w:val='Hyperlink' />
<w:basedOn w:val='Normal' />
<w:uiPriority w:val='99' />
<w:unhideWhenUsed />
<w:rPr>
<w:color w:val='0000FF' w:themeColor='hyperlink' />
<w:u w:val='single' />
</w:rPr>
</w:style>
";
#endregion
}
}