-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileFormat.Words.cs
973 lines (937 loc) · 43.9 KB
/
FileFormat.Words.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
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
using System;
using System.Collections.Generic;
using IO = System.IO;
using OOXML = OpenXML.Words;
using OWD = OpenXML.Words.Data;
using FileFormat.Words.IElements;
using System.Threading;
namespace FileFormat.Words
{
/// <summary>
/// Custom exception class for file format-related exceptions.
/// </summary>
public class FileFormatException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="FileFormatException"/> class with a specified error message and a reference to the inner exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public FileFormatException(string message, Exception innerException) : base(message, innerException)
{
//Do nothing
}
}
/// <summary>
/// Represents a document with structured elements.
/// </summary>
public class Document : IDisposable
{
private List<IElement> _lstStructure;
private ElementStyles _elementStyles;// { get; internal set; }
private OOXML.OwDocument _ooxmlDoc;
private bool _isNew = true;
//private static List<int> _instances = new List<int>();
//private static int _instanceNumber = 0;
//private int _instance = 0;
private static Lazy<List<int>> _instances = new Lazy<List<int>>(() => new List<int>());
private static int _instanceNumber = 0;
private int _instance = 0;
private int _originalSize = 0;
private IO.MemoryStream _ms; //= new IO.MemoryStream();
private readonly object _lockObject = new object(); // Lock object
/// <summary>
/// Initializes a new instance of the <see cref="Document"/> class.
/// This constructor creates a new, empty document.
/// </summary>
/// <remarks>
/// Use this constructor to create a new, blank document that you can populate with content.
/// To work with an existing document, consider using the <see cref="Document(string)"/> constructor.
/// </remarks>
/// <example>
/// <code>
/// // Create a new, empty document
/// var Doc = new FileFormat.Words.Document();
/// // Initialize a new instance of body the empty document.
/// var body = new FileFormat.Words.Body(doc);
/// // Add paragraph wiht run
/// var para = new FileFormat.Words.IElements.Paragraph();
/// para.AddRun(new FileFormat.Words.IElements.Run
/// {
/// Text = "First Run with Times New Roman Blue Color Bold",
/// FontFamily = "Times New Roman",
/// Color = FF.Colors.Blue,
/// Bold = true,
/// FontSize = 12
/// });
/// // Append paragraph to the document
/// body.AppendChild(para);
/// // Save document to file.
/// doc.Save("DocumentWithPara.docx");
/// </code>
/// </example>
public Document()
{
lock (_lockObject)
{
try
{
_isNew = true;
_ms = new IO.MemoryStream();
_lstStructure = new List<IElement>();
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
_elementStyles = _ooxmlDoc.LoadStyles();
}
catch (Exception ex)
{
var errorMessage = ConstructMessage(ex, "Initializing OWDocument");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Document"/> class by loading content from a file specified by its filename.
/// </summary>
/// <param name="filename">The path to the file from which to load the document content.</param>
/// <exception cref="FileFormatException">
/// Thrown if an error occurs while loading the document.
/// </exception>
public Document(string filename)
{
lock (_lockObject)
{
try
{
_isNew = false;
_instance = Interlocked.Increment(ref _instanceNumber);
_instances.Value.Add(_instance);
_ms = new IO.MemoryStream();
using (var fs = new IO.FileStream(filename, IO.FileMode.Open))
{
fs.CopyTo(_ms);
}
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
_lstStructure = _ooxmlDoc.LoadDocument(_ms);
_elementStyles = _ooxmlDoc.LoadStyles();
_originalSize = _lstStructure.Count;
}
catch (Exception ex)
{
var errorMessage = ConstructMessage(ex, "Loading Document from file");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Document"/> class by loading content from a <see cref="System.IO.Stream"/>.
/// </summary>
/// <param name="stream">The input stream from which to load the document content.</param>
/// <exception cref="FileFormatException">
/// Thrown if an error occurs while loading the document.
/// </exception>
public Document(IO.Stream stream)
{
lock (_lockObject)
{
try
{
_isNew = false;
_instance = Interlocked.Increment(ref _instanceNumber);
_instances.Value.Add(_instance);
_ms = new IO.MemoryStream();
stream.CopyTo(_ms);
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
_lstStructure = _ooxmlDoc.LoadDocument(_ms);
_elementStyles = _ooxmlDoc.LoadStyles();
_originalSize = _lstStructure.Count;
}
catch (Exception ex)
{
var errorMessage = ConstructMessage(ex, "Loading Document from stream");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Saves the document to a file specified by its filename.
/// </summary>
/// <param name="filename">The path to the file where the document will be saved.</param>
/// <exception cref="FileFormatException">
/// Thrown if an error occurs while saving the document.
/// </exception>
public void Save(string filename)
{
lock (_lockObject)
{
try
{
if (!_isNew)
{
using var fs = new IO.FileStream(filename, IO.FileMode.Create);
OWD.OoxmlDocData.CreateInstance().Save(fs, this);
}
else
{
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
_ooxmlDoc.CreateDocument(_lstStructure);
using var fs = new IO.FileStream(filename, IO.FileMode.Create);
_ooxmlDoc.SaveDocument(fs);
}
}
catch (Exception ex)
{
var errorMessage = ConstructMessage(ex, "Saving Document to file");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Saves the document to the specified <see cref="System.IO.Stream"/>.
/// </summary>
/// <param name="stream">The stream to which the document will be saved.</param>
/// <exception cref="FileFormatException">
/// Thrown if an error occurs while saving the document.
/// </exception>
public void Save(IO.Stream stream)
{
lock (_lockObject)
{
try
{
if (!_isNew)
{
OWD.OoxmlDocData.CreateInstance().Save(stream, this);
}
else
{
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
_ooxmlDoc.CreateDocument(_lstStructure);
_ooxmlDoc.SaveDocument(stream);
}
}
catch (Exception ex)
{
var errorMessage = ConstructMessage(ex, "Saving Document to stream");
throw new FileFormatException(errorMessage, ex);
}
}
}
private string ConstructMessage(Exception ex, string operation)
{
return $"Error in Operation {operation} at FileFormat.Words: {ex.Message} \n Inner Exception: {ex.InnerException?.Message ?? "N/A"}";
}
internal int GetInstanceInfo()
{
return _instance;
}
/// <summary>
/// Retrieves a list of existing elements from the internal data structure.
/// </summary>
/// <returns>
/// A list of <see cref="IElement"/> objects representing the existing elements.
/// </returns>
/// <seealso cref="IElement"/>
public List<IElement> GetElements()
{
return _lstStructure;
}
/// <summary>
/// Gets the element styles of the document.
/// </summary>
public ElementStyles GetElementStyles()
{
return _elementStyles;
}
/// <summary>
/// Updates an existing element in the structure.
/// </summary>
/// <param name="element">The updated element to replace the existing one.</param>
/// <returns>
/// <c>true</c> if the element is successfully updated; otherwise, <c>false</c>.
/// If the element is not found in the structure, <c>false</c> is returned.
/// If an error occurs during the update operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method updates an existing element within the structure based on the element's unique identifier.
/// It locates the element in the structure and replaces it with the updated element.
/// If the update operation is successful, the method returns <c>true></c>.
/// If the element is not found in the structure, <c>false</c> is returned, and no changes are made to the structure.
/// If an exception occurs during the update operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public bool Update(IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId);
if (position < 0)
{
return false;
}
var backupElement = element;
try
{
_lstStructure[position] = element;
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Update(element, position, this);
return true;
}
catch (Exception ex)
{
_lstStructure[position] = backupElement;
var errorMessage = ConstructMessage(ex, "Update");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Updates an existing element in the structure.
/// </summary>
/// <param name="elementId">The unique identifier of the element to be updated.</param>
/// <param name="element">The updated element to replace the existing one.</param>
/// <returns>
/// <c>true</c> if the element is successfully updated; otherwise, <c>false</c>.
/// If the element with the specified ID is not found in the structure, <c>false</c> is returned.
/// If the elements' IDs do not match, a <see cref="FileFormatException"/> is thrown.
/// If an error occurs during the update operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method updates an existing element within the structure based on the provided element's unique identifier.
/// It verifies that the element to be updated has the same ID as the specified element ID.
/// If the IDs match, the method attempts to locate the element in the structure and replace it with the updated element.
/// If the update operation is successful, the method returns <c>true></c>.
/// If the element with the specified ID is not found, <c>false</c> is returned, and no changes are made to the structure.
/// If the provided element's ID does not match the specified element ID, a <see cref="FileFormatException"/> is thrown.
/// If an exception occurs during the update operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public bool Update(int elementId, IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
if (elementId != element.ElementId)
{
var ex = new Exception("The elements mismatch: Update is only available for same element");
throw new FileFormatException(ex.Message, new InvalidOperationException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position < 0)
{
return false;
}
var backupElement = element;
try
{
_lstStructure[position] = element;
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Update(element, position, this);
return true;
}
catch (Exception ex)
{
_lstStructure[position] = backupElement;
var errorMessage = ConstructMessage(ex, "Update");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Inserts an element before the specified reference element.
/// </summary>
/// <param name="newElement">The element to be inserted.</param>
/// <param name="element">The reference element before which the new element should be inserted.</param>
/// <returns>
/// The unique identifier <see cref="IElement.ElementId"/> of inserted element if the new element is successfully inserted before the reference element.
/// If the reference element is not found in the structure, -1 is returned.
/// If an error occurs during the insertion operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method inserts the specified element before the provided reference element in the structure.
/// The method attempts to locate the reference element and insert the new element before it.
/// If the insertion operation is successful, the method returns <see cref="IElement.ElementId"/> of the inserted element.
/// If the reference element is not found, -1 is returned, and no changes are made to the structure.
/// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public int InsertBefore(IElement newElement, IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId);
if (position < 0)
{
return -1;
}
var newElementId = _originalSize + 1;
switch (newElement)
{
case Paragraph para:
para.ElementId = newElementId;
break;
case Table table:
table.ElementId = newElementId;
break;
case Image image:
image.ElementId = newElementId;
break;
}
try
{
_lstStructure.Insert(position, newElement);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Insert(newElement, position, this);
_originalSize++;
return newElementId;
}
catch (Exception ex)
{
_lstStructure.RemoveAt(position);
var errorMessage = ConstructMessage(ex, "InsertBefore");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Inserts an element before the element with the specified unique ID.
/// </summary>
/// <param name="newElement">The element to be inserted.</param>
/// <param name="elementId">The unique ID of the element before which the new element should be inserted.</param>
/// <returns>
/// The unique identifier <see cref="IElement.ElementId"/> of the inserted element if the new element is successfully inserted before the specified element.
/// If the specified element with the provided ID is not found, -1 is returned.
/// If an error occurs during the insertion operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method inserts the specified element before the element with the provided unique ID in the structure.
/// The method attempts to locate the element with the specified ID and insert the new element before it.
/// If the insertion operation is successful, the method returns <see cref="IElement.ElementId"/> of the inserted element.
/// If the element with the specified ID is not found, -1 is returned, and no changes are made to the structure.
/// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public int InsertBefore(IElement newElement, int elementId)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position < 0) return -1;
var newElementId = _originalSize + 1;
switch (newElement)
{
case Paragraph para:
para.ElementId = newElementId;
break;
case Table table:
table.ElementId = newElementId;
break;
case Image image:
image.ElementId = newElementId;
break;
}
try
{
_lstStructure.Insert(position, newElement);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Insert(newElement, position, this);
_originalSize++;
return newElementId;
}
catch (Exception ex)
{
_lstStructure.RemoveAt(position);
var errorMessage = ConstructMessage(ex, "InsertBefore");
throw new FileFormatException(errorMessage, ex);
}
}
}
internal int Append(IElement newElement)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
//Console.WriteLine($"Number of Elements in List: {_lstStructure.Count}");
var newElementId = _originalSize + 1;
switch (newElement)
{
//Console.WriteLine(_lstStructure.Count);
case Paragraph para:
para.ElementId = newElementId;
break;
case Table table:
table.ElementId = newElementId;
break;
case Image image:
image.ElementId = newElementId;
break;
}
var originalCount = _lstStructure.Count;
try
{
if (_lstStructure.Count > 0 && _lstStructure[originalCount - 1] is Section section)
{
InsertBefore(newElement, section.ElementId);
}
else
{
_lstStructure.Add(newElement);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Append(newElement, this);
}
//Console.WriteLine("Hello + " + _instance);
_originalSize++;
return newElementId;
}
catch (Exception ex)
{
// Determine which element caused the exception and remove it
if (_lstStructure.Count > originalCount)
{
if (_lstStructure[originalCount - 1] is Section section)
{
RemoveBefore(section.ElementId);
}
else
{
_lstStructure.RemoveAt(originalCount - 1); // Corrected index
}
}
var errorMessage = ConstructMessage(ex, "Append");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Inserts an element after a specified element in the structure.
/// </summary>
/// <param name="newElement">The element to be inserted.</param>
/// <param name="element">The element after which the new element should be inserted.</param>
/// <returns>
/// The unique identifier <see cref="IElement.ElementId"/> if the new element is successfully inserted after the specified element.
/// If the specified element is not found, -1 is returned.
/// If an error occurs during the insertion operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method inserts the specified element after the provided element in the structure.
/// The method attempts to locate the specified element by comparing their unique IDs.
/// If the insertion operation is successful, the method returns <see cref="IElement.ElementId"/> of the inserted element.
/// If the specified element is not found, -1 is returned, and no changes are made to the structure.
/// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public int InsertAfter(IElement newElement, IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId);
if (position < 0) return -1;
var newElementId = _originalSize + 1;
switch (newElement)
{
case Paragraph para:
para.ElementId = newElementId;
break;
case Table table:
table.ElementId = newElementId;
break;
case Image image:
image.ElementId = newElementId;
break;
}
try
{
_lstStructure.Insert(position + 1, newElement);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Insert(newElement, position + 1, this);
_originalSize++;
return newElementId;
}
catch (Exception ex)
{
_lstStructure.RemoveAt(position + 1);
var errorMessage = ConstructMessage(ex, "InsertAfter");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Inserts an element after a specified element in the structure by its unique ID.
/// </summary>
/// <param name="newElement">The element to be inserted.</param>
/// <param name="elementId">The unique ID of the element after which the new element should be inserted.</param>
/// <returns>
/// The unique identifier <see cref="IElement.ElementId"/> of the inserted element if the new element is successfully inserted after the specified element.
/// If the specified element is not found, -1 is returned.
/// If an error occurs during the insertion operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method inserts the specified element after the element with the provided unique ID in the structure.
/// It is important to ensure that the specified element exists in the structure to determine the insertion point.
/// If the insertion operation is successful, the method returns <see cref="IElement.ElementId"/> of the inserted element.
/// If the specified element is not found, -1 is returned, and no changes are made to the structure.
/// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public int InsertAfter(IElement newElement, int elementId)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position < 0) return -1;
var newElementId = _originalSize + 1;
switch (newElement)
{
case Paragraph para:
para.ElementId = newElementId;
break;
case Table table:
table.ElementId = newElementId;
break;
case Image image:
image.ElementId = newElementId;
break;
}
try
{
_lstStructure.Insert(position + 1, newElement);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Insert(newElement, position + 1, this);
_originalSize++;
return newElementId;
}
catch (Exception ex)
{
_lstStructure.RemoveAt(position + 1);
var errorMessage = ConstructMessage(ex, "InsertAfter");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Removes the element that follows a specified element in the structure.
/// </summary>
/// <param name="element">The element whose follower should be removed.</param>
/// <returns>
/// <c>true</c> if the element following the specified element is successfully removed; otherwise, <c>false</c>.
/// If the specified element is not found or if there is no element following it, <c>false</c> is returned.
/// If an error occurs during the removal operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method removes the element that comes after the specified element in the structure. It is essential to ensure that the
/// specified element exists in the structure and has an element following it.
/// If the removal operation is successful, the method returns <c>true</c>.
/// If the element is not found or there is no element following it, <c>false</c> is returned, and no changes are made to the structure.
/// If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public bool RemoveAfter(IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId);
if (position >= 0 && position < _lstStructure.Count - 1)
{
var backupElement = _lstStructure[position + 1];
try
{
_lstStructure.RemoveAt(position + 1);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Remove(position + 1, this);
return true;
}
catch (Exception ex)
{
_lstStructure.Insert(position + 1, backupElement);
var errorMessage = ConstructMessage(ex, "RemoveAfter");
throw new FileFormatException(errorMessage, ex);
}
}
return false;
}
}
/// <summary>
/// Removes the element that follows a specified element in the structure by its unique ID.
/// </summary>
/// <param name="elementId">The unique ID of the element whose follower should be removed.</param>
/// <returns>
/// <c>true</c> if the element following the specified element is successfully removed; otherwise, <c>false</c>.
/// If the specified element is not found or if there is no element following it, <c>false</c> is returned.
/// If an error occurs during the removal operation, a <see cref="FileFormatException"/> is thrown.
/// </returns>
/// <remarks>
/// This method removes the element that comes after the specified element with the provided unique ID in the structure.
/// It is essential to ensure that the specified element exists in the structure and has an element following it.
/// If the removal operation is successful, the method returns <c>true</c>.
/// If the element is not found or there is no element following it, <c>false</c> is returned, and no changes are made to the structure.
/// If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state
/// and throws a <see cref="FileFormatException"/> with detailed error information.
/// </remarks>
public bool RemoveAfter(int elementId)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position >= 0 && position < _lstStructure.Count - 1)
{
var backupElement = _lstStructure[position + 1];
try
{
_lstStructure.RemoveAt(position + 1);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Remove(position + 1, this);
return true;
}
catch (Exception ex)
{
_lstStructure.Insert(position + 1, backupElement);
var errorMessage = ConstructMessage(ex, "RemoveAfter");
throw new FileFormatException(errorMessage, ex);
}
}
return false;
}
}
/// <summary>
/// Removes the element that precedes the specified <paramref name="element"/> from the collection.
/// If the specified <paramref name="element"/> is not found or is the first element in the collection, no removal occurs.
/// </summary>
/// <remarks>
/// The method searches for the element with the specified <paramref name="element"/> identifier and removes
/// the element that immediately precedes it in the collection. If the specified <paramref name="element"/>
/// is not found or is the first element in the collection, no removal occurs, and the method returns -1.
/// In case of success, it returns the <see cref="IElement.ElementId"/> of removed element.
/// If the removal of the preceding element fails due to an exception, it rolls back the operation and reverts the document to its original state.
/// </remarks>
/// <param name="element">The element whose predecessor should be removed.</param>
/// <returns>
/// <c>true</c> if the preceding element is successfully removed; otherwise, <c>false</c> if the element is not found.
/// Throws a <see cref="FileFormatException"/> if an exception occurs during the operation.
/// </returns>
/// <seealso cref="IElement.ElementId"/>
public bool RemoveBefore(IElement element)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId);
if (position <= 0) return false;
var backupElement = element;
try
{
_lstStructure.RemoveAt(position - 1);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Remove(position - 1, this);
return true;
}
catch (Exception ex)
{
_lstStructure.Insert(position - 1, backupElement);
var errorMessage = ConstructMessage(ex, "RemoveBefore");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Removes the element that precedes the specified element with the given ElementID in the document.
/// </summary>
/// <param name="elementId">The unique identifier of the element after which you want to remove the preceding element.</param>
/// <returns>
/// <c>true</c> if the preceding element is successfully removed; otherwise, <c>false</c> if the element is not found.
/// Throws a <see cref="FileFormatException"/> if an exception occurs during the operation.
/// </returns>
/// <remarks>
/// If the specified element is not found in the document, this method returns <c>false</c>.
/// If the removal of the preceding element fails due to an exception, it rolls back the operation and reverts the document to its original state.
/// The preceding element is removed from the internal document structure and, if applicable, the underlying OOXML document.
/// </remarks>
public bool RemoveBefore(int elementId)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position <= 0) return false;
var backupElement = _lstStructure[position];
try
{
_lstStructure.RemoveAt(position - 1);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Remove(position - 1, this);
return true;
}
catch (Exception ex)
{
_lstStructure.Insert(position - 1, backupElement);
var errorMessage = ConstructMessage(ex, "RemoveBefore");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Removes the specified element with the given ElementID from the document.
/// </summary>
/// <param name="elementId">The unique identifier of the element you want to remove.</param>
/// <returns>
/// <c>true</c> if the element is successfully removed; otherwise, <c>false</c> if the element is not found.
/// Throws a <see cref="FileFormatException"/> if an exception occurs during the operation.
/// </returns>
/// <remarks>
/// If the specified element is not found in the document, this method returns <c>false</c>.
/// If the removal of the element fails due to an exception, it rolls back the operation and reverts the document to its original state.
/// The element is removed from the internal document structure and, if applicable, the underlying OOXML document.
/// </remarks>
public bool Remove(int elementId)
{
lock (_lockObject)
{
if (_lstStructure == null)
{
throw new FileFormatException("Structure is unavailable...", new NullReferenceException());
}
var position = _lstStructure.FindIndex(e => e.ElementId == elementId);
if (position < 0) return false;
var backupElement = _lstStructure[position];
try
{
_lstStructure.RemoveAt(position);
if (!_isNew) OWD.OoxmlDocData.CreateInstance().Remove(position, this);
return true;
}
catch (Exception ex)
{
_lstStructure.Insert(position, backupElement);
var errorMessage = ConstructMessage(ex, "Remove");
throw new FileFormatException(errorMessage, ex);
}
}
}
/// <summary>
/// Dispose off all managed and unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose of managed resources (if any)
if (_lstStructure != null) _lstStructure = null;
if (_elementStyles != null) _elementStyles = null;
if (_ooxmlDoc != null)
{
_ooxmlDoc.Dispose();
_ooxmlDoc = null;
}
OWD.OoxmlDocData.CreateInstance().Dispose();
}
// Dispose of unmanaged resources
if (_ms == null) return;
_ms.Dispose();
_ms = null;
}
}
/// <summary>
/// Represents the body of a document, containing paragraphs, tables, images, and sections.
/// </summary>
public class Body
{
private readonly object _lockObject = new object();
/// <summary>
/// Gets the list of paragraphs in the body.
/// </summary>
public List<Paragraph> Paragraphs { get; internal set; }
/// <summary>
/// Gets the list of tables in the body.
/// </summary>
public List<Table> Tables { get; internal set; }
/// <summary>
/// Gets the list of images in the body.
/// </summary>
public List<Image> Images { get; internal set; }
/// <summary>
/// Gets the list of sections in the body.
/// </summary>
public List<Section> Sections { get; internal set; }
private Document Doc { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Body"/> class with the specified document.
/// </summary>
/// <param name="doc">The parent document containing the body.</param>
public Body(Document doc)
{
lock (_lockObject)
{
Paragraphs = new List<Paragraph>();
Tables = new List<Table>();
Images = new List<Image>();
Sections = new List<Section>();
foreach (var element in doc.GetElements())
{
if (element is Paragraph)
{
Paragraphs.Add((Paragraph)element);
}
if (element is Table)
{
Tables.Add((Table)element);
}
if (element is Image)
{
Images.Add((Image)element);
}
if (element is Section section)
{
Sections.Add(section);
}
}
Doc = doc;
}
}
/// <summary>
/// Appends a child element to the body.
/// </summary>
/// <param name="element">The element to append to the body.</param>
/// <returns>
/// The unique identifier <see cref="IElement.ElementId"/> of the appended element.
/// </returns>
public int AppendChild(IElement element)
{
lock (_lockObject)
{
return Doc.Append(element);
}
}
}
}