Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Remove-InlineContentControls-Containing-PageField/Remove-InlineContentControls-Containing-PageField.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Remove_InlineContentControls_Containing_PageField
{
class Program
{
public static void Main(string[] args)
{
//Load existing Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Input.docx"), FormatType.Docx))
{
//Find Content control
List<Entity> entities = document.FindAllItemsByProperties(EntityType.InlineContentControl, null, null);
// Iterate through the list of entities in reverse to safely remove items during iteration
for (int i = entities.Count - 1; i >= 0; i--)
{
// Attempt to cast the current entity to an InlineContentControl
InlineContentControl inLineContentControl = entities[i] as InlineContentControl;
if (inLineContentControl != null)
{
// Iterate through the child items of the content control
foreach (ParagraphItem entity in inLineContentControl.ParagraphItems)
{
// Check if the entity is a PAGE field
if (entity is WField field && field.FieldType == FieldType.FieldPage)
{
// If the content control is nested inside another content control
if (inLineContentControl.Owner is InlineContentControl)
{
InlineContentControl owner = inLineContentControl.Owner as InlineContentControl;
// Remove the current content control from its parent content control
owner.ParagraphItems.Remove(inLineContentControl);
}
// If the content control is directly inside a paragraph
else if (inLineContentControl.Owner is WParagraph)
{
WParagraph parentParagraph = inLineContentControl.Owner as WParagraph;
// Remove the content control from the paragraph
parentParagraph.ChildEntities.Remove(inLineContentControl);
}
// Remove the content control from the main entity list
entities.RemoveAt(i);
// Exit the inner loop since the control has been removed
break;
}
}
}
}
// Save the document if needed
using (FileStream fileStream = new FileStream(@"../../../Output/Output.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
document.Save(fileStream, FormatType.Docx);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Remove_InlineContentControls_Containing_PageField</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading