1+ using Syncfusion . DocIO ;
2+ using Syncfusion . DocIO . DLS ;
3+
4+ namespace Remove_InlineContentControls_Containing_PageField
5+ {
6+ class Program
7+ {
8+ public static void Main ( string [ ] args )
9+ {
10+ //Load existing Word document
11+ using ( WordDocument document = new WordDocument ( Path . GetFullPath ( @"Data/Input.docx" ) , FormatType . Docx ) )
12+ {
13+ //Find Content control
14+ List < Entity > entities = document . FindAllItemsByProperties ( EntityType . InlineContentControl , null , null ) ;
15+ // Iterate through the list of entities in reverse to safely remove items during iteration
16+ for ( int i = entities . Count - 1 ; i >= 0 ; i -- )
17+ {
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 )
24+ {
25+ // Check if the entity is a PAGE field
26+ if ( entity is WField field && field . FieldType == FieldType . FieldPage )
27+ {
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 ;
46+ }
47+ }
48+ }
49+ }
50+ // Save the document if needed
51+ using ( FileStream fileStream = new FileStream ( @"../../../Output/Output.docx" , FileMode . OpenOrCreate , FileAccess . ReadWrite ) )
52+ {
53+ document . Save ( fileStream , FormatType . Docx ) ;
54+ }
55+ }
56+ }
57+ }
58+ }
0 commit comments