如何使用 GroupDocs.Editor for .NET 編輯 DOCX 檔案

在當今的數位時代,如何有效編輯 docx 檔案是許多開發者常問的問題。無論是要微調合約、更新報告,或是自動化大量變更,GroupDocs.Editor for .NET 為您提供快速、以程式碼為先的方式來載入、修改並儲存 Word 文件。在本指南中,您將學會如何編輯 DOCX、將 Word 轉換為 HTML,以及將文件儲存為 RTF——全部使用簡潔的 C# 程式碼。

快速解答

  • 什麼函式庫可以在 .NET 中編輯 DOCX? GroupDocs.Editor for .NET.
  • 我可以將 Word 檔案轉換為 HTML 嗎? 可以 – 使用 Edit 方法並取得內嵌的 HTML。
  • 如何將編輯後的檔案儲存為 RTF? 使用 WordProcessingSaveOptions 並指定 Rtf 格式。
  • 是否可以批次文件轉換? 當然可以;遍歷檔案並重複使用相同的 Editor 實例。
  • 生產環境是否需要授權? 試用版可用於測試;付費授權可移除所有限制。

什麼是使用 GroupDocs.Editor 的文件編輯?

GroupDocs.Editor 是一個 .NET 函式庫,抽象化了 Office 檔案格式的複雜性。它讓您可以開啟 DOCX,將其內容以可編輯的 HTML 形式呈現,進行程式化的變更,然後將結果寫回多種格式——包括 DOCX、HTML 與 RTF。

為何使用 GroupDocs.Editor 編輯 DOCX?

  • 不需要安裝 Office – 可在任何伺服器或容器上運行。
  • 高保真度 – 轉換為 HTML 時保留樣式、表格與圖片。
  • 支援批次 – 您可以自動化編輯成千上萬的文件。
  • 跨格式支援 – 可輕鬆 將 docx 轉換 為 HTML 或 RTF,無需額外工具。

前置條件

在深入程式碼之前,請確保您已具備以下條件:

  • GroupDocs.Editor NuGet 套件已安裝(請參閱下方的安裝說明)。
  • .NET 開發環境(Visual Studio、VS Code 或 .NET CLI)。
  • 基本的 C# 知識,並熟悉常見的文件副檔名(DOCX、RTF、HTML)。

設定 GroupDocs.Editor for .NET

首先,將函式庫加入您的專案中。

安裝方式

使用 .NET CLI:

dotnet add package GroupDocs.Editor

使用套件管理員主控台:

Install-Package GroupDocs.Editor

使用 NuGet 套件管理員 UI:

  • 在 Visual Studio 中開啟 NuGet 套件管理員。
  • 搜尋 “GroupDocs.Editor” 並安裝最新版本。

取得授權

您可以先使用免費試用版,或向 GroupDocs 官方網站申請臨時授權。若用於正式環境,請購買授權以解鎖全部功能並移除評估水印。

初始化 Editor

以下是一個最小範例程式,示範如何建立 Editor 實例。

using System;
using GroupDocs.Editor;

