Skip to content

Commit 27baa55

Browse files
committed
Add sample
1 parent ff8467c commit 27baa55

6 files changed

Lines changed: 83 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Remove-InlineContentControls-Containing-PageField/Remove-InlineContentControls-Containing-PageField.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Remove_InlineContentControls_Containing_PageField</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)