Streamline your Word document editing with the Syncfusion .NET Word Library (DocIO). Find and replace text with dynamic elements, including images, hyperlinks, paragraphs, tables, or even an entire document, in C#. This functionality operates independently of Microsoft Word and interop dependencies. Refine your searches using options for case-sensitive and whole-word matches.
Here is an example of how to find and replace text in a Word document using C# in the Syncfusion .NET Word Library (DocIO).
//Load an existing Word document.
using FileStream inputDocumentStream = new FileStream("Template.docx", FileMode.Open, FileAccess.ReadWrite);
using WordDocument document = new WordDocument(inputDocumentStream, FormatType.Docx);
//Replace all occurrences of the word "Adventure" with "Syncfusion".
document.Replace("Adventure", "Syncfusion", false, true);
//Save the Word document.
using FileStream outputDocumentStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite);
document.Save(outputDocumentStream, FormatType.Docx);
Find text in a Word document based on a specific pattern using a regular expression (regex) or normal string. Search for the text in the entire Word document or just within a specific paragraph.
Refine the find operation by matching the casing or whole word for more precise results.
Find only the first occurrence of a piece of text in a Word document.
Find all the occurrences of a length of text in all parts of a Word document, including the body, header, footer, shapes, comments, footnotes, and endnotes.
Find the next occurrence of specified text in a Word document.
Find the first or next occurrence of text that extends across several paragraphs.
Perform a find and replace operation for text in a Word document and see the total number of replacements made.
Replace only the first occurrence of the specified text in a Word document.
Replace searched for text in a Word document with the entire content of another Word document.
Replace searched for text with document elements such as paragraphs, tables, and content controls, while maintaining their source formatting.
Find and retrieve text with formatting (bold, highlight) for replacement in occurrences of other placeholders in a Word document, while preserving that formatting.
Replace all occurances of specified text extending across several paragraphs with replacement text or document elements.
Unlock advanced find functionalities in the Syncfusion .NET Word Library (DocIO) to find Word document elements such as images, shapes, and content controls based on their properties.
With this functionality, you can:
//Load an existing Word document.
using FileStream inputDocumentStream = new FileStream("Template.docx", FileMode.Open, FileAccess.ReadWrite);
using WordDocument document = new WordDocument(inputDocumentStream, FormatType.Docx);
//Find all the inline content controls by Title and Tag.
string[] propertyNames = new string[] { "ContentControlProperties.Title", "ContentControlProperties.Tag" };
string[] propertyValues = new string[] { "Contact", "Contact" };
List<Entity> inlineContentControls = document.FindAllItemsByProperties(EntityType.InlineContentControl,propertyNames,propertyValues);
//Iterate the inline content controls.
for (int i = 0; i < inlineContentControls.Count; i++)
{
InlineContentControl inlineContentControl = inlineContentControls[i] as InlineContentControl;
// To do : Modify the retrieved elements.
}
//Save the Word document.
using FileStream outputDocumentStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite);
document.Save(outputDocumentStream, FormatType.Docx);
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.