-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFileFormat.Words.IElements.cs
869 lines (745 loc) · 24.4 KB
/
FileFormat.Words.IElements.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
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
using System.Collections.Generic;
using System.Linq;
namespace FileFormat.Words.IElements
{
/// <summary>
/// Represents an element in a Word document.
/// </summary>
public interface IElement
{
/// <summary>
/// Gets the unique identifier of the element.
/// </summary>
int ElementId { get; }
}
/// <summary>
/// Represents the indentation settings for a paragraph.
/// </summary>
public class Indentation
{
/// <summary>
/// Gets or sets the distance of the left indentation.
/// </summary>
public double Left { get; set; }
/// <summary>
/// Gets or sets the distance of the right indentation.
/// </summary>
public double Right { get; set; }
/// <summary>
/// Gets or sets the distance of the first line indentation.
/// </summary>
public double FirstLine { get; set; }
/// <summary>
/// Gets or sets the distance of the hanging indentation.
/// </summary>
public double Hanging { get; set; }
//public Indentation()
//{
//Left = 0;
//}
}
/// <summary>
/// Specifies the alignment of a paragraph within a text block or document.
/// </summary>
public enum ParagraphAlignment
{
/// <summary>
/// Aligns the paragraph to the left.
/// </summary>
Left,
/// <summary>
/// Centers the paragraph within the available space.
/// </summary>
Center,
/// <summary>
/// Aligns the paragraph to the right.
/// </summary>
Right,
/// <summary>
/// Justifies the text within the paragraph, aligning both the left and right edges.
/// </summary>
Justify
}
/// <summary>
/// Specifies the border width of an element within a text block or document.
/// </summary>
public enum BorderWidth
{
/// <summary>
/// Single width border/frame.
/// </summary>
Single,
/// <summary>
/// Double width border/frame.
/// </summary>
Double,
/// <summary>
/// Dotted style border/frame.
/// </summary>
Dotted,
/// <summary>
/// DotDash style border/frame.
/// </summary>
DotDash
}
/// <summary>
/// Represents border/frame of an element within word document.
/// </summary>
public class Border
{
/// <summary>
/// Gets or sets the border width.
/// </summary>
public BorderWidth Width { get; set; }
/// <summary>
/// Gets or sets the border color.
/// </summary>
public string Color { get; set; }
/// <summary>
/// Gets or sets the border color.
/// </summary>
public int Size { get; set; }
/// <summary>
/// Constructor for border.
/// </summary>
public Border()
{
Size = 0;
}
}
/// <summary>
/// Represents a paragraph element in a Word document.
/// </summary>
public class Paragraph : IElement
{
/// <summary>
/// Gets the unique identifier of the paragraph.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets or sets the text content of the paragraph.
/// </summary>
public string Text { get; private set; }
/// <summary>
/// Gets the list of runs (text fragments) within the paragraph.
/// </summary>
public List<Run> Runs { get; }
/// <summary>
/// Gets or sets the style of the paragraph.
/// </summary>
public string Style { get; set; }
/// <summary>
/// Gets or Sets Alignment of the word paragraph
/// </summary>
public ParagraphAlignment Alignment { get; set; }
/// <summary>
/// Gets or Sets Indentation of the word paragraph
/// </summary>
public Indentation Indentation { get; set; }
/// <summary>
/// Gets or sets the numbering ID for the paragraph.
/// </summary>
public int? NumberingId { get; set; }
/// <summary>
/// Gets or sets the numbering level for the paragraph.
/// </summary>
public int? NumberingLevel { get; set; }
/// <summary>
/// Gets or sets whether the paragraph has bullet points.
/// </summary>
public bool IsBullet { get; set; }
/// <summary>
/// Gets or sets whether the paragraph has numbered bullets.
/// </summary>
public bool IsNumbered { get; set; }
/// <summary>
/// Gets or sets whether the paragraph has roman number bullets.
/// </summary>
public bool IsRoman { get; set; }
/// <summary>
/// Gets or sets whether the paragraph has alphabetic number bullets.
/// </summary>
public bool IsAlphabeticNumber { get; set; }
/// <summary>
/// Gets or sets the paragraph border.
/// </summary>
public Border ParagraphBorder { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Paragraph"/> class.
/// </summary>
public Paragraph()
{
Runs = new List<Run>();
Style = "Normal";
ParagraphBorder = new Border();
Indentation = new Indentation();
NumberingId = null;
NumberingLevel = null;
IsBullet = false;
IsNumbered = false;
UpdateText(); // Initialize the Text property
}
/// <summary>
/// Adds a run (text fragment) to the paragraph and sets its parent paragraph.
/// </summary>
/// <param name="run">The run to add to the paragraph.</param>
public void AddRun(Run run)
{
run.ParentParagraph = this;
Runs.Add(run);
UpdateText(); // Update the Text property when a new run is added
}
internal void UpdateText()
{
Text = string.Join("", Runs.Select(run => run.Text));
}
}
/// <summary>
/// Provides predefined heading styles.
/// </summary>
public static class Headings
{
/// <summary>
/// Gets the value representing Heading1.
/// </summary>
public static string Heading1 { get; } = "Heading1";
/// <summary>
/// Gets the value representing Heading2.
/// </summary>
public static string Heading2 { get; } = "Heading2";
/// <summary>
/// Gets the value representing Heading3.
/// </summary>
public static string Heading3 { get; } = "Heading3";
/// <summary>
/// Gets the value representing Heading4.
/// </summary>
public static string Heading4 { get; } = "Heading4";
/// <summary>
/// Gets the value representing Heading5.
/// </summary>
public static string Heading5 { get; } = "Heading5";
/// <summary>
/// Gets the value representing Heading6.
/// </summary>
public static string Heading6 { get; } = "Heading6";
/// <summary>
/// Gets the value representing Heading7.
/// </summary>
public static string Heading7 { get; } = "Heading7";
/// <summary>
/// Gets the value representing Heading8.
/// </summary>
public static string Heading8 { get; } = "Heading8";
/// <summary>
/// Gets the value representing Heading9.
/// </summary>
public static string Heading9 { get; } = "Heading9";
}
/// <summary>
/// Represents a run of text within a paragraph.
/// </summary>
public class Run
{
private string _text;
/// <summary>
/// Gets or sets the text content of the run.
/// </summary>
public string Text
{
get => _text;
set
{
_text = value;
if (ParentParagraph != null)
{
ParentParagraph.UpdateText();
}
}
}
/// <summary>
/// Gets or sets the font family of the run.
/// </summary>
public string FontFamily { get; set; }
/// <summary>
/// Gets or sets the font size of the run.
/// </summary>
public int FontSize { get; set; }
/// <summary>
/// Gets or sets the color of the run's text.
/// </summary>
public string Color { get; set; }
/// <summary>
/// Gets or sets whether the run's text is bold.
/// </summary>
public bool Bold { get; set; }
/// <summary>
/// Gets or sets whether the run's text is italic.
/// </summary>
public bool Italic { get; set; }
/// <summary>
/// Gets or sets whether the run's text is underlined.
/// </summary>
public bool Underline { get; set; }
internal Paragraph ParentParagraph { get; set; }
}
/// <summary>
/// Provides predefined colors with hexadecimal values.
/// </summary>
public static class Colors
{
/// <summary>
/// Gets the hexadecimal value for the color Black (000000).
/// </summary>
public static string Black { get; } = "000000";
/// <summary>
/// Gets the hexadecimal value for the color White (FFFFFF).
/// </summary>
public static string White { get; } = "FFFFFF";
/// <summary>
/// Gets the hexadecimal value for the color Red (FF0000).
/// </summary>
public static string Red { get; } = "FF0000";
/// <summary>
/// Gets the hexadecimal value for the color Green (00FF00).
/// </summary>
public static string Green { get; } = "00FF00";
/// <summary>
/// Gets the hexadecimal value for the color Blue (0000FF).
/// </summary>
public static string Blue { get; } = "0000FF";
/// <summary>
/// Gets the hexadecimal value for the color Yellow (FFFF00).
/// </summary>
public static string Yellow { get; } = "FFFF00";
/// <summary>
/// Gets the hexadecimal value for the color Cyan (00FFFF).
/// </summary>
public static string Cyan { get; } = "00FFFF";
/// <summary>
/// Gets the hexadecimal value for the color Magenta (FF00FF).
/// </summary>
public static string Magenta { get; } = "FF00FF";
/// <summary>
/// Gets the hexadecimal value for the color Gray (808080).
/// </summary>
public static string Gray { get; } = "808080";
/// <summary>
/// Gets the hexadecimal value for the color Silver (C0C0C0).
/// </summary>
public static string Silver { get; } = "C0C0C0";
/// <summary>
/// Gets the hexadecimal value for the color Maroon (800000).
/// </summary>
public static string Maroon { get; } = "800000";
/// <summary>
/// Gets the hexadecimal value for the color Olive (808000).
/// </summary>
public static string Olive { get; } = "808000";
/// <summary>
/// Gets the hexadecimal value for the color Green (008000).
/// </summary>
public static string Teal { get; } = "008000";
/// <summary>
/// Gets the hexadecimal value for the color Navy (000080).
/// </summary>
public static string Navy { get; } = "000080";
/// <summary>
/// Gets the hexadecimal value for the color Purple (800080).
/// </summary>
public static string Purple { get; } = "800080";
/// <summary>
/// Gets the hexadecimal value for the color Orange (FFA500).
/// </summary>
public static string Orange { get; } = "FFA500";
/// <summary>
/// Gets the hexadecimal value for the color Lime (00FF00).
/// </summary>
public static string Lime { get; } = "00FF00";
/// <summary>
/// Gets the hexadecimal value for the color Aqua (00FFFF).
/// </summary>
public static string Aqua { get; } = "00FFFF";
/// <summary>
/// Gets the hexadecimal value for the color Fuchsia (FF00FF).
/// </summary>
public static string Fuchsia { get; } = "FF00FF";
/// <summary>
/// Gets the hexadecimal value for the color Silver (C0C0C0).
/// </summary>
public static string LimeGreen { get; } = "32CD32";
}
/// <summary>
/// Represents an image element in a Word document.
/// </summary>
public class Image : IElement
{
/// <summary>
/// Gets the unique identifier of the image.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets or sets the binary image data.
/// </summary>
public byte[] ImageData { get; set; }
/// <summary>
/// Gets or sets the height of the image.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Gets or sets the width of the image.
/// </summary>
public int Width { get; set; }
}
/// <summary>
/// Represents a shape element in a Word document.
/// </summary>
public class Shape : IElement
{
/// <summary>
/// Gets the unique identifier of the shape.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets or sets the x position of the shape.
/// </summary>
public int X { get; set; }
/// <summary>
/// Gets or sets the y position of the shape.
/// </summary>
public int Y { get; set; }
/// <summary>
/// Gets or sets the height of the shape.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Gets or sets the width of the shape.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Gets or sets the type of the shape.
/// </summary>
public ShapeType Type { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Shape"/> class.
/// </summary>
public Shape()
{
X = 100;
Y = 100;
Width = 200;
Height = 200;
Type = ShapeType.Ellipse;
}
/// <summary>
/// Initializes a new instance of the <see cref="Shape"/> class with specified values.
/// </summary>
/// <param name="x">x position of the shape.</param>
/// <param name="y">y position of the shape.</param>
/// <param name="width">Width of the shape.</param>
/// <param name="height">Height of the shape.</param>
/// <param name="shapeType">Type of the shape (e.g rectangle, ellipse etc).</param>
public Shape(int x,int y,int width,int height,ShapeType shapeType)
{
X = x;
Y = y;
Width = width;
Height = height;
Type = shapeType;
}
}
/// <summary>
/// Represents a grouped shapes element in a Word document.
/// </summary>
public class GroupShape : IElement
{
/// <summary>
/// Gets the unique identifier of the group shape.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets or sets the type of the first shape in the group.
/// </summary>
public Shape Shape1 { get; set; }
/// <summary>
/// Gets or sets the type of the second shape in the group.
/// </summary>
public Shape Shape2 { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="GroupShape"/> class.
/// </summary>
public GroupShape()
{
Shape1 = new Shape();
Shape2 = new Shape();
}
/// <summary>
/// Initializes a new instance of the <see cref="GroupShape"/> class with specified values.
/// </summary>
/// <param name="shape1">Firs shape in the group.</param>
/// <param name="shape2">Second shape in the group.</param>
public GroupShape(Shape shape1,Shape shape2)
{
Shape1 = shape1;
//Shape1.ElementId = ElementId + 1;
Shape2 = shape2;
//Shape2.ElementId = ElementId + 2;
}
}
/// <summary>
/// Specifies the type of a shape within the word document.
/// </summary>
public enum ShapeType
{
/// <summary>
/// Rectangle shape.
/// </summary>
Rectangle,
/// <summary>
/// Triangle shape.
/// </summary>
Triangle,
/// <summary>
/// Ellipse or Oval shape.
/// </summary>
Ellipse,
/// <summary>
/// Diamond shape.
/// </summary>
Diamond,
/// <summary>
/// Hexagone shape.
/// </summary>
Hexagone
}
/// <summary>
/// Represents a table element in a Word document.
/// </summary>
public class Table : IElement
{
/// <summary>
/// Gets the unique identifier of the table.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets or sets the table style.
/// </summary>
public string Style { get; set; }
/// <summary>
/// Gets or sets the list of rows within the table.
/// </summary>
public List<Row> Rows { get; set; }
/// <summary>
/// Gets or sets the column properties of the table.
/// </summary>
public Column Column { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Table"/> class with empty rows and default column properties.
/// </summary>
public Table()
{
Rows = new List<Row>();
Column = new Column();
Style = "TableGrid";
}
/// <summary>
/// Initializes a new instance of the <see cref="Table"/> class with a specified number of rows and columns.
/// </summary>
/// <param name="rows">The number of rows in the table.</param>
/// <param name="columns">The number of columns in the table.</param>
public Table(int rows, int columns)
{
Rows = new List<Row>();
Column = new Column();
for (var i = 0; i < rows; i++)
{
var row = new Row();
row.Cells = new List<Cell>();
for (var j = 0; j < columns; j++)
{
var cellContent = "";
var paragraph = new Paragraph();
paragraph.AddRun(new Run { Text = cellContent });
var cell = new Cell { Paragraphs = new List<Paragraph> { paragraph } };
row.Cells.Add(cell);
}
Rows.Add(row);
}
}
}
/// <summary>
/// Represents a row within a table in a Word document.
/// </summary>
public class Row
{
/// <summary>
/// Gets or sets the list of cells within the row.
/// </summary>
public List<Cell> Cells { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Row"/> class with empty cells.
/// </summary>
public Row()
{
Cells = new List<Cell>();
}
}
/// <summary>
/// Represents a cell within a row of a table in a Word document.
/// </summary>
public class Cell
{
/// <summary>
/// Gets or sets the list of paragraphs within the cell.
/// </summary>
public List<Paragraph> Paragraphs { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Cell"/> class with empty paragraphs.
/// </summary>
public Cell()
{
Paragraphs = new List<Paragraph>();
}
}
/// <summary>
/// Represents column properties of a table in a Word document.
/// </summary>
public class Column
{
/// <summary>
/// Gets or sets the width of the column.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Column"/> class with a default width of 0.
/// </summary>
public Column()
{
Width = 0;
}
}
/// <summary>
/// Represents a section element in a Word document.
/// </summary>
public class Section : IElement
{
/// <summary>
/// Gets the unique identifier of the section.
/// </summary>
public int ElementId { get; internal set; }
/// <summary>
/// Gets the page size properties for the section.
/// </summary>
public PageSize PageSize { get; internal set; }
/// <summary>
/// Gets the page margin properties for the section.
/// </summary>
public PageMargin PageMargin { get; internal set; }
internal Section()
{
//Do nothing
}
}
/// <summary>
/// Represents the page size properties of a section in a Word document.
/// </summary>
public class PageSize
{
/// <summary>
/// Gets sets the height of the page.
/// </summary>
public int Height { get; internal set; }
/// <summary>
/// Gets the width of the page.
/// </summary>
public int Width { get; internal set; }
/// <summary>
/// Gets the orientation of the page (e.g., "Portrait" or "Landscape").
/// </summary>
public string Orientation { get; internal set; }
internal PageSize()
{
//Do nothing
}
}
/// <summary>
/// Represents the page margin properties of a section in a Word document.
/// </summary>
public class PageMargin
{
/// <summary>
/// Gets the top margin of the page.
/// </summary>
public int Top { get; internal set; }
/// <summary>
/// Gets the right margin of the page.
/// </summary>
public int Right { get; internal set; }
/// <summary>
/// Gets the bottom margin of the page.
/// </summary>
public int Bottom { get; internal set; }
/// <summary>
/// Gets the left margin of the page.
/// </summary>
public int Left { get; internal set; }
/// <summary>
/// Gets the header margin of the page.
/// </summary>
public int Header { get; internal set; }
/// <summary>
/// Gets the footer margin of the page.
/// </summary>
public int Footer { get; internal set; }
internal PageMargin()
{
//Do nothing
}
}
/// <summary>
/// Represents an unknown element in a Word document.
/// </summary>
public class Unknown : IElement
{
/// <summary>
/// Gets the unique identifier of the unknown element.
/// </summary>
public int ElementId { get; internal set; }
internal Unknown()
{
// Do nothing
}
}
/// <summary>
/// Represents Styles associated with different elements.
/// </summary>
public class ElementStyles
{
/// <summary>
/// Gets the fonts defined in theme.
/// </summary>
public List<string> ThemeFonts { get; internal set; }
/// <summary>
/// Gets the fonts defined in FontTable
/// </summary>
public List<string> TableFonts { get; internal set; }
/// <summary>
/// Gets the Paragraph Styles
/// </summary>
public List<string> ParagraphStyles { get; internal set; }
/// <summary>
/// Gets the Table Styles
/// </summary>
public List<string> TableStyles { get; internal set; }
/// <summary>
/// Initializes all Styles.
/// </summary>
public ElementStyles()
{
ThemeFonts = new List<string>();
TableFonts = new List<string>();
ParagraphStyles = new List<string>();
TableStyles = new List<string>();
}
}
}