使用 GroupDocs.Editor for .NET 程式化編輯 PDF

介紹

如果您需要在 .NET 應用程式中 程式化編輯 PDF 檔案,您已來到正確的教學。本指南將逐步說明每個步驟——從安裝 GroupDocs.Editor、載入受密碼保護的 PDF、以串流方式讀取檔案、啟用分頁功能,到儲存編輯後的文件。無論是更新單一字詞或處理大量 PDF,您都會看到此函式庫如何讓工作變得輕鬆且可靠。

快速解答

  • 可以在不開啟 UI 的情況下編輯 PDF 嗎? 可以,GroupDocs.Editor 完全在程式碼中運作。
  • 支援受密碼保護的 PDF 嗎? 當然可以——您可以在載入選項中提供密碼。
  • 大型 PDF 的限制是多少? API 可透過串流技術處理超過 500 MB 的檔案。
  • 如何啟用分頁模式? 在編輯選項中設定 EnablePagination = true
  • 正式環境需要授權嗎? 非試用部署必須使用商業授權。

什麼是程式化編輯 PDF?

程式化編輯 PDF 是指透過程式碼而非手動使用 GUI 編輯器來修改 PDF 檔案的內容。GroupDocs.Editor for .NET 提供完整的 API,讓您可以直接從 C# 替換文字、圖像與版面元素。此方式支援自動化、批次處理與整合至 Web 服務,開發人員可在不需要使用者互動的情況下套用變更。API 抽象化 PDF 結構,讓您操作高階物件,同時函式庫負責底層檔案格式的複雜性。

using System;
using GroupDocs.Editor.Formats;
using GroupDocs.Editor.HtmlCss.Resources;
using GroupDocs.Editor.Options;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

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

GroupDocs.Editor 支援 30+ 種文件格式,且可編輯高達 500 MB 的 PDF 而不需要將整個檔案載入記憶體,非常適合高吞吐量的後端服務。其 內建分頁 功能確保多頁 PDF 在編輯後仍保留正確的分頁斷點,函式庫亦提供 原生串流 以高效讀寫檔案。

前置條件

在開始之前,您需要以下幾項準備:

  1. .NET 開發環境 – Visual Studio、Rider 或任何支援 .NET 6+ 的 IDE。
  2. GroupDocs.Editor for .NET – 從 release page 下載並安裝函式庫。
  3. 基本的 C# 知識 – 了解類別、串流與例外處理將有助於開發。

匯入命名空間

在撰寫任何程式碼之前,請確保已在專案中匯入必要的命名空間:

using System;
using GroupDocs.Editor.Formats;
using GroupDocs.Editor.HtmlCss.Resources;
using GroupDocs.Editor.Options;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

如何載入受密碼保護的 PDF?

PdfLoadOptions 定義載入 PDF 檔案的選項,包括密碼與記憶體設定。要載入受保護的 PDF,建立 PdfLoadOptions 實例,將其 Password 屬性設為文件的密碼,然後將此物件傳遞給編輯器。這樣可確保在任何編輯操作之前先將檔案解密。

Options.PdfLoadOptions loadOptions = new PdfLoadOptions();
// If the document is password-protected
loadOptions.Password = "your_password";

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

首先,您需要指定 PDF 文件的路徑。此教學假設您已有一個範例 PDF 檔案。

string inputFilePath = "Your Sample Document.pdf";

如何讀取 PDF 檔案串流?

FileStream 提供讀寫磁碟檔案的串流。使用它以讀取模式開啟 PDF,讓編輯器在不鎖定檔案的情況下處理文件。例如:new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) 可確保最佳效能與安全的併發讀取。

using (FileStream fs = File.OpenRead(inputFilePath))

步驟 2:從路徑建立串流

接下來,根據先前指定的路徑建立檔案串流。此串流將用於讀取 PDF 文件。

using (FileStream fs = File.OpenRead(inputFilePath))

如何設定受密碼保護的 PDF 載入選項?

PdfLoadOptions 定義載入 PDF 檔案的選項,包括密碼與記憶體使用量。建立實例後,將 Password 屬性指派為文件的密碼。對於大型 PDF,您也可以設定 UseMemoryCache = false 以降低記憶體消耗。這些設定可讓載入器有效處理加密與大型檔案。

Options.PdfLoadOptions loadOptions = new PdfLoadOptions();
// If the document is password-protected
loadOptions.Password = "your_password";

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

要載入 PDF 文件,必須指定載入選項。如果您的 PDF 受密碼保護,請在此提供密碼。

Options.PdfLoadOptions loadOptions = new PdfLoadOptions();
// If the document is password-protected
loadOptions.Password = "your_password";

如何使用串流與選項初始化 Editor?

Editor 是負責載入文件並提供編輯功能的主要類別。透過傳入回傳檔案串流的委派以及回傳先前設定好的載入選項的委派來實例化它。這會在記憶體中建立 PDF 的表示,供後續操作使用。

using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
    var documentInfo = editor.GetDocumentInfo(null);

步驟 4:將文件載入 Editor 實例

現在,使用檔案串流與載入選項將文件載入 Editor 實例。

using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
    var documentInfo = editor.GetDocumentInfo(null);

如何在編輯 PDF 時啟用分頁?

PdfEditOptions 指定 PDF 檔案的編輯設定,例如分頁。建立此類別的實例並設定 EnablePagination = true。啟用分頁可在修改後保留原始的分頁斷點與版面配置,確保輸出 PDF 與來源檔案的視覺結構相同。

Options.PdfEditOptions editOptions = new PdfEditOptions();
editOptions.EnablePagination = true;

步驟 5:建立編輯選項

為文件設定編輯選項。本例中,我們將啟用分頁模式。

