|
4 | 4 | using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read)) |
5 | 5 | { |
6 | 6 | //Load the Word document from the FileStream. |
7 | | - WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx); |
8 | | - //Iterate through each section in the document. |
9 | | - for (int secIndex = 0; secIndex < document.Sections.Count; secIndex++) |
| 7 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) |
10 | 8 | { |
11 | | - WSection sec = document.Sections[secIndex]; |
12 | | - //Iterate through the body items of the section. |
13 | | - for (int bodyItemIndex = 0; bodyItemIndex < sec.Body.ChildEntities.Count; bodyItemIndex++) |
| 9 | + //Iterate through each section in the document. |
| 10 | + for (int secIndex = 0; secIndex < document.Sections.Count; secIndex++) |
14 | 11 | { |
15 | | - IEntity body = sec.Body.ChildEntities[bodyItemIndex]; |
16 | | - //Check if the body item is a block content control. |
17 | | - if (body is BlockContentControl) |
| 12 | + WSection sec = document.Sections[secIndex]; |
| 13 | + //Iterate through the body items of the section. |
| 14 | + for (int bodyItemIndex = 0; bodyItemIndex < sec.Body.ChildEntities.Count; bodyItemIndex++) |
18 | 15 | { |
19 | | - BlockContentControl blockContentControl = body as BlockContentControl; |
20 | | - //Get the block content control index in the body. |
21 | | - int index = sec.Body.ChildEntities.IndexOf(blockContentControl); |
22 | | - //Move the child entities of block content control to section body. |
23 | | - for (int blockItemIndex = 0; blockItemIndex < blockContentControl.ChildEntities.Count;) |
| 16 | + IEntity body = sec.Body.ChildEntities[bodyItemIndex]; |
| 17 | + //Check if the body item is a block content control. |
| 18 | + if (body is BlockContentControl) |
24 | 19 | { |
25 | | - IEntity item = blockContentControl.ChildEntities[blockItemIndex]; |
26 | | - //Insert the child entity to the section body. |
27 | | - sec.Body.ChildEntities.Insert(index, item); |
28 | | - //Increment the index. |
29 | | - index++; |
| 20 | + BlockContentControl blockContentControl = body as BlockContentControl; |
| 21 | + //Get the block content control index in the body. |
| 22 | + int index = sec.Body.ChildEntities.IndexOf(blockContentControl); |
| 23 | + //Move the child entities of block content control to section body. |
| 24 | + for (int blockItemIndex = 0; blockItemIndex < blockContentControl.ChildEntities.Count;) |
| 25 | + { |
| 26 | + IEntity item = blockContentControl.ChildEntities[blockItemIndex]; |
| 27 | + //Insert the child entity to the section body. |
| 28 | + sec.Body.ChildEntities.Insert(index, item); |
| 29 | + //Increment the index. |
| 30 | + index++; |
| 31 | + } |
| 32 | + //Remove the block content control from the section body. |
| 33 | + sec.Body.ChildEntities.Remove(blockContentControl); |
30 | 34 | } |
31 | | - //Remove the block content control from the section body. |
32 | | - sec.Body.ChildEntities.Remove(blockContentControl); |
33 | | - } |
34 | | - //Check if the body item is a paragraph. |
35 | | - else if (body is WParagraph) |
36 | | - { |
37 | | - WParagraph paragraph = body as WParagraph; |
38 | | - //Iterate through the items within the paragraph. |
39 | | - for (int paraItemIndex = 0; paraItemIndex < paragraph.ChildEntities.Count; paraItemIndex++) |
| 35 | + //Check if the body item is a paragraph. |
| 36 | + else if (body is WParagraph) |
40 | 37 | { |
41 | | - ParagraphItem item = paragraph.ChildEntities[paraItemIndex] as ParagraphItem; |
42 | | - //Check if the paragraph item is an inline content control. |
43 | | - if (item is InlineContentControl) |
| 38 | + WParagraph paragraph = body as WParagraph; |
| 39 | + //Iterate through the items within the paragraph. |
| 40 | + for (int paraItemIndex = 0; paraItemIndex < paragraph.ChildEntities.Count; paraItemIndex++) |
44 | 41 | { |
45 | | - InlineContentControl inlineContentControl = item as InlineContentControl; |
46 | | - //Get the index of inline content control in the paragraph. |
47 | | - int index = paragraph.ChildEntities.IndexOf(item); |
48 | | - //Move the child items of inline content control to the paragraph. |
49 | | - for (int inlineItemIndex = 0; inlineItemIndex < inlineContentControl.ParagraphItems.Count;) |
| 42 | + ParagraphItem item = paragraph.ChildEntities[paraItemIndex] as ParagraphItem; |
| 43 | + //Check if the paragraph item is an inline content control. |
| 44 | + if (item is InlineContentControl) |
50 | 45 | { |
51 | | - ParagraphItem inlineItem = inlineContentControl.ParagraphItems[inlineItemIndex]; |
52 | | - //Insert the inline content control items to paragraph's child entities. |
53 | | - paragraph.ChildEntities.Insert(index, inlineItem); |
54 | | - //Increment the index. |
55 | | - index++; |
| 46 | + InlineContentControl inlineContentControl = item as InlineContentControl; |
| 47 | + //Get the index of inline content control in the paragraph. |
| 48 | + int index = paragraph.ChildEntities.IndexOf(item); |
| 49 | + //Move the child items of inline content control to the paragraph. |
| 50 | + for (int inlineItemIndex = 0; inlineItemIndex < inlineContentControl.ParagraphItems.Count;) |
| 51 | + { |
| 52 | + ParagraphItem inlineItem = inlineContentControl.ParagraphItems[inlineItemIndex]; |
| 53 | + //Insert the inline content control items to paragraph's child entities. |
| 54 | + paragraph.ChildEntities.Insert(index, inlineItem); |
| 55 | + //Increment the index. |
| 56 | + index++; |
| 57 | + } |
| 58 | + //Remove the inline content control from the paragraph. |
| 59 | + paragraph.ChildEntities.Remove(inlineContentControl); |
56 | 60 | } |
57 | | - //Remove the inline content control from the paragraph. |
58 | | - paragraph.ChildEntities.Remove(inlineContentControl); |
59 | 61 | } |
60 | 62 | } |
61 | 63 | } |
62 | 64 | } |
63 | | - } |
64 | | - //Create a file stream for saving the document. |
65 | | - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
66 | | - { |
67 | | - //Save the modified document to the file stream. |
68 | | - document.Save(outputFileStream, FormatType.Docx); |
| 65 | + //Create a file stream for saving the document. |
| 66 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 67 | + { |
| 68 | + //Save the modified document to the file stream. |
| 69 | + document.Save(outputFileStream, FormatType.Docx); |
| 70 | + } |
69 | 71 | } |
70 | 72 | } |
0 commit comments