IRONSOFTWAREHOME

How to Remove Text from DOCX in C#

Ahmad Sohail
Ahmad Sohail
Updated: August 2, 2026

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.

Quickstart: Remove Text from DOCX
  1. Install IronWord via NuGet Package Manager
  2. Load your DOCX file using WordDocument
  3. Access paragraphs or text runs by index
  4. Call Remove() on the target element
  5. Save the modified document
  1. 1Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. 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#
  3. 3Deploy to test on your live environment

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

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");

What Does the Document Look Like Before Removal?

Word document with three colored paragraphs before text removal, showing formatting ribbon and paragraph tools

How Does the Document Structure Change After Removal?

Word document showing three colored Lorem ipsum paragraphs demonstrating text formatting before paragraph 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");

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?

Word document showing text with underlined formatting before text run removal operation

How Does Removing a Text Run Affect Paragraph Formatting?

Word document showing formatted paragraph with underlined text after specific text run removal

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");

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?

Word document showing Lorem ipsum text with 'ERRONEOUS TEXT IS HERE' in bold before using find and replace

How Does FindText Handle Multiple Occurrences?

Word document showing underlined text sections after find and remove operation on Lorem ipsum content

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 Sohail
Full Stack Developer

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.

...
Read More

Ready to Get Started?

Nuget Downloads 56,401Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronWord
nuget.org/packages/IronWord/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronWord"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronWord to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronWord.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required