How to Replace Text in a Word Document

This article was translated from English: Does it need improvement?
Translated
View the article in English

Automating text replacement in Word documents is a common need, whether you're personalizing templates, updating reports, or managing content in bulk. IronWord is a C# library designed to make this task straightforward and efficient.

A key advantage of IronWord is that it manipulates Word documents directly, without relying on Microsoft Office Interop. This means you don't need Word installed on your server, resulting in a significantly faster, more reliable, and scalable solution for back-end processes and web applications.

In this how-to guide, we'll walk through code examples to show you just how easily you can find and replace text in any Word document.

Quickstart: Replace Text in Word Documents Easily

This quick guide demonstrates how to swiftly replace text in a Word document using IronWord. With just a few lines of C#, you can load a DOCX file, replace specified text, and save the updated document. This efficient process ensures that developers can enhance document automation without the need for Microsoft Office Interop, making it ideal for backend processes and web applications.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy and run this code snippet.

    IronWord.Document doc = new IronWord.Document("sample.docx");
    doc.ReplaceText("oldText", "newText");
    doc.SaveAs("updated.docx");
  3. Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    arrow pointer

Replace Text Example

Replacing text within a Word Document is intuitive with IronWord. First, we load an existing document. Then, we access the paragraphs collection through Paragraphs and use the ReplaceText method on a specific paragraph.

The ReplaceText method takes two string parameters: the text to find and the text to replace it with.

提示 The ReplaceText method is case-sensitive and replaces all instances of the string within the selected paragraph.

提示All object lists used in this example follow zero-based indexing.

Input

In this example, we'll be using this sample Word document that contains two paragraphs, both with the text "old text".

Sample Docx

Code

Here's the code below, we'll be replacing the first paragraph of "old text" with the word "new text".

:path=/static-assets/word/content-code-examples/how-to/replace-words.cs
using IronWord;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Replace the first paragraph's old text with new text
doc.Paragraphs[0].ReplaceText("old text", "new text");

// Save updated Word Document
doc.SaveAs("updated.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Output Docx

As you can see in the output file, only the first paragraph's text has been changed, while the second remains untouched.

Replacing Multiple Text

To replace every occurrence of a word throughout the entire document, loop through theParagraphscollection and apply theReplaceTextmethod to each one.

We'll use the same sample.docx file as before. If the text to be replaced isn't found in a paragraph, no operation is performed, and the loop continues to the following paragraph.

请注意 If the text you wish to replace is not found, no operations will be performed.

Code

Here's the code below, we'll be replacing the first paragraph of "old text" with the word "new text".

:path=/static-assets/word/content-code-examples/how-to/replace-words-multiple.cs
using IronWord;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Loop through each paragraph to find and replace text
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
    // Replace all occurrences of "old text" with "new text" in the current paragraph
    doc.Paragraphs[i].ReplaceText("old text", "new text");
}

// Save updated Word Document
doc.SaveAs("updated.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Output Multiple Docx

As you can see from this updated Word Document, both paragraphs have been replaced with the word "new text".

Find Text

If you need to verify that text exists before performing an operation, you can use the FindText method. This method searches the entire document and returns the first TextElement that matches the search query.

Below is an example of searching for the old text in the Word document mentioned above.

Code

:path=/static-assets/word/content-code-examples/how-to/replace-words-find-text.cs
using IronWord;
using System;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"));
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Find Text Output

As you can see, it returned the matching text.

警告 If FindText returns nothing, the text does not exist in the document. Ensure your search term is spelled correctly and matches the case of the text in the document.

常见问题解答

IronWord的用途是什么?

IronWord是一个用于在Word文档中自动化文本替换的C#库。它简化了个性化模板、更新报告以及批量管理内容的任务。

使用IronWord需要安装Microsoft Word吗?

不,IronWord不依赖于Microsoft Office Interop,因此不需要在服务器上安装Word。这使得它成为更快和更可扩展的Word文档处理解决方案。

如何使用IronWord替换Word文档中的文本?

要使用IronWord替换Word文档中的文本,请加载文档,访问段落,并使用ReplaceText方法将旧文本替换为新文本。

IronWord中的ReplaceText方法是否区分大小写?

是的,IronWord中的ReplaceText方法是区分大小写的,并替换所选段落中指定字符串的所有实例。

我可以使用IronWord替换整个文档中的文本吗?

是的,你可以通过遍历Paragraphs集合并将ReplaceText方法应用于每个段落来替换整个文档中的文本。

如果要替换的文本在段落中不存在,会发生什么?

如果在段落中找不到文本,则不执行任何操作,并循环继续到下一个段落。

如何使用IronWord验证文档中是否存在特定文本?

你可以使用FindText方法搜索整个文档并返回与查询匹配的第一个TextElement。

如果FindText没有返回任何结果,我应该怎么办?

如果FindText没有返回任何结果,这意味着文本在文档中不存在。请确保您的搜索词拼写正确并且与文档中的文本匹配大小写。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布