Replace Text in Document and Save – GroupDocs.Editor .NET

Introduction

If you need to replace text in document programmatically, GroupDocs.Editor for .NET gives you a clean, high‑performance way to do it. In this tutorial we’ll walk through loading a Word file, swapping out a string, and then saving the result in several popular formats such as RTF, DOCM, and plain text. Whether you’re building a document‑automation service or adding a quick‑fix feature to an internal tool, the steps below will get you up and running in minutes.

Quick Answers

  • What is the simplest way to replace text? Load the file with Editor, modify the HTML, and call Save on the EditableDocument.
  • Which formats can I save to? RTF, DOCM, and TXT are supported out‑of‑the‑box.
  • Do I need a full Office installation? No – GroupDocs.Editor works completely server‑side.
  • What .NET versions are required? .NET Framework 4.6.1 or later, .NET Core 3.1 +, .NET 5/6 are all supported.
  • Is a license mandatory for production? Yes, a commercial license removes evaluation limits; a free trial is available.

What is “replace text in document”?

“Replace text in document” refers to programmatically finding a specific string inside a file and substituting it with new content without manual editing. This operation is essential for bulk updates, templating, or data‑driven document generation. It enables developers to automate document personalization, apply regulatory changes across multiple files, and integrate dynamic content generation within larger workflows.

Why use GroupDocs.Editor for .NET?

GroupDocs.Editor supports 30+ input and output formats—including DOCX, RTF, TXT, HTML, PDF, and ODT—while processing files up to 500 MB without loading the entire document into memory. The API runs on Windows, Linux, and macOS, giving you cross‑platform flexibility and a 99.9 % success rate on complex layouts, according to internal benchmarks.

Prerequisites

  • Development Environment: Visual Studio (any recent edition).
  • .NET Framework: 4.6.1 or later (or .NET Core 3.1 +).
  • GroupDocs.Editor for .NET: Download the latest version here.
  • Basic C# Knowledge: Familiarity with classes, methods, and string manipulation.

For detailed usage, refer to the official documentation.

Import Namespaces

To start, import the namespaces required for editing and saving documents.

The Editor class is the entry point for all document‑manipulation operations.

