-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFunctions.cs
More file actions
740 lines (637 loc) · 30.6 KB
/
HelperFunctions.cs
File metadata and controls
740 lines (637 loc) · 30.6 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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.IO.Packaging;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Text;
using System.Xml.Linq;
namespace Novacode
{
internal static class HelperFunctions
{
public const string DOCUMENT_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
public const string TEMPLATE_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";
public static bool IsNullOrWhiteSpace(this string value)
{
if (value == null) return true;
return string.IsNullOrEmpty(value.Trim());
}
internal static string GetAuthorName()
{
#if NETFRAMEWORK
return WindowsIdentity.GetCurrent().Name;
#else
return string.Empty;
#endif
}
/// <summary>
/// Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same
/// </summary>
/// <param name="desired"></param>
/// <param name="toCheck"></param>
/// <param name="fo">Matching options whether check if desired attributes are inder a, or a has exactly and only these attributes as b has.</param>
/// <returns></returns>
internal static bool ContainsEveryChildOf(XElement desired, XElement toCheck, MatchFormattingOptions fo)
{
foreach (XElement e in desired.Elements())
{
// If a formatting property has the same name and 'val' attribute's value, its considered to be equivalent.
if (!toCheck.Elements(e.Name).Where(bElement => bElement.GetAttribute(XName.Get("val", DocX.w.NamespaceName)) == e.GetAttribute(XName.Get("val", DocX.w.NamespaceName))).Any())
return false;
}
// If the formatting has to be exact, no additionaly formatting must exist.
if (fo == MatchFormattingOptions.ExactMatch)
return desired.Elements().Count() == toCheck.Elements().Count();
return true;
}
internal static void CreateRelsPackagePart(DocX Document, Uri uri)
{
PackagePart pp = Document.package.CreatePart(uri, DocX.contentTypeApplicationRelationShipXml, CompressionOption.Maximum);
using (TextWriter tw = new StreamWriter(new PackagePartStream(pp.GetStream())))
{
XDocument d = new XDocument
(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement(XName.Get("Relationships", DocX.rel.NamespaceName))
);
var root = d.Root;
d.Save(tw);
}
}
internal static int GetSize(XElement Xml)
{
switch (Xml.Name.LocalName)
{
case "tab":
return 1;
case "br":
return 1;
case "t":
goto case "delText";
case "delText":
return Xml.Value.Length;
case "tr":
goto case "br";
case "tc":
goto case "br";
default:
return 0;
}
}
internal static string GetText(XElement e)
{
StringBuilder sb = new StringBuilder();
GetTextRecursive(e, ref sb);
return sb.ToString();
}
internal static void GetTextRecursive(XElement Xml, ref StringBuilder sb)
{
sb.Append(ToText(Xml));
if (Xml.HasElements)
foreach (XElement e in Xml.Elements())
GetTextRecursive(e, ref sb);
}
internal static List<FormattedText> GetFormattedText(XElement e)
{
List<FormattedText> alist = new List<FormattedText>();
GetFormattedTextRecursive(e, ref alist);
return alist;
}
internal static void GetFormattedTextRecursive(XElement Xml, ref List<FormattedText> alist)
{
FormattedText ft = ToFormattedText(Xml);
FormattedText last = null;
if (ft != null)
{
if (alist.Count() > 0)
last = alist.Last();
if (last != null && last.CompareTo(ft) == 0)
{
// Update text of last entry.
last.text += ft.text;
}
else
{
if (last != null)
ft.index = last.index + last.text.Length;
alist.Add(ft);
}
}
if (Xml.HasElements)
foreach (XElement e in Xml.Elements())
GetFormattedTextRecursive(e, ref alist);
}
internal static FormattedText ToFormattedText(XElement e)
{
// The text representation of e.
String text = ToText(e);
if (text == String.Empty)
return null;
// e is a w:t element, it must exist inside a w:r element or a w:tabs, lets climb until we find it.
while (!e.Name.Equals(XName.Get("r", DocX.w.NamespaceName)) &&
!e.Name.Equals(XName.Get("tabs", DocX.w.NamespaceName)))
e = e.Parent;
// e is a w:r element, lets find the rPr element.
XElement rPr = e.Element(XName.Get("rPr", DocX.w.NamespaceName));
FormattedText ft = new FormattedText();
ft.text = text;
ft.index = 0;
ft.formatting = null;
// Return text with formatting.
if (rPr != null)
ft.formatting = Formatting.Parse(rPr);
return ft;
}
internal static string ToText(XElement e)
{
switch (e.Name.LocalName)
{
case "tab":
return "\t";
case "br":
return "\n";
case "t":
goto case "delText";
case "delText":
{
if (e.Parent != null && e.Parent.Name.LocalName == "r")
{
XElement run = e.Parent;
var rPr = run.Elements().FirstOrDefault(a => a.Name.LocalName == "rPr");
if (rPr != null)
{
var caps = rPr.Elements().FirstOrDefault(a => a.Name.LocalName == "caps");
if (caps != null)
return e.Value.ToUpper();
}
}
return e.Value;
}
case "tr":
goto case "br";
case "tc":
goto case "tab";
default: return "";
}
}
internal static XElement CloneElement(XElement element)
{
return new XElement
(
element.Name,
element.Attributes(),
element.Nodes().Select
(
n =>
{
XElement e = n as XElement;
if (e != null)
return CloneElement(e);
return n;
}
)
);
}
internal static PackagePart CreateOrGetSettingsPart(Package package)
{
PackagePart settingsPart;
Uri settingsUri = new Uri("/word/settings.xml", UriKind.Relative);
if (!package.PartExists(settingsUri))
{
settingsPart = package.CreatePart(settingsUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", CompressionOption.Maximum);
PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) ||
p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase));
mainDocumentPart.CreateRelationship(settingsUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings");
XDocument settings = XDocument.Parse
(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<w:settings xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w10='urn:schemas-microsoft-com:office:word' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' xmlns:sl='http://schemas.openxmlformats.org/schemaLibrary/2006/main'>
<w:zoom w:percent='100' />
<w:defaultTabStop w:val='720' />
<w:characterSpacingControl w:val='doNotCompress' />
<w:compat />
<w:rsids>
<w:rsidRoot w:val='00217F62' />
<w:rsid w:val='001915A3' />
<w:rsid w:val='00217F62' />
<w:rsid w:val='00A906D8' />
<w:rsid w:val='00AB5A74' />
<w:rsid w:val='00F071AE' />
</w:rsids>
<m:mathPr>
<m:mathFont m:val='Cambria Math' />
<m:brkBin m:val='before' />
<m:brkBinSub m:val='--' />
<m:smallFrac m:val='off' />
<m:dispDef />
<m:lMargin m:val='0' />
<m:rMargin m:val='0' />
<m:defJc m:val='centerGroup' />
<m:wrapIndent m:val='1440' />
<m:intLim m:val='subSup' />
<m:naryLim m:val='undOvr' />
</m:mathPr>
<w:themeFontLang w:val='en-IE' w:bidi='ar-SA' />
<w:clrSchemeMapping w:bg1='light1' w:t1='dark1' w:bg2='light2' w:t2='dark2' w:accent1='accent1' w:accent2='accent2' w:accent3='accent3' w:accent4='accent4' w:accent5='accent5' w:accent6='accent6' w:hyperlink='hyperlink' w:followedHyperlink='followedHyperlink' />
<w:shapeDefaults>
<o:shapedefaults v:ext='edit' spidmax='2050' />
<o:shapelayout v:ext='edit'>
<o:idmap v:ext='edit' data='1' />
</o:shapelayout>
</w:shapeDefaults>
<w:decimalSymbol w:val='.' />
<w:listSeparator w:val=',' />
</w:settings>"
);
XElement themeFontLang = settings.Root.Element(XName.Get("themeFontLang", DocX.w.NamespaceName));
themeFontLang.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture);
// Save the settings document.
using (TextWriter tw = new StreamWriter(new PackagePartStream(settingsPart.GetStream())))
settings.Save(tw);
}
else
settingsPart = package.GetPart(settingsUri);
return settingsPart;
}
internal static void CreateCustomPropertiesPart(DocX document)
{
PackagePart customPropertiesPart = document.package.CreatePart(new Uri("/docProps/custom.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.custom-properties+xml", CompressionOption.Maximum);
XDocument customPropDoc = new XDocument
(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement
(
XName.Get("Properties", DocX.customPropertiesSchema.NamespaceName),
new XAttribute(XNamespace.Xmlns + "vt", DocX.customVTypesSchema)
)
);
using (TextWriter tw = new StreamWriter(new PackagePartStream(customPropertiesPart.GetStream(FileMode.Create, FileAccess.Write))))
customPropDoc.Save(tw, SaveOptions.None);
document.package.CreateRelationship(customPropertiesPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
}
internal static XDocument DecompressXMLResource(string manifest_resource_name)
{
// XDocument to load the compressed Xml resource into.
XDocument document;
// Get a reference to the executing assembly.
//Assembly assembly = Assembly.GetEntryAssembly();
Assembly assembly = typeof(HelperFunctions).GetTypeInfo().Assembly;
// Open a Stream to the embedded resource.
Stream stream = assembly.GetManifestResourceStream(manifest_resource_name.Replace("Novacode", "DocXCore"));
int i = 0;
foreach (string resource in assembly.GetManifestResourceNames())
i += 1;
// Decompress the embedded resource.
using (GZipStream zip = new GZipStream(stream, CompressionMode.Decompress))
{
// Load this decompressed embedded resource into an XDocument using a TextReader.
using (TextReader sr = new StreamReader(zip))
{
document = XDocument.Load(sr);
}
}
// Return the decompressed Xml as an XDocument.
return document;
}
/// <summary>
/// If this document does not contain a /word/numbering.xml add the default one generated by Microsoft Word
/// when the default bullet, numbered and multilevel lists are added to a blank document
/// </summary>
/// <param name="package"></param>
/// <returns></returns>
internal static XDocument AddDefaultNumberingXml(Package package)
{
XDocument numberingDoc;
// Create the main document part for this package
PackagePart wordNumbering = package.CreatePart(new Uri("/word/numbering.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", CompressionOption.Maximum);
numberingDoc = DecompressXMLResource("Novacode.Resources.numbering.xml.gz");
// Save /word/numbering.xml
using (TextWriter tw = new StreamWriter(new PackagePartStream(wordNumbering.GetStream(FileMode.Create, FileAccess.Write))))
numberingDoc.Save(tw, SaveOptions.None);
PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) ||
p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase));
mainDocumentPart.CreateRelationship(wordNumbering.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering");
return numberingDoc;
}
/// <summary>
/// If this document does not contain a /word/styles.xml add the default one generated by Microsoft Word.
/// </summary>
/// <param name="package"></param>
/// <returns></returns>
internal static XDocument AddDefaultStylesXml(Package package)
{
XDocument stylesDoc;
// Create the main document part for this package
PackagePart word_styles = package.CreatePart(new Uri("/word/styles.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", CompressionOption.Maximum);
stylesDoc = HelperFunctions.DecompressXMLResource("Novacode.Resources.default_styles.xml.gz");
XElement lang = stylesDoc.Root.Element(XName.Get("docDefaults", DocX.w.NamespaceName)).Element(XName.Get("rPrDefault", DocX.w.NamespaceName)).Element(XName.Get("rPr", DocX.w.NamespaceName)).Element(XName.Get("lang", DocX.w.NamespaceName));
lang.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture);
// Save /word/styles.xml
using (TextWriter tw = new StreamWriter(new PackagePartStream(word_styles.GetStream(FileMode.Create, FileAccess.Write))))
stylesDoc.Save(tw, SaveOptions.None);
PackagePart mainDocumentPart = package.GetParts().Where
(
p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)||p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)
).Single();
mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles");
return stylesDoc;
}
internal static XElement CreateEdit(EditType t, DateTime edit_time, object content)
{
if (t == EditType.del)
{
foreach (object o in (IEnumerable<XElement>)content)
{
if (o is XElement)
{
XElement e = (o as XElement);
IEnumerable<XElement> ts = e.DescendantsAndSelf(XName.Get("t", DocX.w.NamespaceName));
for (int i = 0; i < ts.Count(); i++)
{
XElement text = ts.ElementAt(i);
text.ReplaceWith(new XElement(DocX.w + "delText", text.Attributes(), text.Value));
}
}
}
}
return
(
new XElement(DocX.w + t.ToString(),
new XAttribute(DocX.w + "id", 0),
new XAttribute(DocX.w + "author", GetAuthorName()),
new XAttribute(DocX.w + "date", edit_time),
content)
);
}
internal static XElement CreateTable(int rowCount, int columnCount)
{
int[] columnWidths = new int[columnCount];
for (int i = 0; i < columnCount; i++)
{
columnWidths[i] = 2310;
}
return CreateTable(rowCount, columnWidths);
}
internal static XElement CreateTable(int rowCount, int[] columnWidths)
{
XElement newTable =
new XElement
(
XName.Get("tbl", DocX.w.NamespaceName),
new XElement
(
XName.Get("tblPr", DocX.w.NamespaceName),
new XElement(XName.Get("tblStyle", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "TableGrid")),
new XElement(XName.Get("tblW", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), "5000"), new XAttribute(XName.Get("type", DocX.w.NamespaceName), "auto")),
new XElement(XName.Get("tblLook", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "04A0"))
)
);
/*XElement tableGrid = new XElement(XName.Get("tblGrid", DocX.w.NamespaceName));
for (int i = 0; i < columnWidths.Length; i++)
tableGrid.Add(new XElement(XName.Get("gridCol", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), XmlConvert.ToString(columnWidths[i]))));
newTable.Add(tableGrid);*/
for (int i = 0; i < rowCount; i++)
{
XElement row = new XElement(XName.Get("tr", DocX.w.NamespaceName));
for (int j = 0; j < columnWidths.Length; j++)
{
XElement cell = CreateTableCell();
row.Add(cell);
}
newTable.Add(row);
}
return newTable;
}
/// <summary>
/// Create and return a cell of a table
/// </summary>
internal static XElement CreateTableCell(double w = 2310)
{
return new XElement
(
XName.Get("tc", DocX.w.NamespaceName),
new XElement(XName.Get("tcPr", DocX.w.NamespaceName),
new XElement(XName.Get("tcW", DocX.w.NamespaceName),
new XAttribute(XName.Get("w", DocX.w.NamespaceName), w),
new XAttribute(XName.Get("type", DocX.w.NamespaceName), "dxa"))),
new XElement(XName.Get("p", DocX.w.NamespaceName),
new XElement(XName.Get("pPr", DocX.w.NamespaceName)))
);
}
internal static List CreateItemInList(List list, string listText, int level = 0, ListItemType listType = ListItemType.Numbered, int? startNumber = null, bool trackChanges = false, bool continueNumbering = false)
{
if (list.NumId == 0)
{
list.CreateNewNumberingNumId(level, listType, startNumber, continueNumbering);
}
if (listText != null) //I see no reason why you shouldn't be able to insert an empty element. It simplifies tasks such as populating an item from html.
{
var newParagraphSection = new XElement
(
XName.Get("p", DocX.w.NamespaceName),
new XElement(XName.Get("pPr", DocX.w.NamespaceName),
new XElement(XName.Get("numPr", DocX.w.NamespaceName),
new XElement(XName.Get("ilvl", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", level)),
new XElement(XName.Get("numId", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", list.NumId)))),
new XElement(XName.Get("r", DocX.w.NamespaceName), new XElement(XName.Get("t", DocX.w.NamespaceName), listText))
);
if (trackChanges)
newParagraphSection = CreateEdit(EditType.ins, DateTime.Now, newParagraphSection);
if (startNumber == null)
{
list.AddItem(new Paragraph(list.Document, newParagraphSection, 0, ContainerType.Paragraph));
}
else
{
list.AddItemWithStartValue(new Paragraph(list.Document, newParagraphSection, 0, ContainerType.Paragraph), (int)startNumber);
}
}
return list;
}
internal static void RenumberIDs(DocX document)
{
IEnumerable<XAttribute> trackerIDs =
(from d in document.mainDoc.Descendants()
where d.Name.LocalName == "ins" || d.Name.LocalName == "del"
select d.Attribute(XName.Get("id", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")));
for (int i = 0; i < trackerIDs.Count(); i++)
trackerIDs.ElementAt(i).Value = i.ToString();
}
internal static Paragraph GetFirstParagraphEffectedByInsert(DocX document, int index)
{
// This document contains no Paragraphs and insertion is at index 0
if (document.Paragraphs.Count() == 0 && index == 0)
return null;
foreach (Paragraph p in document.Paragraphs)
{
if (p.endIndex >= index)
return p;
}
throw new ArgumentOutOfRangeException();
}
internal static List<XElement> FormatInput(string text, XElement rPr)
{
List<XElement> newRuns = new List<XElement>();
XElement tabRun = new XElement(DocX.w + "tab");
XElement breakRun = new XElement(DocX.w + "br");
StringBuilder sb = new StringBuilder();
if (string.IsNullOrEmpty(text))
{
return newRuns; //I dont wanna get an exception if text == null, so just return empy list
}
char lastChar = '\0';
foreach (char c in text)
{
switch (c)
{
case '\t':
if (sb.Length > 0)
{
XElement t = new XElement(DocX.w + "t", sb.ToString());
Novacode.Text.PreserveSpace(t);
newRuns.Add(new XElement(DocX.w + "r", rPr, t));
sb = new StringBuilder();
}
newRuns.Add(new XElement(DocX.w + "r", rPr, tabRun));
break;
case '\r':
if (sb.Length > 0)
{
XElement t = new XElement(DocX.w + "t", sb.ToString());
Novacode.Text.PreserveSpace(t);
newRuns.Add(new XElement(DocX.w + "r", rPr, t));
sb = new StringBuilder();
}
newRuns.Add(new XElement(DocX.w + "r", rPr, breakRun));
break;
case '\n':
if (lastChar == '\r') break;
if (sb.Length > 0)
{
XElement t = new XElement(DocX.w + "t", sb.ToString());
Novacode.Text.PreserveSpace(t);
newRuns.Add(new XElement(DocX.w + "r", rPr, t));
sb = new StringBuilder();
}
newRuns.Add(new XElement(DocX.w + "r", rPr, breakRun));
break;
default:
sb.Append(c);
break;
}
lastChar = c;
}
if (sb.Length > 0)
{
XElement t = new XElement(DocX.w + "t", sb.ToString());
Novacode.Text.PreserveSpace(t);
newRuns.Add(new XElement(DocX.w + "r", rPr, t));
}
return newRuns;
}
internal static XElement[] SplitParagraph(Paragraph p, int index)
{
// In this case edit dosent really matter, you have a choice.
Run r = p.GetFirstRunEffectedByEdit(index, EditType.ins);
XElement[] split;
XElement before, after;
if (r.Xml.Parent.Name.LocalName == "ins")
{
split = p.SplitEdit(r.Xml.Parent, index, EditType.ins);
before = new XElement(p.Xml.Name, p.Xml.Attributes(), r.Xml.Parent.ElementsBeforeSelf(), split[0]);
after = new XElement(p.Xml.Name, p.Xml.Attributes(), r.Xml.Parent.ElementsAfterSelf(), split[1]);
}
else if (r.Xml.Parent.Name.LocalName == "del")
{
split = p.SplitEdit(r.Xml.Parent, index, EditType.del);
before = new XElement(p.Xml.Name, p.Xml.Attributes(), r.Xml.Parent.ElementsBeforeSelf(), split[0]);
after = new XElement(p.Xml.Name, p.Xml.Attributes(), r.Xml.Parent.ElementsAfterSelf(), split[1]);
}
else
{
split = Run.SplitRun(r, index);
before = new XElement(p.Xml.Name, p.Xml.Attributes(), r.Xml.ElementsBeforeSelf(), split[0]);
after = new XElement(p.Xml.Name, p.Xml.Attributes(), split[1], r.Xml.ElementsAfterSelf());
}
if (before.Elements().Count() == 0)
before = null;
if (after.Elements().Count() == 0)
after = null;
return new XElement[] { before, after };
}
/// <!--
/// Bug found and fixed by trnilse. To see the change,
/// please compare this release to the previous release using TFS compare.
/// -->
internal static bool IsSameFile(Stream streamOne, Stream streamTwo)
{
int file1byte, file2byte;
if (streamOne.Length != streamTwo.Length)
{
// Return false to indicate files are different
return false;
}
// Read and compare a byte from each file until either a
// non-matching set of bytes is found or until the end of
// file1 is reached.
do
{
// Read one byte from each file.
file1byte = streamOne.ReadByte();
file2byte = streamTwo.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
// Return the success of the comparison. "file1byte" is
// equal to "file2byte" at this point only if the files are
// the same.
streamOne.Position = 0;
streamTwo.Position = 0;
return ((file1byte - file2byte) == 0);
}
internal static UnderlineStyle GetUnderlineStyle(string underlineStyle)
{
switch (underlineStyle)
{
case "single":
return UnderlineStyle.singleLine;
case "double":
return UnderlineStyle.doubleLine;
case "thick":
return UnderlineStyle.thick;
case "dotted":
return UnderlineStyle.dotted;
case "dottedHeavy":
return UnderlineStyle.dottedHeavy;
case "dash":
return UnderlineStyle.dash;
case "dashedHeavy":
return UnderlineStyle.dashedHeavy;
case "dashLong":
return UnderlineStyle.dashLong;
case "dashLongHeavy":
return UnderlineStyle.dashLongHeavy;
case "dotDash":
return UnderlineStyle.dotDash;
case "dashDotHeavy":
return UnderlineStyle.dashDotHeavy;
case "dotDotDash":
return UnderlineStyle.dotDotDash;
case "dashDotDotHeavy":
return UnderlineStyle.dashDotDotHeavy;
case "wave":
return UnderlineStyle.wave;
case "wavyHeavy":
return UnderlineStyle.wavyHeavy;
case "wavyDouble":
return UnderlineStyle.wavyDouble;
case "words":
return UnderlineStyle.words;
default:
return UnderlineStyle.none;
}
}
}
}