How to Remove Text from DOCX in C#
IronWord provides multiple methods to remove text from Word documents in C#, including removing entire paragraphs, specific text runs, and finding text by content before deletion, all while preserving document structure and formatting integrity.
Get started with IronWord
How to Remove Text from DOCX
- Download a C# library for removing text from Word Documents
- Load an existing Word document
- Identify the text content that needs to be removed
- Remove the selected text with
Removemethod - Save the changes and export the updated document as a DOCX file
Quickstart: Remove Text from DOCX
- Install IronWord via NuGet Package Manager
- Load your DOCX file using
WordDocument - Access paragraphs or text runs by index
- Call
Remove()on the target element - Save the modified document
Get started making PDFs with NuGet now:
Install IronWord with NuGet Package Manager
Copy and run this code snippet.
using IronWord; // Load document WordDocument doc = new WordDocument("document.docx"); // Remove second paragraph doc.Paragraphs[1].Remove(); // Save changes doc.SaveAs("document_modified.docx");Deploy to test on your live environment
How Do I Remove an Entire Paragraph from a Word Document?
To remove a paragraph from a Word document, access it through the Paragraphs collection using its index position. This example removes the paragraph at index 1, which deletes all its content and formatting. The document structure automatically adjusts after removal, with subsequent paragraphs shifting up to fill the gap.
When working with paragraph removal, remember that indexing is zero-based. The first paragraph is at index 0, the second at index 1, and so on. The Remove() method completely eliminates the paragraph element from the document structure, including all text runs, formatting, and any embedded content like images or hyperlinks within that paragraph.
:path=/static-assets/word/content-code-examples/how-to/remove-text-simple.csusing 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");What Does the Document Look Like Before Removal?

How Does the Document Structure Change After Removal?

The removal operation maintains the document's overall formatting and structure. The remaining paragraphs retain their original colors and formatting properties. This is a key advantage of using IronWord's removal methods—they preserve the integrity of surrounding content while cleanly removing the targeted elements.
How Can I Remove Specific Formatted Text Within a Paragraph?
A paragraph can contain multiple text runs, where each run represents text with consistent formatting properties like bold, italic, or color. Understanding text runs is crucial for precise content manipulation. Each time the formatting changes within a paragraph, a new text run begins. For example, if you have a sentence where one word is bold, you typically have three text runs: the text before the bold word, the bold word itself, and the text after.
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. This granular control allows you to preserve paragraph structure while removing only specific formatted segments.
:path=/static-assets/word/content-code-examples/how-to/remove-text-text-run.csusing 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");When working with text runs, remember that removing a text run doesn't affect the formatting of surrounding runs. Each text run maintains its formatting properties independently. This makes it possible to surgically remove specific formatted content without disrupting the visual appearance of the remaining text.
What Happens to Text Runs Before Removal?

How Does Removing a Text Run Affect Paragraph Formatting?

As shown in the images, removing a specific text run maintains the formatting integrity of the remaining content. The underlined text and other formatting properties remain unchanged, demonstrating the precision of text run manipulation.
How Do I Find and Remove Text by Content Rather Than Position?
The FindText method locates and removes content dynamically based on text matching rather than position. This is particularly useful for removing invalid text, placeholders, or specific content anywhere in the document without knowing its exact location. The method returns the matching text element or null if not found, allowing safe removal with a null check before calling Remove.
This approach is invaluable when dealing with template documents where placeholder text needs removal, or when cleaning up documents with known error patterns. The FindText method performs a case-sensitive search by default, ensuring precise matching of the target content.
:path=/static-assets/word/content-code-examples/how-to/remove-text-find.csusing 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");The null-conditional operator (?.) in the code above is crucial for safe execution. If the text isn't found, FindText returns null, and the null-conditional operator prevents a NullReferenceException. This pattern makes your code more robust when dealing with documents where the target text might not always be present.
What Content Should I Look for Before Using Find and Remove?

How Does FindText Handle Multiple Occurrences?

When using FindText, note that it returns the first occurrence of the matching text. If you need to remove all occurrences of specific text throughout the document, implement a loop that continues searching and removing until no more matches are found. This iterative approach ensures comprehensive text removal across the entire document.
The find and remove functionality preserves the document's formatting and structure outside the removed text. Paragraph breaks, formatting of surrounding text, and document styles remain intact, making it a safe operation for maintaining document consistency while removing unwanted content.
Frequently Asked Questions
How do I remove an entire paragraph from a Word document in C#?
With IronWord, you can remove a paragraph by accessing it through the Paragraphs collection using its index position and calling the Remove() method. For example, doc.Paragraphs[1].Remove() removes the second paragraph (index 1). The document structure automatically adjusts after removal, with subsequent paragraphs shifting up to fill the gap.
What happens to the document structure when I remove text?
IronWord maintains the document's overall formatting and structure when removing text. The remaining paragraphs retain their original colors and formatting properties, and the document automatically reflows to eliminate gaps. This ensures the integrity of surrounding content is preserved while cleanly removing targeted elements.
Can I remove specific formatted text within a paragraph instead of the entire paragraph?
Yes, IronWord allows you to remove specific text runs within a paragraph. Since paragraphs can contain multiple text runs (each representing text with consistent formatting), you can target and remove individual runs while preserving the rest of the paragraph's content and structure.
What is a text run in Word documents?
In IronWord, a text run represents a portion of text within a paragraph that has consistent formatting properties like bold, italic, or color. Each time the formatting changes within a paragraph, a new text run begins. Understanding text runs is crucial for precise content manipulation when working with Word documents.
How do I save changes after removing text from a Word document?
After removing text using IronWord, you can save the modified document by calling the SaveAs() method. For example, doc.SaveAs("document_modified.docx") saves the updated document with all removal changes applied. This creates a new file while preserving the original document.
Does removing paragraphs also delete embedded content like images or hyperlinks?
Yes, when you use IronWord's Remove() method on a paragraph, it completely eliminates the paragraph element from the document structure, including all text runs, formatting, and any embedded content such as images or hyperlinks within that paragraph.






