轉換 Word 為 PDF 並儲存已編輯的文件

介紹

如果您需要 convert Word to PDF 同時將已編輯的文件儲存為多種輸出格式,您來對地方了。本指南將逐步說明從載入 WordProcessing 檔案、以程式方式編輯內容,到使用 GroupDocs.Editor for .NET 匯出為 PDF、DOCX、HTML 等格式的整個流程。完成後,您將擁有一套可在任何 .NET 4.6.1+ 或更高版本專案中重複使用的範例。

快速解答

  • Can GroupDocs.Editor convert DOCX to PDF? 是 — 只需載入文件並使用 Save 指定目標格式。
  • Do I need Microsoft Word installed? 不需要,API 於伺服器端執行轉換,無需 Office。
  • Which .NET versions are supported? .NET Framework 4.6.1+、.NET Core 3.1+、.NET 5/6+。
  • How many formats can I export to? 超過 20 種 WordProcessing 格式,包括 PDF、DOCX、HTML 與 TXT。
  • Is a license required for production? 是,需要商業授權;亦提供免費試用版。

什麼是「convert word to pdf」?

Convert word to pdf 指將 Microsoft Word (.docx) 檔案轉換為 PDF 文件,同時保留版面配置、字型與圖片。
GroupDocs.Editor 完全在記憶體中執行此轉換,免除外部工具的需求。

為什麼在此任務中使用 GroupDocs.Editor?

GroupDocs.Editor 支援 50+ 種輸入與輸出格式,且可在不將整個檔案載入記憶體的情況下處理最多 500 頁 的文件,與傳統基於 Office 的轉換相比,可降低 最高 40 % 的 CPU 使用率

前置條件

開始之前,請確保您已具備以下項目:

  1. GroupDocs.Editor for .NET – 從 here 下載最新版本。
  2. Development Environment – Visual Studio 或任何相容 .NET 的 IDE。
  3. .NET Framework – 版本 4.6.1 或更高(或 .NET Core 3.1+)。
  4. Sample Document – 一個 WordProcessing 檔案(例如 sample.docx)。
  5. Basic C# Knowledge – 熟悉 C# 語法與專案設定。

匯入命名空間

Editor 及相關類別位於 GroupDocs.Editor 命名空間。請在 C# 檔案的頂部匯入它們:

Editor 類別是載入、編輯與儲存文件的入口點。
EditableDocument 類別代表可在記憶體中編輯的文件。

using System;
using GroupDocs.Editor.Metadata;

讓我們將整個流程拆解為可管理的步驟。請仔細跟隨,以確保您了解每個環節。

步驟 1:取得輸入檔案的路徑

首先,您需要指定輸入 WordProcessing 檔案的路徑。此檔案將被載入並進行編輯。

string inputFilePath = "Your Sample Document";

步驟 2:為文件建立載入選項

接著,為 WordProcessing 文件建立專屬的載入選項。這些選項可自訂文件的載入方式。
WordProcessingLoadOptions 定義載入 WordProcessing 檔案時使用的參數,例如字型處理與記憶體限制。

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();

步驟 3:使用選項載入文件

現在,使用先前指定的載入選項將文件載入至 Editor 實例中。

using (Editor editor = new Editor(inputFilePath, delegate { return loadOptions; }))

步驟 4:建立編輯選項

為文件準備編輯選項。這些選項將決定文件的開啟編輯方式。
WordProcessingEditOptions 指定 WordProcessing 檔案的編輯行為,包括啟用變更追蹤與保留原始格式。

WordProcessingEditOptions editOptions = new WordProcessingEditOptions();

步驟 5:開啟文件以進行編輯

透過建立 EditableDocument 實例來開啟文件以進行編輯。此實例允許您對文件進行變更。

using (EditableDocument beforeEdit = editor.Edit(editOptions))

步驟 6:編輯文件內容

現在是編輯文件內容的時候。首先,將文件取得為單一的 base64 編碼字串。
GetEmbeddedHtml 會將目前的文件內容提取為 base64 編碼的 HTML 字串,方便後續操作。

string allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml();

以下範例示範如何修改文件中的副標題。

string allEmbeddedInsideStringEdited = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");

步驟 7:從已編輯內容建立新的 EditableDocument

從已編輯的內容與資源建立新的 EditableDocument 實例。

using (EditableDocument afterEdit = EditableDocument.FromMarkup(allEmbeddedInsideStringEdited, null))

步驟 8:將文件儲存為多種格式

最後,遍歷所有支援的 WordProcessing 格式,將已編輯的文件分別儲存為各種格式。
Save 方法會將已編輯的文件寫入指定格式的檔案,並在內部處理轉換。

foreach (WordProcessingFormats oneFormat in WordProcessingFormats.All)
{
    // Prepare save options
    WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(oneFormat);
    // Prepare save path
    string savePath = Path.Combine("OutputDirectory", "MultipleOutFormats." + saveOptions.OutputFormat.Extension);
    // Save the document
    editor.Save(afterEdit, savePath, saveOptions);
}

完成訊息

最後,您可以列印訊息以表示流程已成功完成。

Console.WriteLine("SavingEditedDocumentToAllFormats routine has successfully finished");

如何使用 GroupDocs.Editor 轉換 Word 為 PDF?

使用 Editor.Load(或 new Editor(inputPath, loadOptions))載入來源 DOCX,然後呼叫 editableDocument.Save("output.pdf", SaveFormat.Pdf)Editor.Load 會以指定的檔案與可選的載入選項初始化 Editor 實例,為編輯做準備。API 會自動處理字型、表格與圖片,提供忠實的 PDF 轉換結果,且不需 Microsoft Word。請確保輸出路徑可寫入,並處理可能的例外,以保證轉換成功。

常見使用情境

  • Automated report generation – 建立 DOCX 範本,程式化填入資料,最後匯出為 PDF 供發佈。
  • Batch conversion pipelines – 逐一處理資料夾中的 Word 檔案,編輯中繼資料,並將每個檔案轉換為 PDF 或 HTML。
  • Document archiving – 將已編輯的版本儲存為中性、唯讀的 PDF 格式,以符合合規需求。

疑難排解與技巧

  • Large files – 設定 LoadOptions.MemoryLimit 以避免過高的記憶體使用。
  • Missing fonts – 在轉換前於 DOCX 中嵌入所需字型,或透過 EditorSettings.FontsPath 提供自訂字型資料夾。
  • Encoding issues – 確認 base64 字串正確解碼;在 C# 中使用 Convert.FromBase64String

常見問題

Q: GroupDocs.Editor for .NET 是什麼?
A: GroupDocs.Editor for .NET 是一個功能強大的 API,讓您能直接在 .NET 應用程式中編輯、轉換與儲存多種格式的文件。

Q: 我可以免費使用 GroupDocs.Editor 嗎?
A: 是的,您可以使用免費試用版。從 here 下載。

Q: GroupDocs.Editor 支援哪些格式?
A: 此函式庫支援超過 20 種 WordProcessing 格式,包括 DOCX、PDF、HTML、TXT 與 ODT。

Q: 如何取得 GroupDocs.Editor 的臨時授權?
A: 您可於 here 取得臨時授權。

Q: 我可以在哪裡找到更多文件與支援?
A: 詳細文件可於 here 取得,社群支援則可在 here 獲得。


最後更新: 2026-06-01
測試版本: GroupDocs.Editor 2.10 for .NET
作者: GroupDocs

相關教學