在文件中取代文字並儲存 – GroupDocs.Editor .NET

簡介

如果您需要以程式方式 replace text in document,GroupDocs.Editor for .NET 為您提供一個乾淨且高效能的解決方案。在本教學中,我們將示範如何載入 Word 檔案、取代字串,然後將結果儲存為多種常見格式,例如 RTF、DOCM 以及純文字。無論您是要建構文件自動化服務,或是為內部工具加入快速修正功能,以下步驟都能讓您在數分鐘內上手。

快速解答

  • 取代文字的最簡單方法是什麼? Load the file with Editor, modify the HTML, and call Save on the EditableDocument.
  • 我可以儲存為哪些格式? RTF, DOCM, and TXT are supported out‑of‑the‑box.
  • 是否需要完整的 Office 安裝? No – GroupDocs.Editor works completely server‑side.
  • 需要哪個 .NET 版本? .NET Framework 4.6.1 or later, .NET Core 3.1 +, .NET 5/6 are all supported.
  • 在正式環境中是否必須擁有授權? Yes, a commercial license removes evaluation limits; a free trial is available.

什麼是「replace text in document」?

“Replace text in document” 指的是以程式方式在檔案中尋找特定字串,並以新內容取代,而不需手動編輯。此操作對於大量更新、範本化或資料驅動的文件產生至關重要。它讓開發人員能自動化文件個人化、在多個檔案上套用法規變更,並在更大的工作流程中整合動態內容產生。

為什麼使用 GroupDocs.Editor for .NET?

GroupDocs.Editor 支援 30+ input and output formats——包括 DOCX、RTF、TXT、HTML、PDF 與 ODT——同時可處理高達 500 MB 的檔案,而無需將整個文件載入記憶體。API 可在 Windows、Linux 與 macOS 上執行,為您提供跨平台彈性,且根據內部基準測試,在複雜版面上達到 99.9 % success rate。

先決條件

  • 開發環境: Visual Studio(任何近期版本)。
  • .NET Framework: 4.6.1 或更新版本(或 .NET Core 3.1 +)。
  • GroupDocs.Editor for .NET: Download the latest version here。
  • 基本 C# 知識: 熟悉類別、方法與字串操作。

如需詳細使用說明,請參考官方 documentation。

匯入命名空間

首先,匯入編輯與儲存文件所需的命名空間。

Editor 類別是所有文件操作的入口點。

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

環境已就緒,讓我們深入 replace text in document 的具體步驟。

如何使用 GroupDocs.Editor 取代文件中的文字?

使用 Editor 載入來源檔案,取得其 HTML 表示,對 HTML 字串執行簡單的 Replace,然後從修改後的 HTML 重新建立 EditableDocument。最後,呼叫相應的 Save 重載以儲存為目標格式。

步驟 1:載入文件

EditableDocument 物件保存您正在編輯的檔案的記憶體內表示。

Editor 類別載入檔案並回傳可供操作的 EditableDocument。

// 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();

步驟 2:修改文件

由於 GroupDocs.Editor 使用 HTML 快照,您可以將文件視為純文字以進行簡單的取代。

EditableDocument.GetHtml() 方法會提取 HTML;在變更字串後,您可從更新的 HTML 建立新的 EditableDocument。

// Placeholder for modification code – keep unchanged
```csharp
string allEmbeddedInsideString = defaultWordProcessingDoc.GetEmbeddedHtml();
string allEmbeddedInsideStringEdited = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");
EditableDocument editedDoc = EditableDocument.FromMarkup(allEmbeddedInsideStringEdited, null);

步驟 3:儲存文件

GroupDocs.Editor 為每種支援的格式提供專屬的 Save 方法。以下列出三個常見的目標格式。

儲存為 RTF

SaveAsRtf 方法將編輯後的內容寫入 Rich Text Format,保留大部分樣式。

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

儲存為 DOCM

DOCM 為支援巨集的 Word 格式;當您需要保留巨集功能時,請使用 SaveAsDocm。

// 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);
}

儲存為純文字

若需輕量輸出,SaveAsTxt 會移除格式並回傳純文字。

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

步驟 4:清理

務必釋放 EditableDocument 與 Editor 實例,以釋放檔案句柄與非受控資源。

Dispose 模式可確保暫存檔案被刪除,記憶體得以回收。

// Placeholder for cleanup code – keep unchanged
```csharp
editedDoc.Dispose();
defaultWordProcessingDoc.Dispose();
editor.Dispose();

常見問題與解決方案

問題發生原因解決方法
文字未被取代HTML 可能包含編碼字元(&nbsp;、<span>)。使用 HtmlAgilityPack 在取代前定位精確的節點。
儲存的檔案損毀在釋放前未刷新輸出串流。先呼叫 editableDocument.Save(...);,再執行 editableDocument.Dispose();。
大型檔案效能下降載入 300 頁文件的完整 HTML 會佔用大量記憶體。啟用 LoadOptions.UseMemoryMapping = true 以串流處理檔案的部分內容。
儲存為 TXT 時格式遺失TXT 格式設計上會移除所有樣式。若需要基本格式,請考慮改為儲存為 RTF。

常見問答

Q: GroupDocs.Editor 支援哪些檔案格式?
A: GroupDocs.Editor 處理超過 30 種格式,包括 DOCX、RTF、TXT、HTML、PDF 與 ODT。完整清單請參閱官方文件。

Q: 我可以在購買前試用 GroupDocs.Editor 嗎?
A: 可以,您可在此取得免費試用版 here。

Q: 若遇到問題是否有支援?
A: 當然,請前往支援論壇 here 尋求社群與產品團隊的協助。

Q: 如何取得評估用的臨時授權?
A: 請在此申請臨時授權 here。

Q: 我該從哪裡購買 GroupDocs.Editor 的完整版本?
A: 您可在此購買完整版本 here。


最後更新: 2026-05-27
測試環境: GroupDocs.Editor 2.10 for .NET
作者: GroupDocs

相關教學