How to Remove Text from DOCX

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

Removing text and content from Word documents is essential for document cleanup, redaction, and content management tasks. Flexible methods help delete paragraphs, text runs, and other elements from DOCX files while maintaining document structure and integrity.

In this how-to, different ways of removing text using IronWord will be demonstrated.

Get started with IronWord

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

第一步:
green arrow pointer


Remove Text Example

To remove a paragraph from a Word document, access it through the Paragraphs collection using its index position. In this example, we remove the paragraph at index 1, which deletes all its content and formatting. The document structure automatically adjusts after removal.

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

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

// Remove the second paragraph
doc.Paragraphs[1].Remove();

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

Before Removal

Document before paragraph removal

After Removal

Document after paragraph removal

Removing Specific Text Run

A paragraph can contain multiple text runs, where each run represents text with consistent formatting properties like bold, italic, or color. To remove specific formatted content within a paragraph, access the Texts collection and target the desired text run by index. This example removes the third text run (index 2) from the first paragraph, leaving other content intact.

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

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

// Remove the first paragraph's third textrun
doc.Paragraphs[0].Texts[2].Remove();

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

Before Text Run Removal

Document before text run removal

After Text Run Removal

Document after text run removal

Find and Remove Example

The FindText method locates and removes content dynamically based on text matching rather than position. This is useful for removing invalid text, placeholders, or specific content anywhere in the document. The method returns the matching text element or null if not found, allowing safe removal with a null check before calling Remove.

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

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

// Find and remove erroneous text
var textToRemove = doc.FindText("ERRONEOUS TEXT IS HERE.");
textToRemove?.Remove();

// Save the cleaned document
doc.SaveAs("sample_cleaned.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Before Find and Remove

Document before finding and removing text

After Find and Remove

Document after finding and removing text

常见问题解答

从 DOCX 文件中删除文本的主要目的是什么?

从 DOCX 文件中删除文本对于文档清理、编辑和内容管理任务至关重要,可确保文档保持其结构和完整性。

我该如何开始使用 IronWord 从 Word 文档中删除文本?

要开始使用 IronWord,请下载 C# 库,加载现有的 Word 文档,确定要删除的文本内容,使用 Remove 方法,然后保存更新后的文档。

IronWord 能否从 DOCX 文档中删除整个段落?

是的,IronWord 允许您通过访问段落集合并删除所需段落的索引位置来删除整个段落。

是否可以使用 IronWord 删除段落中的特定文本?

是的,您可以通过访问 Texts 集合并按索引定位文本,来删除段落中的特定文本,从而删除格式化内容而不影响段落的其余部分。

IronWord 中 FindText 方法如何用于删除文本?

FindText 方法可根据文本匹配动态定位并删除内容,这对于删除文档中的无效文本、占位符或特定内容非常有用。

使用 IronWord 删除文本后,文档结构会发生什么变化?

使用 IronWord 删除文本后,文档结构会自动调整,确保文档的整体格式和完整性得以保持。

IronWord 能否处理删除具有特定格式(例如粗体或斜体)的文本?

是的,IronWord 可以通过定位段落中具有所需格式属性的特定文本,删除具有特定格式(例如粗体或斜体)的文本。

在 IronWord 中使用 Remove 方法之前应该考虑哪些因素?

在使用 Remove 方法之前,请确保文本元素不为空,以避免出错,因为如果找不到匹配的内容,FindText 方法将返回 null。

IronWord是否支持删除Word文档中的占位符?

是的,IronWord 支持使用 FindText 方法查找并删除文档中的占位符内容,从而删除占位符。

Ahmad Sohail
全栈开发者

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

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

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

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