Options.PdfEditOptions editOptions = new PdfEditOptions();
editOptions.EnablePagination = true;

如何產生可編輯的中介文件?

CreateEditableDocument 會產生已載入文件的可編輯表示。對 Editor 實例呼叫此方法,並傳入先前定義的 PdfEditOptions。該方法回傳一個包含類 HTML 內容的 EditableDocument,您可以在將其儲存回 PDF 之前以程式方式修改。

using (EditableDocument beforeEdit = editor.Edit(editOptions))
{
    // Extract textual content as HTML markup
    string originalContent = beforeEdit.GetContent();
    List<IHtmlResource> allResources = beforeEdit.AllResources;

步驟 6:建立中介可編輯文件

使用編輯器實例與編輯選項建立中介可編輯文件。

using (EditableDocument beforeEdit = editor.Edit(editOptions))
{
    // Extract textual content as HTML markup
    string originalContent = beforeEdit.GetContent();
    List<IHtmlResource> allResources = beforeEdit.AllResources;

如何在可編輯內容中取代文字?

EditableDocument 以可編輯格式保存文件內容。存取其 Content 屬性,可取得文件的 HTML 表示字串。使用標準的 C# 字串操作(如 Replace)或正規表達式,在重新建構文件之前修改文字。

string editedContent = originalContent.Replace("document", "edited document");

步驟 7:修改內容

依需求修改文件內容。本範例僅示範在文件中替換一個單詞。

string editedContent = originalContent.Replace("document", "edited document");

變更後如何重建 EditableDocument?

在編輯完 HTML 字串後,透過傳入修改後的內容與相關資源(圖像、字型)回傳給編輯器,建立新的 EditableDocument。此動作會重新構建文件的內部結構,為儲存做好準備。

using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
    string originalContent3 = afterEdit.GetContent();

步驟 8:使用編輯後的內容建立新 EditableDocument

以編輯後的內容與資源建立新的 EditableDocument 實例。

using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
    string originalContent3 = afterEdit.GetContent();

如何設定 PDF 儲存選項(含加密)?

PdfSaveOptions 定義儲存 PDF 檔案的選項,包括密碼保護與壓縮。實例化後,將 Password 設為輸出檔案的加密密碼,必要時啟用 EnablePagination 以保留頁面版面,並調整 CompressionLevel 以處理大型檔案。這些設定決定編輯後的 PDF 如何寫入磁碟。

FixedLayoutFormats docmFormat = FixedLayoutFormats.Pdf;
Options.PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.Password = "output_password";
saveOptions.OptimizeMemoryUsage = true;

步驟 9:建立文件儲存選項

為 PDF 文件指定儲存選項。您也可以為輸出文件設定密碼。

FixedLayoutFormats docmFormat = FixedLayoutFormats.Pdf;
Options.PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.Password = "output_password";
saveOptions.OptimizeMemoryUsage = true;

如何將編輯後的 PDF 儲存至磁碟?

Save 使用指定的儲存選項將編輯後的文件寫入檔案。於 Editor 實例上呼叫此方法,提供更新過的 EditableDocument 與已配置好的 PdfSaveOptions。此方法會在目標位置產生最終的 PDF,並套用您設定的加密或分頁設定。

string outputFilename = Path.GetFileNameWithoutExtension(inputFilePath) + "." + docmFormat.Extension;
string outputPath = Path.Combine("OutputDirectoryPath", outputFilename);
using (FileStream outputStream = File.Create(outputPath))
{
    editor.Save(afterEdit, outputStream, saveOptions);
}

步驟 10:儲存編輯後的文件

最後,將編輯後的文件儲存至指定的輸出路徑。

string outputFilename = Path.GetFileNameWithoutExtension(inputFilePath) + "." + docmFormat.Extension;
string outputPath = Path.Combine("OutputDirectoryPath", outputFilename);
using (FileStream outputStream = File.Create(outputPath))
{
    editor.Save(afterEdit, outputStream, saveOptions);
}

常見問題與解決方案

  • 大型 PDF 造成記憶體激增 – 透過設定 LoadOptions.UseMemoryCache = false 以啟用串流。
  • 文字未被取代 – 確認大小寫完全相符;若需模糊匹配,可考慮使用正規表達式。
  • 分頁斷點錯亂 – 確認在編輯與儲存選項中皆將 EnablePagination 設為 true。

常見問與答

Q: 可以使用 GroupDocs.Editor for .NET 編輯其他文件格式嗎?
A: 可以,函式庫支援 Word、Excel、PowerPoint,以及除 PDF 外的超過 30 種其他格式。

Q: 如何取得 GroupDocs.Editor for .NET 的免費試用?
A: 您可以從 GroupDocs.Editor free trial page 下載免費試用版。

Q: 能否使用 GroupDocs.Editor for .NET 處理大型 PDF 文件?
A: 能,API 包含串流與記憶體最佳化功能,讓您處理超過 500 MB 的 PDF。

Q: 儲存時如何加密 PDF 文件?
A: 在呼叫 Save 前,於 PdfSaveOptions 上設定 Password 屬性;輸出的 PDF 將會受到密碼保護。

Q: 遇到問題時該向哪裡尋求支援?
A: 您可前往 GroupDocs.Editor support forum 取得協助。

結論

現在您已掌握使用 GroupDocs.Editor for .NET 程式化編輯 PDF 的完整端對端工作流程。從載入受密碼保護的 PDF、以串流方式讀取、啟用分頁功能,到儲存加密輸出,函式庫涵蓋所有常見情境。進一步探索 API,可批次處理文件、操作圖像,或整合至雲端儲存服務。


Last Updated: 2026-07-15
Tested With: GroupDocs.Editor 23.12 for .NET
Author: GroupDocs

相關教學