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.
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
- 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
-
1Install IronWord with NuGet Package Manager
-
2Copy 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");C# -
3Deploy to test on your live environment
Start using IronWord in your project today with a free trial
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.
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");Imports IronWord
' Load a DOCX document
Dim doc As 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.
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");Imports IronWord
' Load a DOCX document
Dim doc As 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.
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");Imports IronWord
' Load the document
Dim doc As New WordDocument("sample.docx")
' Find and remove erroneous text
Dim 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.
How Does FindText Handle Multiple Occurrences?

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 can I remove text from a Word DOCX file in C# using IronWord?
IronWord provides multiple methods to remove text from Word DOCX files in C#, such as removing entire paragraphs, specific text runs, and finding text by content before deletion, while maintaining the document's structure and formatting.
What is the process for removing an entire paragraph from a Word document?
With IronWord, you can remove an entire paragraph by accessing it through the `Paragraphs` collection using its index. Use the `Remove()` method on the desired paragraph, and the document structure will automatically adjust to fill the gap.
Can IronWord remove specific formatted text within a paragraph?
Yes, IronWord allows you to remove specific formatted text within a paragraph by targeting text runs. Access the `Texts` collection and call `Remove()` on the targeted text run.
How does the `FindText` method work in IronWord?
The `FindText` method in IronWord allows you to locate and remove text based on matching content rather than position. It performs a case-sensitive search and returns the matching text element for removal.
Does IronWord preserve document formatting when removing text?
Yes, IronWord preserves the document's formatting and structural integrity when removing text, ensuring that surrounding content remains unchanged.
How do I implement text removal for multiple occurrences using IronWord?
For multiple occurrences, use a loop with the `FindText` method to iteratively search and remove each occurrence of the desired text until none remain, ensuring comprehensive text removal.
Is it possible to remove text by content without knowing its position in the document?
Yes, using IronWord's `FindText` method, you can dynamically remove text by its content without needing to know its exact position in the document.
What happens to a document's structure when a paragraph is removed using IronWord?
When a paragraph is removed using IronWord, the document structure adjusts so that subsequent paragraphs shift up to fill the gap, maintaining the integrity of the document layout.
Can IronWord selectively remove formatted segments within a paragraph?
Absolutely, IronWord provides granular control by allowing you to target and remove specific formatted text runs within a paragraph, without disrupting the overall paragraph formatting.
Why is using IronWord advantageous for text removal in DOCX files?
IronWord is advantageous for text removal in DOCX files because it provides precise control over text content, maintains document formatting, and allows dynamic content manipulation, ensuring professionalism and consistency.

Ahmad is a full-stack developer with a strong foundation in C#, Python, and web technologies. He has a deep interest in building scalable software solutions and enjoys exploring how design and functionality meet in real-world applications.