Skip to content

Commit 20d6ca9

Browse files
Addressed the feed backs
1 parent b97341c commit 20d6ca9

File tree

3 files changed

+51
-51
lines changed
  • Content-Controls/Remove-content-control-not-content/.NET/Remove-content-control-not-content
  • Tables/Remove-contents-above-table/.NET/Remove-contents-above-table

3 files changed

+51
-51
lines changed

Content-Controls/Remove-content-control-not-content/.NET/Remove-content-control-not-content/Program.cs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,69 @@
44
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
55
{
66
//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))
108
{
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++)
1411
{
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++)
1815
{
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)
2419
{
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);
3034
}
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)
4037
{
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++)
4441
{
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)
5045
{
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);
5660
}
57-
//Remove the inline content control from the paragraph.
58-
paragraph.ChildEntities.Remove(inlineContentControl);
5961
}
6062
}
6163
}
6264
}
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+
}
6971
}
7072
}

Tables/Remove-contents-above-table/.NET/Remove-contents-above-table/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
{
1717
document.Save(outputFileStream, FormatType.Docx); //Saves the document in DOCX format.
1818
}
19-
//Closes the Word document after saving.
20-
document.Close();
2119
}
2220
}
2321

0 commit comments

Comments
 (0)