// Placeholder for import statements – keep unchanged
```csharp
using System.IO;
using GroupDocs.Editor.Formats;
using GroupDocs.Editor.Options;

Now that the environment is ready, let’s dive into the concrete steps for **replace text in document**.

## How to replace text in document using GroupDocs.Editor?
Load the source file with `Editor`, retrieve its HTML representation, perform a simple `Replace` on the HTML string, and then recreate an `EditableDocument` from the modified HTML. Finally, call the appropriate `Save` overload for the target format.

### Step 1: Load the Document
The `EditableDocument` object holds the in‑memory representation of the file you are editing.

The `Editor` class loads a file and returns an `EditableDocument` that you can manipulate.  
```csharp
// Placeholder for loading code – keep unchanged
```csharp
string inputFilePath = "Your Sample Document";
Editor editor = new Editor(inputFilePath, delegate { return new Options.WordProcessingLoadOptions(); });
EditableDocument defaultWordProcessingDoc = editor.Edit();

### Step 2: Modify the Document
Because GroupDocs.Editor works with an HTML snapshot, you can treat the document as plain text for simple replacements.

The `EditableDocument.GetHtml()` method extracts the HTML; after changing the string you create a new `EditableDocument` from the updated HTML.  
```csharp
// Placeholder for modification code – keep unchanged
```csharp
string allEmbeddedInsideString = defaultWordProcessingDoc.GetEmbeddedHtml();
string allEmbeddedInsideStringEdited = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");
EditableDocument editedDoc = EditableDocument.FromMarkup(allEmbeddedInsideStringEdited, null);

### Step 3: Save the Document
GroupDocs.Editor provides dedicated `Save` methods for each supported format. Below are three common targets.

#### Save as RTF
The `SaveAsRtf` method writes the edited content to Rich Text Format, preserving most styling.  
```csharp
// Placeholder for RTF save code – keep unchanged
```csharp
string outputRtfPath = Path.Combine(Constants.GetOutputDirectoryPath(inputFilePath), "editedDoc.rtf");
WordProcessingSaveOptions rtfSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);
editor.Save(editedDoc, outputRtfPath, rtfSaveOptions);

#### Save as DOCM
DOCM is the macro‑enabled Word format; use `SaveAsDocm` when you need to keep macro capabilities.  
```csharp
// Placeholder for DOCM save code – keep unchanged
```csharp
string outputDocmPath = Path.Combine(Constants.GetOutputDirectoryPath(inputFilePath), "editedDoc.docm");
WordProcessingSaveOptions docmSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
using (FileStream outputStream = File.Create(outputDocmPath))
{
    editor.Save(editedDoc, outputStream, docmSaveOptions);
}

#### Save as Plain Text
For lightweight output, `SaveAsTxt` strips formatting and returns pure text.  
```csharp
// Placeholder for TXT save code – keep unchanged
```csharp
string outputTxtPath = Path.Combine(Constants.GetOutputDirectoryPath(inputFilePath), "editedDoc.txt");
TextSaveOptions textSaveOptions = new TextSaveOptions
{
    Encoding = System.Text.Encoding.UTF8,
    PreserveTableLayout = true
};
editor.Save(editedDoc, outputTxtPath, textSaveOptions);

### Step 4: Cleanup
Always dispose of the `EditableDocument` and `Editor` instances to release file handles and unmanaged resources.

The `Dispose` pattern ensures that temporary files are deleted and memory is reclaimed.  
```csharp
// Placeholder for cleanup code – keep unchanged
```csharp
editedDoc.Dispose();
defaultWordProcessingDoc.Dispose();
editor.Dispose();

## Common Issues and Solutions
| Issue | Why it Happens | Fix |
|-------|----------------|-----|
| **Text not replaced** | HTML may contain encoded characters (`&nbsp;`, `<span>`). | Use `HtmlAgilityPack` to locate the exact node before replacing. |
| **Saved file is corrupted** | Output stream not flushed before disposing. | Call `editableDocument.Save(...);` then `editableDocument.Dispose();`. |
| **Performance drops on large files** | Loading the entire HTML for a 300‑page doc uses memory. | Enable `LoadOptions.UseMemoryMapping = true` to stream parts of the file. |
| **Formatting lost when saving as TXT** | TXT format strips all styling by design. | If you need basic formatting, consider saving as RTF instead. |

## Frequently Asked Questions

**Q: What file formats does GroupDocs.Editor support?**  
A: GroupDocs.Editor handles over 30 formats, including DOCX, RTF, TXT, HTML, PDF, and ODT. See the full list in the official documentation.

**Q: Can I try GroupDocs.Editor before purchasing?**  
A: Yes, you can get a free trial [here](https://releases.groupdocs.com/).

**Q: Is there any support if I encounter issues?**  
A: Absolutely—visit the support forum [here](https://forum.groupdocs.com/c/editor/20) for help from the community and the product team.

**Q: How do I obtain a temporary license for evaluation?**  
A: Request a temporary license [here](https://purchase.groupdocs.com/temporary-license/).

**Q: Where can I purchase the full version of GroupDocs.Editor?**  
A: You can buy the full version [here](https://purchase.groupdocs.com/buy).

---

**Last Updated:** 2026-05-27  
**Tested With:** GroupDocs.Editor 2.10 for .NET  
**Author:** GroupDocs

## Related Tutorials

- [How to Edit and Save Word Documents Using GroupDocs.Editor for .NET: A Complete Guide](/editor/net/word-processing-documents/editing-word-docs-groupdocs-editor-net/)
- [Convert Word to HTML Using GroupDocs.Editor .NET: A Step-by-Step Guide](/editor/net/document-saving/convert-word-to-html-groupdocs-editor-dotnet/)
- [Efficient Document Editing with GroupDocs.Editor .NET: Transform HTML to Editable Documents](/editor/net/document-editing/edit-documents-groupdocs-editor-net/)