How to Remove Content Control Without Deleting in Core Word?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies.Using this library, you can remove content control without deleting content in Word document using C#.
Steps to remove content control without deleting content in Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.
- Include the following namespaces in Program.cs file
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- Use the following code example to remove content control without deleting content in Word document.
C#
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
{
//Load the Word document from the FileStream.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Iterate through each section in the document.
for (int secIndex = 0; secIndex < document.Sections.Count; secIndex++)
{
WSection sec = document.Sections[secIndex];
//Iterate through the body items of the section.
for (int bodyItemIndex = 0; bodyItemIndex < sec.Body.ChildEntities.Count; bodyItemIndex++)
{
IEntity body = sec.Body.ChildEntities[bodyItemIndex];
//Check if the body item is a block content control.
if (body is BlockContentControl)
{
BlockContentControl blockContentControl = body as BlockContentControl;
//Get the block content control index in the body.
int index = sec.Body.ChildEntities.IndexOf(blockContentControl);
//Move the child entities of block content control to section body.
for (int blockItemIndex = 0; blockItemIndex < blockContentControl.ChildEntities.Count;)
{
IEntity item = blockContentControl.ChildEntities[blockItemIndex];
//Insert the child entity to the section body.
sec.Body.ChildEntities.Insert(index, item);
//Increment the index.
index++;
}
//Remove the block content control from the section body.
sec.Body.ChildEntities.Remove(blockContentControl);
}
//Check if the body item is a paragraph.
else if (body is WParagraph)
{
WParagraph paragraph = body as WParagraph;
//Iterate through the items within the paragraph.
for (int paraItemIndex = 0; paraItemIndex < paragraph.ChildEntities.Count; paraItemIndex++)
{
ParagraphItem item = paragraph.ChildEntities[paraItemIndex] as ParagraphItem;
//Check if the paragraph item is an inline content control.
if (item is InlineContentControl)
{
InlineContentControl inlineContentControl = item as InlineContentControl;
//Get the index of inline content control in the paragraph.
int index = paragraph.ChildEntities.IndexOf(item);
//Move the child items of inline content control to the paragraph.
for (int inlineItemIndex = 0; inlineItemIndex < inlineContentControl.ParagraphItems.Count;)
{
ParagraphItem inlineItem = inlineContentControl.ParagraphItems[inlineItemIndex];
//Insert the inline content control items to paragraph's child entities.
paragraph.ChildEntities.Insert(index, inlineItem);
//Increment the index.
index++;
}
//Remove the inline content control from the paragraph.
paragraph.ChildEntities.Remove(inlineContentControl);
}
}
}
}
}
//Create a file stream for saving the document.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the modified document to the file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
You can download a complete working sample that remove content control without deleting content in Word document from the GitHub.
By executing the program, you will get the Word document as follows.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to remove content control without deleting content in Core Word document.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!