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,51 @@
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 all PAGE fields in the document
List<Entity> pageFields = document.FindAllItemsByProperty(EntityType.Field, "FieldType", "FieldPage");
// Iterate through the list of entities in reverse to safely remove items during iteration
for (int i = pageFields.Count - 1; i >= 0; i--)
{
// Attempt to cast the current entity to an WField
WField pageField = pageFields[i] as WField;
if (pageField != null)
{
// Check the direct parent of PAGE field is InlineContentControl
if (pageField.Owner is InlineContentControl)
{
InlineContentControl inlineContentControl = pageField.Owner as InlineContentControl;
// 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);
}
}
}
}
// 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