How to Edit Text in a Word Document

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

Editing text in Word documents is crucial for content updates and maintaining documents. IronWord provides direct access to paragraph text runs, allowing precise modifications to existing DOCX content programmatically.

This tutorial demonstrates how to edit text in DOCX files using C# and IronWord.

Get started with IronWord

今天在您的项目中使用 IronWord,免费试用。

第一步:
green arrow pointer


Edit Text Example

To edit text in a Word document, load the existing file and access the target paragraph. The paragraph exposes a Texts array, and Texts[0] property references the first text run within that paragraph. Text runs are segments of text with consistent formatting.

提示If a paragraph contains multiple formatting styles, each distinct segment is a separate text run.

:path=/static-assets/word/content-code-examples/how-to/edit-text-simple.cs
using IronWord;

// Load a DOCX document
WordDocument doc = new WordDocument("raw_document.docx");

// Edit existing text on the first paragraph
doc.Paragraphs[0].Texts[0].Text = "First paragraph is updated successfully";

// Export docx
doc.SaveAs("edited_document.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Before Modification

Document before text modification

After Modification

Document after text modification

Edit Specific Text Run

When a paragraph contains multiple text runs with different formatting, you can target specific runs using array indexing. Text runs use zero-based indexing, where Texts[0] is the first run, Texts[1] is the second, and so on. In this example, we target the second text run of the fourth paragraph using Paragraphs[3].Texts[1].

:path=/static-assets/word/content-code-examples/how-to/edit-text-text-run.cs
using IronWord;

// Load the document
WordDocument doc = new WordDocument("text_document.docx");

// Edit second text run in the 4th paragraph
doc.Paragraphs[3].Texts[1].Text = "Edited the textrun successfully within the same paragraph";

// Save to the same file
doc.SaveAs("text_document.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Before Modification

Document before text run modification

After Modification

Document after text run modification

常见问题解答

什么是 IronWord?

IronWord 是一个功能强大的 C# 库,专为操作 DOCX 文件而设计,允许用户以编程方式编辑和修改 Word 文档中的文本。

如何使用 IronWord 编辑 DOCX 文件中的文本?

要使用 IronWord 编辑 DOCX 文件中的文本,请加载现有文件,确定包含要修改的文本的段落,然后使用 `Texts` 数组进行更改,最后导出更新后的文件。

Word文档中的文本段是什么?

段落中格式一致的文本段称为文本单元。每个段落可以包含多个文本单元,尤其是在应用不同格式样式的情况下。

如何使用 IronWord 定位段落中的特定文本?

您可以使用从零开始的索引来定位特定的文本段。例如,要编辑第四段中的第二个文本段,请使用 `Paragraphs[3].Texts[1]`。

在Word文档中编辑文本有何重要性?

在 Word 文档中编辑文本对于更新内容以及保持文档的准确性和相关性至关重要。

IronWord能否处理段落内多种格式样式?

是的,IronWord 可以处理段落内的多种格式样式,它将每个不同的段落视为单独的文本段,可以单独访问和修改。

是否可以使用 C# 以编程方式访问 Word 文档?

是的,您可以借助 IronWord 等库,使用 C# 以编程方式访问和操作 Word 文档。

Ahmad Sohail
全栈开发者

Ahmad 是一名全栈开发人员,拥有扎实的 C#、Python 和 Web 技术基础。他对构建可扩展的软件解决方案深感兴趣,并喜欢探索设计和功能在实际应用中如何结合。

在加入 Iron Software 团队之前,Ahmad 致力于自动化项目和 API 集成,专注于提高性能和开发人员体验。

在业余时间,他喜欢尝试 UI/UX 想法,贡献开源工具,并偶尔从事技术写作和文档工作,以便让复杂主题更易于理解。

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