Skip to content

Commit 91fe3f3

Browse files
committed
Addressed the feedback
1 parent 27baa55 commit 91fe3f3

1 file changed

Lines changed: 24 additions & 31 deletions

File tree

  • Content-Controls/Remove-InlineContentControls-Containing-PageField/.NET/Remove-InlineContentControls-Containing-PageField

Content-Controls/Remove-InlineContentControls-Containing-PageField/.NET/Remove-InlineContentControls-Containing-PageField/Program.cs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,39 @@ class Program
77
{
88
public static void Main(string[] args)
99
{
10-
//Load existing Word document
10+
// Load existing Word document
1111
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Input.docx"), FormatType.Docx))
1212
{
13-
//Find Content control
14-
List<Entity> entities = document.FindAllItemsByProperties(EntityType.InlineContentControl, null, null);
13+
// Find all PAGE fields in the document
14+
List<Entity> pageFields = document.FindAllItemsByProperty(EntityType.Field, "FieldType", "FieldPage");
1515
// Iterate through the list of entities in reverse to safely remove items during iteration
16-
for (int i = entities.Count - 1; i >= 0; i--)
16+
for (int i = pageFields.Count - 1; i >= 0; i--)
1717
{
18-
// Attempt to cast the current entity to an InlineContentControl
19-
InlineContentControl inLineContentControl = entities[i] as InlineContentControl;
20-
if (inLineContentControl != null)
21-
{
22-
// Iterate through the child items of the content control
23-
foreach (ParagraphItem entity in inLineContentControl.ParagraphItems)
18+
// Attempt to cast the current entity to an WField
19+
WField pageField = pageFields[i] as WField;
20+
if (pageField != null)
21+
{
22+
// Check the direct parent of PAGE field is InlineContentControl
23+
if (pageField.Owner is InlineContentControl)
2424
{
25-
// Check if the entity is a PAGE field
26-
if (entity is WField field && field.FieldType == FieldType.FieldPage)
25+
InlineContentControl inlineContentControl = pageField.Owner as InlineContentControl;
26+
// If the content control is nested inside another content control
27+
if (inlineContentControl.Owner is InlineContentControl)
2728
{
28-
// If the content control is nested inside another content control
29-
if (inLineContentControl.Owner is InlineContentControl)
30-
{
31-
InlineContentControl owner = inLineContentControl.Owner as InlineContentControl;
32-
// Remove the current content control from its parent content control
33-
owner.ParagraphItems.Remove(inLineContentControl);
34-
}
35-
// If the content control is directly inside a paragraph
36-
else if (inLineContentControl.Owner is WParagraph)
37-
{
38-
WParagraph parentParagraph = inLineContentControl.Owner as WParagraph;
39-
// Remove the content control from the paragraph
40-
parentParagraph.ChildEntities.Remove(inLineContentControl);
41-
}
42-
// Remove the content control from the main entity list
43-
entities.RemoveAt(i);
44-
// Exit the inner loop since the control has been removed
45-
break;
29+
InlineContentControl owner = inlineContentControl.Owner as InlineContentControl;
30+
// Remove the current content control from its parent content control
31+
owner.ParagraphItems.Remove(inlineContentControl);
32+
}
33+
// If the content control is directly inside a paragraph
34+
else if (inlineContentControl.Owner is WParagraph)
35+
{
36+
WParagraph parentParagraph = inlineContentControl.Owner as WParagraph;
37+
// Remove the content control from the paragraph
38+
parentParagraph.ChildEntities.Remove(inlineContentControl);
4639
}
4740
}
4841
}
49-
}
42+
}
5043
// Save the document if needed
5144
using (FileStream fileStream = new FileStream(@"../../../Output/Output.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
5245
{

0 commit comments

Comments
 (0)