-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParagraph.cs
More file actions
4698 lines (4221 loc) · 184 KB
/
Paragraph.cs
File metadata and controls
4698 lines (4221 loc) · 184 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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Drawing;
using System.Xml.Linq;
using System.Collections;
using System.IO.Packaging;
using System.Globalization;
using System.Security.Principal;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace Novacode
{
/// <summary>
/// Represents a document paragraph.
/// </summary>
public class Paragraph : InsertBeforeOrAfter
{
// The Append family of functions use this List to apply style.
internal List<XElement> runs;
// This paragraphs text alignment
private Alignment alignment;
public ContainerType ParentContainer;
private XElement ParagraphNumberPropertiesBacker { get; set; }
/// <summary>
/// Fetch the paragraph number properties for a list element.
/// </summary>
public XElement ParagraphNumberProperties
{
get
{
return ParagraphNumberPropertiesBacker ?? (ParagraphNumberPropertiesBacker = GetParagraphNumberProperties());
}
}
private XElement GetParagraphNumberProperties()
{
var numPrNode = Xml.Descendants().FirstOrDefault(el => el.Name.LocalName == "numPr");
if (numPrNode != null)
{
var numIdNode = numPrNode.Descendants().First(numId => numId.Name.LocalName == "numId");
var numIdAttribute = numIdNode.Attribute(DocX.w + "val");
if (numIdAttribute != null && numIdAttribute.Value.Equals("0"))
{
return null;
}
}
return numPrNode;
}
private bool? IsListItemBacker { get; set; }
/// <summary>
/// Determine if this paragraph is a list element.
/// </summary>
public bool IsListItem
{
get
{
IsListItemBacker = IsListItemBacker ?? (ParagraphNumberProperties != null);
return (bool)IsListItemBacker;
}
}
private int? IndentLevelBacker { get; set; }
/// <summary>
/// If this element is a list item, get the indentation level of the list item.
/// </summary>
public int? IndentLevel
{
get
{
if (!IsListItem)
{
return null;
}
return IndentLevelBacker ?? (IndentLevelBacker = int.Parse(ParagraphNumberProperties.Descendants().First(el => el.Name.LocalName == "ilvl").GetAttribute(DocX.w + "val")));
}
}
/// <summary>
/// Determine if the list element is a numbered list of bulleted list element
/// </summary>
public ListItemType ListItemType;
internal int startIndex, endIndex;
/// <summary>
/// Returns a list of all Pictures in a Paragraph.
/// </summary>
/// <example>
/// Returns a list of all Pictures in a Paragraph.
/// <code>
/// <![CDATA[
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Paragraph in a document.
/// Paragraph p = document.Paragraphs[0];
///
/// // Get all of the Pictures in this Paragraph.
/// List<Picture> pictures = p.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// ]]>
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures =
(
from p in Xml.Descendants()
where (p.Name.LocalName == "drawing")
let id =
(
from e in p.Descendants()
where e.Name.LocalName.Equals("blip")
select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
).SingleOrDefault()
where id != null
let img = new Image(Document, mainPart.GetRelationship(id))
select new Picture(Document, p, img)
).ToList();
List<Picture> shapes =
(
from p in Xml.Descendants()
where (p.Name.LocalName == "pict")
let id =
(
from e in p.Descendants()
where e.Name.LocalName.Equals("imagedata")
select e.Attribute(XName.Get("id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
).SingleOrDefault()
where id != null
let img = new Image(Document, mainPart.GetRelationship(id))
select new Picture(Document, p, img)
).ToList();
foreach (Picture p in shapes)
pictures.Add(p);
return pictures;
}
}
/// <summary>
/// Returns a list of Hyperlinks in this Paragraph.
/// </summary>
/// <example>
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Paragraph in this document.
/// Paragraph p = document.Paragraphs[0];
///
/// // Get all of the hyperlinks in this Paragraph.
/// <![CDATA[ List<Hyperlink> ]]> hyperlinks = paragraph.Hyperlinks;
///
/// // Change the first hyperlinks text and Uri
/// Hyperlink h0 = hyperlinks[0];
/// h0.Text = "DocX";
/// h0.Uri = new Uri("http://docx.codeplex.com");
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks = new List<Hyperlink>();
List<XElement> hyperlink_elements =
(
from h in Xml.Descendants()
where (h.Name.LocalName == "hyperlink" || h.Name.LocalName == "instrText")
select h
).ToList();
foreach (XElement he in hyperlink_elements)
{
if (he.Name.LocalName == "hyperlink")
{
try
{
Hyperlink h = new Hyperlink(Document, mainPart, he);
h.mainPart = mainPart;
hyperlinks.Add(h);
}
catch (Exception) { }
}
else
{
// Find the parent run, no matter how deeply nested we are.
XElement e = he;
while (e.Name.LocalName != "r")
e = e.Parent;
// Take every element until we reach w:fldCharType="end"
List<XElement> hyperlink_runs = new List<XElement>();
foreach (XElement r in e.ElementsAfterSelf(XName.Get("r", DocX.w.NamespaceName)))
{
// Add this run to the list.
hyperlink_runs.Add(r);
XElement fldChar = r.Descendants(XName.Get("fldChar", DocX.w.NamespaceName)).SingleOrDefault<XElement>();
if (fldChar != null)
{
XAttribute fldCharType = fldChar.Attribute(XName.Get("fldCharType", DocX.w.NamespaceName));
if (fldCharType != null && fldCharType.Value.Equals("end", StringComparison.CurrentCultureIgnoreCase))
{
try
{
Hyperlink h = new Hyperlink(Document, he, hyperlink_runs);
h.mainPart = mainPart;
hyperlinks.Add(h);
}
catch (Exception) { }
break;
}
}
}
}
}
return hyperlinks;
}
}
///<summary>
/// The style name of the paragraph.
///</summary>
public string StyleName
{
get
{
var element = this.GetOrCreate_pPr();
var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
var attr = styleElement?.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (!string.IsNullOrEmpty(attr?.Value))
{
return attr.Value;
}
return "Normal";
}
set
{
if (string.IsNullOrEmpty(value))
{
value = "Normal";
}
var element = this.GetOrCreate_pPr();
var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
if (styleElement == null)
{
element.Add(new XElement(XName.Get("pStyle", DocX.w.NamespaceName)));
styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
}
styleElement.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), value);
}
}
// A collection of field type DocProperty.
private List<DocProperty> docProperties;
internal List<XElement> styles = new List<XElement>();
/// <summary>
/// Returns a list of field type DocProperty in this document.
/// </summary>
public List<DocProperty> DocumentProperties
{
get { return docProperties; }
}
internal Paragraph(DocX document, XElement xml, int startIndex, ContainerType parent = ContainerType.None)
: base(document, xml)
{
ParentContainer = parent;
this.startIndex = startIndex;
this.endIndex = startIndex + GetElementTextLength(xml);
RebuildDocProperties();
// As per Unused code affecting performance (Wiki Link: [discussion:454191]) and coffeycathal suggestion no longer requeried
//#region It's possible that a Paragraph may have pStyle references
//// Check if this Paragraph references any pStyle elements.
//var stylesElements = xml.Descendants(XName.Get("pStyle", DocX.w.NamespaceName));
//// If one or more pStyles are referenced.
//if (stylesElements.Count() > 0)
//{
// Uri style_package_uri = new Uri("/word/styles.xml", UriKind.Relative);
// PackagePart styles_document = document.package.GetPart(style_package_uri);
// using (TextReader tr = new StreamReader(styles_document.GetStream()))
// {
// XDocument style_document = XDocument.Load(tr);
// XElement styles_element = style_document.Element(XName.Get("styles", DocX.w.NamespaceName));
// var styles_element_ids = stylesElements.Select(e => e.Attribute(XName.Get("val", DocX.w.NamespaceName)).Value);
// //foreach(string id in styles_element_ids)
// //{
// // var style =
// // (
// // from d in styles_element.Descendants()
// // let styleId = d.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
// // let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
// // where type != null && type.Value == "paragraph" && styleId != null && styleId.Value == id
// // select d
// // ).First();
// // styles.Add(style);
// //}
// }
//}
//#endregion
this.runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).ToList();
}
/// <summary>
/// Insert a new Table before this Paragraph, this Table can be from this document or another document.
/// </summary>
/// <param name="t">The Table t to be inserted.</param>
/// <returns>A new Table inserted before this Paragraph.</returns>
/// <example>
/// Insert a new Table before this Paragraph.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Table from document a before this Paragraph.
/// Table newTable = p2.InsertTableBeforeSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public override Table InsertTableBeforeSelf(Table t)
{
t = base.InsertTableBeforeSelf(t);
t.mainPart = mainPart;
return t;
}
private Direction direction;
/// <summary>
/// Gets or Sets the Direction of content in this Paragraph.
/// <example>
/// Create a Paragraph with content that flows right to left. Default is left to right.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph with the text "Hello World".
/// Paragraph p = document.InsertParagraph("Hello World.");
///
/// // Make this Paragraph flow right to left. Default is left to right.
/// p.Direction = Direction.RightToLeft;
///
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
/// </summary>
public Direction Direction
{
get
{
XElement pPr = GetOrCreate_pPr();
XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName));
return bidi == null ? Direction.LeftToRight : Direction.RightToLeft;
}
set
{
direction = value;
XElement pPr = GetOrCreate_pPr();
XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName));
if (direction == Direction.RightToLeft)
{
if (bidi == null)
pPr.Add(new XElement(XName.Get("bidi", DocX.w.NamespaceName)));
}
else
{
bidi?.Remove();
}
}
}
public bool IsKeepWithNext
{
get
{
var pPr = GetOrCreate_pPr();
var keepWithNextE = pPr.Element(XName.Get("keepNext", DocX.w.NamespaceName));
if (keepWithNextE == null)
{
return false;
}
return true;
}
}
/// <summary>
/// This paragraph will be kept on the same page as the next paragraph
/// </summary>
/// <example>
/// Create a Paragraph that will stay on the same page as the paragraph that comes next
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
///
/// {
/// // Create a new Paragraph with the text "Hello World".
/// Paragraph p = document.InsertParagraph("Hello World.");
/// p.KeepWithNext();
/// document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
/// <param name="keepWithNext"></param>
/// <returns></returns>
public Paragraph KeepWithNext(bool keepWithNext = true)
{
var pPr = GetOrCreate_pPr();
var keepWithNextE = pPr.Element(XName.Get("keepNext", DocX.w.NamespaceName));
if (keepWithNextE == null && keepWithNext)
{
pPr.Add(new XElement(XName.Get("keepNext", DocX.w.NamespaceName)));
}
if (!keepWithNext && keepWithNextE != null)
{
keepWithNextE.Remove();
}
return this;
}
/// <summary>
/// Keep all lines in this paragraph together on a page
/// </summary>
/// <example>
/// Create a Paragraph whose lines will stay together on a single page
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph with the text "Hello World".
/// Paragraph p = document.InsertParagraph("All lines of this paragraph will appear on the same page...\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6...");
/// p.KeepLinesTogether();
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
/// <param name="keepTogether"></param>
/// <returns></returns>
public Paragraph KeepLinesTogether(bool keepTogether = true)
{
var pPr = GetOrCreate_pPr();
var keepLinesE = pPr.Element(XName.Get("keepLines", DocX.w.NamespaceName));
if (keepLinesE == null && keepTogether)
{
pPr.Add(new XElement(XName.Get("keepLines", DocX.w.NamespaceName)));
}
if (!keepTogether)
{
keepLinesE?.Remove();
}
return this;
}
/// <summary>
/// If the pPr element doesent exist it is created, either way it is returned by this function.
/// </summary>
/// <returns>The pPr element for this Paragraph.</returns>
internal XElement GetOrCreate_pPr()
{
// Get the element.
XElement pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
// If it dosen't exist, create it.
if (pPr == null)
{
Xml.AddFirst(new XElement(XName.Get("pPr", DocX.w.NamespaceName)));
pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
}
// Return the pPr element for this Paragraph.
return pPr;
}
/// <summary>
/// If the ind element doesent exist it is created, either way it is returned by this function.
/// </summary>
/// <returns>The ind element for this Paragraphs pPr.</returns>
internal XElement GetOrCreate_pPr_ind()
{
// Get the element.
XElement pPr = GetOrCreate_pPr();
XElement ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName));
// If it dosen't exist, create it.
if (ind == null)
{
pPr.Add(new XElement(XName.Get("ind", DocX.w.NamespaceName)));
ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName));
}
// Return the pPr element for this Paragraph.
return ind;
}
private float indentationFirstLine;
/// <summary>
/// Get or set the indentation of the first line of this Paragraph.
/// </summary>
/// <example>
/// Indent only the first line of a Paragraph.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph.
/// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
///
/// // Indent only the first line of the Paragraph.
/// p.IndentationFirstLine = 2.0f;
///
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
public float IndentationFirstLine
{
get
{
GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
if (firstLine != null)
return float.Parse(firstLine.Value);
return 0.0f;
}
set
{
if (IndentationFirstLine != value)
{
indentationFirstLine = value;
XElement pPr = GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
// Paragraph can either be firstLine or hanging (Remove hanging).
XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
hanging?.Remove();
string indentation = ((indentationFirstLine / 0.1) * 57).ToString();
XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
if (firstLine != null)
firstLine.Value = indentation;
else
ind.Add(new XAttribute(XName.Get("firstLine", DocX.w.NamespaceName), indentation));
}
}
}
private float indentationHanging;
/// <summary>
/// Get or set the indentation of all but the first line of this Paragraph.
/// </summary>
/// <example>
/// Indent all but the first line of a Paragraph.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph.
/// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
///
/// // Indent all but the first line of the Paragraph.
/// p.IndentationHanging = 1.0f;
///
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
public float IndentationHanging
{
get
{
GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
if (hanging != null)
return float.Parse(hanging.Value) / (57 * 10);
return 0.0f;
}
set
{
if (IndentationHanging != value)
{
indentationHanging = value;
XElement pPr = GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
// Paragraph can either be firstLine or hanging (Remove firstLine).
XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
if (firstLine != null)
firstLine.Remove();
string indentation = ((indentationHanging / 0.1) * 57).ToString();
XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
if (hanging != null)
hanging.Value = indentation;
else
ind.Add(new XAttribute(XName.Get("hanging", DocX.w.NamespaceName), indentation));
}
}
}
private float indentationBefore;
/// <summary>
/// Set the before indentation in cm for this Paragraph.
/// </summary>
/// <example>
/// // Indent an entire Paragraph from the left.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph.
/// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
///
/// // Indent this entire Paragraph from the left.
/// p.IndentationBefore = 2.0f;
///
/// // Save all changes made to this document.
/// document.Save();
///}
/// </code>
/// </example>
public float IndentationBefore
{
get
{
XElement pPr = GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName));
if (left != null)
return float.Parse(left.Value) / (57 * 10);
return 0.0f;
}
set
{
if (IndentationBefore != value)
{
indentationBefore = value;
XElement pPr = GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
string indentation = ((indentationBefore / 0.1) * 57).ToString(CultureInfo.CurrentCulture);
XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName));
if (left != null)
left.Value = indentation;
else
ind.Add(new XAttribute(XName.Get("left", DocX.w.NamespaceName), indentation));
}
}
}
private float indentationAfter;
/// <summary>
/// Set the after indentation in cm for this Paragraph.
/// </summary>
/// <example>
/// // Indent an entire Paragraph from the right.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph.
/// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
///
/// // Make the content of this Paragraph flow right to left.
/// p.Direction = Direction.RightToLeft;
///
/// // Indent this entire Paragraph from the right.
/// p.IndentationAfter = 2.0f;
///
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
public float IndentationAfter
{
get
{
GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName));
if (right != null)
return float.Parse(right.Value);
return 0.0f;
}
set
{
if (IndentationAfter != value)
{
indentationAfter = value;
XElement pPr = GetOrCreate_pPr();
XElement ind = GetOrCreate_pPr_ind();
string indentation = ((indentationAfter / 0.1) * 57).ToString();
XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName));
if (right != null)
right.Value = indentation;
else
ind.Add(new XAttribute(XName.Get("right", DocX.w.NamespaceName), indentation));
}
}
}
/// <summary>
/// Insert a new Table into this document before this Paragraph.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="columnCount">The number of columns this Table should have.</param>
/// <returns>A new Table inserted before this Paragraph.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("Hello World", false);
///
/// // Insert a new Table before this Paragraph.
/// Table newTable = p.InsertTableBeforeSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public override Table InsertTableBeforeSelf(int rowCount, int columnCount)
{
return base.InsertTableBeforeSelf(rowCount, columnCount);
}
/// <summary>
/// Insert a new Table after this Paragraph.
/// </summary>
/// <param name="t">The Table t to be inserted.</param>
/// <returns>A new Table inserted after this Paragraph.</returns>
/// <example>
/// Insert a new Table after this Paragraph.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Table from document a after this Paragraph.
/// Table newTable = p2.InsertTableAfterSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public override Table InsertTableAfterSelf(Table t)
{
t = base.InsertTableAfterSelf(t);
t.mainPart = mainPart;
return t;
}
/// <summary>
/// Insert a new Table into this document after this Paragraph.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="columnCount">The number of columns this Table should have.</param>
/// <returns>A new Table inserted after this Paragraph.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("Hello World", false);
///
/// // Insert a new Table after this Paragraph.
/// Table newTable = p.InsertTableAfterSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public override Table InsertTableAfterSelf(int rowCount, int columnCount)
{
return base.InsertTableAfterSelf(rowCount, columnCount);
}
/// <summary>
/// Insert a Paragraph before this Paragraph, this Paragraph may have come from the same or another document.
/// </summary>
/// <param name="p">The Paragraph to insert.</param>
/// <returns>The Paragraph now associated with this document.</returns>
/// <example>
/// Take a Paragraph from document a, and insert it into document b before this Paragraph.
/// <code>
/// // Place holder for a Paragraph.
/// Paragraph p;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first paragraph from this document.
/// p = documentA.Paragraphs[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Paragraph from document a before this Paragraph.
/// Paragraph newParagraph = p2.InsertParagraphBeforeSelf(p);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public override Paragraph InsertParagraphBeforeSelf(Paragraph p)
{
Paragraph p2 = base.InsertParagraphBeforeSelf(p);
p2.PackagePart = mainPart;
return p2;
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.");
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public override Paragraph InsertParagraphBeforeSelf(string text)
{
Paragraph p = base.InsertParagraphBeforeSelf(text);
p.PackagePart = mainPart;
return p;
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
{
Paragraph p = base.InsertParagraphBeforeSelf(text, trackChanges);
p.PackagePart = mainPart;
return p;
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <param name="formatting">The formatting to apply to this insertion.</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// Formatting boldFormatting = new Formatting();
/// boldFormatting.Bold = true;
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false, boldFormatting);
///
/// // Save all changes made to this new document.