使用 GroupDocs.Editor 編輯 DOCX 及轉換格式的方式
Effortlessly manage and transform Word documents within your .NET environment using GroupDocs.Editor .NET. In this tutorial you’ll discover how to edit docx files, convert them to formats such as RTF, DOCM, and plain text, and automate your document workflow for maximum productivity.
快速解答
- 需要的程式庫是什麼? GroupDocs.Editor for .NET (20.10+).
- 我可以將 DOCX 轉換為 RTF 嗎? 是的 – 使用
WordProcessingSaveOptions搭配WordProcessingFormats.Rtf。 - 支援儲存為 TXT 嗎? 當然,透過
TextSaveOptions。 - 我需要授權嗎? 試用版可用於開發;授權可解鎖所有功能。
- 如何處理大量檔案? 可將程式碼與 async/await 或 Parallel.ForEach 結合,以進行批次處理。
使用 GroupDocs.Editor 編輯 docx 是什麼?
GroupDocs.Editor 會載入 DOCX,將其內容以可編輯的 HTML 形式呈現,讓您以程式方式修改該 HTML,然後將結果儲存回任何支援的格式。此方式免除 Office interop 的需求,且可在任何伺服器端 .NET 平台上運作。
為何在文件工作流程自動化中使用 GroupDocs.Editor?
- 僅限伺服器端 – 無需安裝 Office。
- 多種輸出格式 – RTF、DOCM、TXT、HTML、PDF 等。
- 高效能 – 輕量級 API,專為批次情境設計。
- 完整控制 – 在儲存前編輯、取代或注入 HTML 片段。
先決條件
- GroupDocs.Editor 程式庫(版本 20.10 或更新)
- .NET Framework 4.7.2 + 或 .NET 5/6
- Visual Studio(任何近期版本)
- 基本的 C# 知識與檔案 I/O 熟悉度
安裝 GroupDocs.Editor
您可以透過 .NET CLI、Package Manager Console 或 NuGet UI 來加入此套件。
dotnet add package GroupDocs.Editor
Install-Package GroupDocs.Editor
取得授權
先使用免費試用版或申請臨時授權金鑰。若用於正式環境,請購買授權以解鎖無限制的處理功能。
如何載入與編輯 DOCX 文件
首先,將編輯器指向來源檔案,接著取得可編輯的 HTML,進行所需的變更,最後從修改後的標記建立新的 EditableDocument。
using (Editor editor = new Editor("YOUR_DOCUMENT_DIRECTORY/sample.docx", new Options.WordProcessingLoadOptions()))
{
// Your document manipulation code here
}
逐步程式碼說明
- 定義輸入檔案路徑
string inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
- 建立
Editor實例
using GroupDocs.Editor;
using GroupDocs.Editor.Options;
Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions());
- 編輯文件的 HTML
EditableDocument defaultWordProcessingDoc = editor.Edit();
string allEmbeddedInsideString = defaultWordProcessingDoc.GetEmbeddedHtml();
string allEmbeddedInsideStringEdited = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");
- 從編輯後的 HTML 建立新的
EditableDocument
EditableDocument editedDoc = EditableDocument.FromMarkup(allEmbeddedInsideStringEdited, null);
- 釋放暫時物件
editedDoc.Dispose();
defaultWordProcessingDoc.Dispose();
editor.Dispose();
如何將 Word 轉換為 RTF
使用適當的儲存選項即可輕鬆將編輯後的文件儲存為 RTF。
string outputRtfPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.rtf");
WordProcessingSaveOptions rtfSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
editor.Save(editedDoc, outputRtfPath, rtfSaveOptions);
}
editor.Dispose();
如何使用 Stream 將 DOCX 儲存為 DOCM
當需要支援巨集的 DOCM 格式時,您可以直接寫入 FileStream。
string outputDocmPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.docm");
WordProcessingSaveOptions docmSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
using (FileStream outputStream = File.Create(outputDocmPath))
{
editor.Save(editedDoc, outputStream, docmSaveOptions);
}
}
editor.Dispose();
如何將 DOCX 轉換為 TXT(純文字)
純文字抽取對於索引、搜尋或電子郵件通知很有用。
string outputTxtPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.txt");
TextSaveOptions textSaveOptions = new TextSaveOptions()
{
Encoding = Encoding.UTF8,
PreserveTableLayout = true
};
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
editor.Save(editedDoc, outputTxtPath, textSaveOptions);
}
editor.Dispose();
常見使用案例與實務情境
- 自動化報告產生: 將分析性的 Word 報告轉換為 TXT 以進行電子郵件分發。
- 協同編輯平台: 讓使用者編輯 HTML 片段,並將結果儲存為 DOCM 或 RTF。
- 文件歸檔: 將舊版 DOCX 檔案遷移至 DOCM,以支援巨集同時保留內容。
- Web 服務: 即時串流 DOCX → PDF/RTF 轉換,無需暫存檔案。
批次處理 Word 文件的效能技巧
- 即時釋放: 必須對
Editor與文件物件呼叫Dispose()。 - 明智載入: 選擇最具體的
LoadOptions,避免不必要的解析。 - 安全平行化: 使用
Parallel.ForEach,每個執行緒使用獨立的Editor實例。 - 避免大型記憶體字串: 處理巨型文件時,使用串流而非完整 HTML 字串。
常見問題
Q: 我可以編輯受密碼保護的 DOCX 嗎?
A: 可以。建立 Editor 時,透過 WordProcessingLoadOptions.Password 提供密碼。
Q: 是否可以一次執行多個檔案的轉換?
A: 當然。將單一檔案的邏輯包在迴圈中或使用 Parallel.ForEach 進行並行處理。
Q: GroupDocs.Editor 支援 .NET Core 嗎?
A: 此程式庫可在 .NET 5、.NET 6、.NET Core 3.1 以及完整的 .NET Framework 上運作。
Q: 儲存為 DOCM 時巨集會發生什麼事?
A: 使用 Docm 儲存格式時會保留巨集;而 RTF/TXT 會將其移除。
Q: 大批次作業時如何排除 “OutOfMemoryException”?
A: 將檔案分成較小批次處理,立即釋放物件,並考慮提升應用程式的記憶體上限。
結論
您現在已擁有完整、可投入生產的工作流程,使用 GroupDocs.Editor .NET 來 編輯 docx 檔案並將其轉換為 RTF、DOCM 或純文字。依照上述步驟,您可以自動化文件工作流程、處理批次作業,並將無縫的格式轉換整合至任何 .NET 應用程式中。
最後更新: 2026-04-07
測試使用: GroupDocs.Editor 20.10 (latest at time of writing)
作者: GroupDocs