How to find and replace list paragraphs in ASP.NET Core Word document?
Syncfusion® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can find and replace with multiple list paragraphs in Word document using C#.
Steps to find and replace with multiple list paragraphs in Word document
-
Create a new C# .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 namespace in the Program.cs file.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- Include the below code snippet in Program.cs to find and replace with multiple list paragraphs in Word document.
C#
string[] filePaths = {"../../../Data/Heading1Items.docx","../../../Data/Heading2Items.docx"};
//Open the file as Stream.
using (FileStream documentStream = new FileStream("../../../Data/Input.docx", FileMode.Open, FileAccess.Read))
{
//Open an existing Word document.
using (WordDocument document = new WordDocument(documentStream, FormatType.Docx))
{
for(int documentIndex = 1; documentIndex <= filePaths.Length; documentIndex++)
{
//String to be found.
string findText = "<<Heading"+ documentIndex + "Items>>";
//Find the selection in the document.
TextSelection selection = document.Find(findText, false, false);
//Get the owner paragraph.
WParagraph ownerPara = selection.GetAsOneRange().OwnerParagraph;
//Open the file as Stream.
using (FileStream subDocumentStream = new FileStream(filePaths[documentIndex-1], FileMode.Open, FileAccess.Read))
{
//Open an sub Word document.
using (WordDocument subDocument = new WordDocument(subDocumentStream, FormatType.Docx))
{
//Create a text body part to be replaced.
TextBodyPart textBodyPart = CreateBodyPart(subDocument, ownerPara);
//Replace the text with the created text body part.
document.Replace(findText, textBodyPart, true, true);
}
}
}
//Save the Word document.
using (FileStream output = new FileStream("../../../Result.docx", FileMode.Create, FileAccess.Write))
{
document.Save(output, FormatType.Docx);
}
}
}
- Add the following method to create body parts that need to be replaced in the Word document.
C#
/// <summary>
/// Create body parts that need to be replaced in the Word document.
/// </summary>
/// <param name="subDocument">Document contains the paragraph which need to be replace</param>
/// <param name="ownerPara">The paragraph that needs to be copied.</param>
TextBodyPart CreateBodyPart(WordDocument subDocument, WParagraph ownerPara)
{
//Creates new text body part.
TextBodyPart bodyPart = new TextBodyPart(ownerPara.Document);
//Iterate each section of the Word document.
foreach (WSection section in subDocument.Sections)
{
//Accesses the Body of section where all the contents in document are apart
WTextBody sectionBody = section.Body;
IterateTextBody(sectionBody, ownerPara, bodyPart);
}
return bodyPart;
}
/// <summary>
/// Iterates textbody child elements.
/// </summary>
void IterateTextBody(WTextBody sectionTextBody, WParagraph ownerPara, TextBodyPart bodyPart)
{
//Iterates through each of the child items of WTextBody
for (int i = 0; i < sectionTextBody.ChildEntities.Count; i++)
{
//IEntity is the basic unit in DocIO DOM.
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
IEntity bodyItemEntity = sectionTextBody.ChildEntities[i];
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
//Decides the element type by using EntityType
switch (bodyItemEntity.EntityType)
{
case EntityType.Paragraph:
WParagraph paragraph = bodyItemEntity as WParagraph;
AddParagraphItemsToTextBody (paragraph, ownerPara, bodyPart);
break;
case EntityType.Table:
//Add to text body part.
bodyPart.BodyItems.Add(bodyItemEntity.Clone());
break;
case EntityType.BlockContentControl:
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
//Iterates to the body items of Block Content Control.
IterateTextBody(blockContentControl.TextBody, ownerPara, bodyPart);
break;
}
}
}
/// <summary>
/// Add the paragraph items to the text body part.
/// </summary>
void AddParagraphItemsToTextBody(WParagraph paragraph, WParagraph ownerPara, TextBodyPart bodyPart)
{
//Clone the paragraph.
WParagraph paratoInsert = ownerPara.Clone() as WParagraph;
//Clear the paragraph's items.
paratoInsert.ChildEntities.Clear();
//Iterate the items of the paragraph.
for (int paraItemIndex = 0; paraItemIndex < paragraph.ChildEntities.Count; paraItemIndex++)
{
//Add the paragraph items.
paratoInsert.ChildEntities.Add(paragraph.ChildEntities[paraItemIndex].Clone());
}
//Add to text body part.
bodyPart.BodyItems.Add(paratoInsert.Clone());
}
A complete working sample to find and replace with multiple list paragraphs in Word document can be downloaded from 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 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 find and replace list paragraphs in ASP.NET 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!