class Program
{
    static void Main()
    {
        string inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
        
        try
        {
            // Initialize GroupDocs.Editor
            using (Editor editor = new Editor(inputFilePath))
            {
                Console.WriteLine("GroupDocs.Editor initialized successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Initialization failed: " + ex.Message);
        }
    }
}

實作指南

如何載入 DOCX 文件?

載入檔案是進行任何編輯之前的第一步。

string inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor;
try
{
    // Load the input document
    editor = new Editor(inputFilePath);
}
catch (Exception ex)
{
    Console.WriteLine("Error loading document: " + ex.Message);
}

如何將 Word 轉換為 HTML?

檔案載入後,您可以將其內容以 HTML 形式呈現、進行編輯,之後再重新儲存。

using GroupDocs.Editor;
using GroupDocs.Editor.Options;

Editor editor; // Assume editor is already initialized
EditableDocument beforeEdit = null;
try
{
    // Open the document for editing
    beforeEdit = editor.Edit();
    
    // Retrieve content and resources
    string allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml();
    
    // Modify the embedded HTML content
    string editedContent = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");
}
catch (Exception ex)
{
    Console.WriteLine("Error editing document: " + ex.Message);
}
finally
{
    beforeEdit?.Dispose();
}

如何將文件儲存為 RTF?

在調整完 HTML 後,將其轉回 Word 處理格式——本例為 RTF。

using System;
using System.IO;
using GroupDocs.Editor;
using GroupDocs.Editor.Options;

Editor editor; // Assume editor is already initialized
EditableDocument afterEdit = null;
string outputPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "edited_document.rtf");
try
{
    // Create a new EditableDocument from the edited content
    afterEdit = EditableDocument.FromMarkup(editedContent, null);
    
    // Prepare saving options for RTF format
    WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);
    
    // Save the document to a file
    editor.Save(afterEdit, outputPath, saveOptions);
}
catch (Exception ex)
{
    Console.WriteLine("Error saving document: " + ex.Message);
}
finally
{
    afterEdit?.Dispose();
    editor.Dispose();
}

實務應用

GroupDocs.Editor 在實務情境中表現優異:

  1. 自動化文件編輯 – 迭代合約資料夾,替換佔位符,並將每個檔案匯出為 RTF。
  2. 內容管理系統 – 讓使用者直接在 Web 介面編輯 Word 檔案,然後將結果儲存為 HTML 或 PDF。
  3. 批次文件轉換 – 結合載入、HTML 抽取與儲存步驟,於數分鐘內將數百個 DOCX 檔案轉換為 HTML 或 RTF。

效能考量

處理大型檔案或大量批次時,請留意以下建議:

  • 及時釋放物件EditableDocumentEditor 實作 IDisposable
  • 串流大型檔案 – 使用 FileStream 而非一次載入整個檔案至記憶體。
  • 重複使用儲存選項 – 每批次僅建立一次 WordProcessingSaveOptions 可減少開銷。

常見問題與解決方案

問題原因解決方案
OutOfMemoryException將巨大的 DOCX 載入記憶體。改用基於串流的載入 (new Editor(Stream)),並在每個檔案處理後釋放。
Missing images after conversion抽取 HTML 時未複製資源。使用 beforeEdit.GetResources(),在建立 EditableDocument.FromMarkup 時重新嵌入。
License not applied試用授權已過期。更新授權檔案路徑或透過 License.SetLicense 嵌入授權字串。

結論

您現在已了解如何使用 GroupDocs.Editor for .NET 以程式方式 編輯 docx 檔案、將 Word 轉換為 HTML,以及 將文件儲存為 RTF。這些功能讓您能建立自動化流程、將編輯功能整合至 Web 應用,並自信地執行批次轉換。

準備好進一步了嗎?探索進階主題,如 批次文件轉換、協同編輯,或將編輯後的內容儲存至雲端儲存服務。


Last Updated: 2026-03-25
Tested With: GroupDocs.Editor 23.12 for .NET
Author: GroupDocs


常見問答

Q: GroupDocs.Editor 是否相容所有 .NET 版本?
A: 是的,它支援 .NET Framework、.NET Core 以及 .NET 5/6 以上。

Q: 我可以編輯除 DOCX 之外的其他格式嗎?
A: 當然可以。此函式庫支援 PDF、RTF、HTML 以及許多其他辦公格式。

Q: 在批次處理時該如何處理錯誤?
A: 為每個檔案操作加上 try‑catch 區塊,記錄例外,然後繼續處理下一個檔案。

Q: 此函式庫是否支援在 CI/CD 流程中 自動化文件編輯
A: 支援,您可以在建置代理或 Docker 容器中執行相同程式碼,無需安裝 Office。

Q: 大型文件對效能有何影響?
A: 效能取決於文件大小與可用記憶體。使用串流與適當釋放可降低記憶體使用量。