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

Nutzen Sie IronWord heute kostenlos in Ihrem Projekt.

Erster Schritt:
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

Häufig gestellte Fragen

Was ist der Hauptzweck des Entfernens von Text aus DOCX-Dateien?

Das Entfernen von Text aus DOCX-Dateien ist für die Dokumentenbereinigung, Schwärzung und Inhaltsverwaltung unerlässlich, um sicherzustellen, dass das Dokument seine Struktur und Integrität beibehält.

Wie kann ich mit IronWord Text aus Word-Dokumenten entfernen?

Um mit IronWord zu beginnen, laden Sie die C#-Bibliothek herunter, laden Sie das vorhandene Word-Dokument, identifizieren Sie den zu entfernenden Textinhalt, verwenden Sie die Remove-Methode und speichern Sie das aktualisierte Dokument.

Kann IronWord ganze Absätze aus einem DOCX-Dokument entfernen?

Ja, IronWord ermöglicht es Ihnen, ganze Absätze zu entfernen, indem Sie über die Absatzsammlung darauf zugreifen und den gewünschten Absatz an seiner Indexposition löschen.

Ist es mit IronWord möglich, bestimmte Textabschnitte innerhalb eines Absatzes zu entfernen?

Ja, Sie können bestimmte Textabschnitte innerhalb eines Absatzes entfernen, indem Sie auf die Sammlung „Texte“ zugreifen und den Textabschnitt anhand seines Index auswählen. Dadurch können Sie formatierte Inhalte löschen, ohne den Rest des Absatzes zu beeinträchtigen.

Wie funktioniert die FindText-Methode zum Entfernen von Text in IronWord?

Die FindText-Methode lokalisiert und entfernt Inhalte dynamisch anhand von Textübereinstimmungen. Dies ist nützlich, um ungültigen Text, Platzhalter oder bestimmte Inhalte im gesamten Dokument zu löschen.

Was geschieht mit der Dokumentstruktur nach dem Entfernen von Text mit IronWord?

Nach dem Entfernen von Text mit IronWord passt sich die Dokumentstruktur automatisch an, sodass das Gesamtformat und die Integrität des Dokuments erhalten bleiben.

Kann IronWord das Entfernen von Text mit bestimmter Formatierung wie Fettdruck oder Kursivschrift verarbeiten?

Ja, IronWord kann Text mit bestimmten Formatierungen wie Fettdruck oder Kursivschrift entfernen, indem es den spezifischen Textabschnitt innerhalb eines Absatzes auswählt, der die gewünschten Formatierungseigenschaften aufweist.

Was sollte vor der Verwendung der Methode „Entfernen“ in IronWord beachtet werden?

Bevor Sie die Remove-Methode verwenden, stellen Sie sicher, dass das Textelement nicht null ist, um Fehler zu vermeiden, da die FindText-Methode null zurückgibt, wenn kein übereinstimmender Inhalt gefunden wird.

Unterstützt IronWord das Entfernen von Platzhaltern in Word-Dokumenten?

Ja, IronWord unterstützt das Entfernen von Platzhaltern durch die Verwendung der FindText-Methode, um Platzhalterinhalte im Dokument zu finden und zu entfernen.

Ahmad Sohail
Full-Stack-Entwickler

Ahmad ist ein Full-Stack-Entwickler mit einer soliden Grundlage in C#, Python und Webtechnologien. Er hat ein großes Interesse am Aufbau skalierbarer Softwarelösungen und genießt es, zu erkunden, wie Design und Funktionalität in realen Anwendungen aufeinandertreffen.

Bevor er dem Iron Software Team beitrat, arbeitete ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 25,807 | Version: 2025.11 gerade veröffentlicht