
Introduction
Replacing text in Word documents is essential for document editing, automation, and content updates. This guide explains how to find and replace text in Word documents (DOCX, DOC) in C# using Aspose.Words for .NET.
Why Automate Text Replacement in Word?
- Modify large Word documents quickly and efficiently.
- Standardize content updates across multiple documents.
- Use regex-based searching for advanced text pattern matching.
Table of Contents
- Setting Up Word Text Replacement in C#
- Finding and Replacing Text in Word
- Using Regex to Replace Text
- Replacing Text in Headers and Footers
- Batch Process Multiple Word Files
- Getting a Free API License
- Conclusion and Additional Resources
1. Setting Up Word Text Replacement in C#
To perform text replacement in Word documents, we use Aspose.Words for .NET. This library provides:
- Automated text find-and-replace for DOCX, DOC, and other formats.
- Support for regex-based replacements.
- Efficient processing of large Word documents.
Installation
Install via NuGet:
PM> Install-Package Aspose.Words
Alternatively, download the DLL from the Aspose Downloads Page.
2. Finding and Replacing Text in Word
Follow these steps to replace text in a Word document programmatically:
- Load the Word file using the
Document
class. - Define FindReplaceOptions for text replacement.
- Execute text replacement across the document.
Code Example
// Load the document
Document doc = new Document("input.docx");
// Create a Find and Replace options object
FindReplaceOptions options = new FindReplaceOptions();
// Perform the find and replace operation
doc.Range.Replace("old text", "new text", options);
// Save the modified document
doc.Save("output.docx");
This method automates text updates in Word files.
3. Using Regex to Replace Text
For pattern-based text replacement, use regular expressions:
FindReplaceOptions options = new FindReplaceOptions();
doc.Range.Replace(new Regex(@"Example"), "Updated Text", options);
This enables advanced text search-and-replace functionality.
4. Replacing Text in Headers and Footers
Modify headers and footers in Word documents:
foreach (Section section in doc.Sections)
{
HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary];
if (header != null)
{
header.Range.Replace("Old Header", "New Header", options);
}
}
This ensures content updates in all document sections.
5. Batch Process Multiple Word Files
To find and replace text across multiple Word documents, loop through a folder:
string[] files = Directory.GetFiles("input_docs", "*.docx");
foreach (string file in files)
{
Document doc = new Document(file);
doc.Range.Replace("Placeholder", "New Content", options);
doc.Save(file);
}
This method automates bulk text updates.
6. Getting a Free API License
To unlock full Aspose.Words features, request a free temporary license.
For documentation, visit the Aspose.Words Guide or ask queries on the Aspose forum.
7. Conclusion and Additional Resources
Summary
This guide covered:
✅ How to find and replace text in Word using C#
✅ Using regex for advanced text replacement
✅ Modifying headers and footers dynamically
✅ Batch processing multiple Word files
With Aspose.Words for .NET, you can efficiently automate text manipulation in Word documents. Start enhancing document editing workflows today!