GroupDocs.Annotation .NET 教程:提取 PDF 頁面

介紹

有沒有遇過需要從龐大的 PDF 文件中 提取 PDF 頁面 的情況?無論是處理法律合約、學術論文,或是技術手冊,手動分割 PDF 都可能浪費數小時。在本指南中,我們將向您展示如何使用 GroupDocs.Annotation for .NET 精確提取指定頁面、為何此函式庫是企業工作負載的可靠選擇,以及如何保持程式碼的高速與可維護性。

  • 您將完成的目標: 安裝並授權 GroupDocs.Annotation、提取任意頁面範圍、乾淨地管理檔案路徑、排除常見問題,並為大型檔案優化效能。
  • 適用對象: 熟悉 C# 的開發者,需要一個可靠且支援註解的 PDF 頁面提取解決方案。

快速答覆

  • 我可以只提取少數頁面嗎? 可以,只需在 SaveOptions 中設定 FirstPage 與 LastPage。
  • 它會保留註解嗎? 當然會;所有註解、表單欄位與中繼資料都會隨提取的頁面一起保留。
  • 它能處理多大的檔案? 可處理數百頁的 PDF(500 頁以上),且不需將整個檔案載入記憶體。
  • 我需要授權嗎? 試用版可用於評估;正式環境需購買永久授權。
  • 它相容 .NET‑Core 嗎? 完全支援 .NET 5、.NET 6 與 .NET Core 3.1。

什麼是「提取 PDF 頁面」?

提取 PDF 頁面 指的是建立一個僅包含原始文件中選定頁面的新 PDF,同時保留所有原始內容、註解與版面配置。GroupDocs.Annotation 於記憶體中完成此操作,無需渲染整個來源檔案。

為何選擇 GroupDocs.Annotation 進行頁面提取?

GroupDocs.Annotation 支援 超過 50 種輸入與輸出格式,包括 PDF、DOCX、PPTX、XLSX 與 TIFF,且在標準伺服器上可於 5 秒內處理 500 頁的 PDF。與許多免費函式庫不同,它會自動保留註解、評論與表單欄位,因而非常適合對文件完整性有嚴格要求的受規範產業。

前置條件(千萬別跳過!)

  • Visual Studio 2022(或任何近期的 .NET IDE)
  • .NET 6 SDK(或 .NET 5 / Framework 4.8)
  • 基本的 C# 知識 – 您將使用類別、using 陳述式與檔案路徑
  • 用於測試的多頁 PDF(任何至少 5 頁的 PDF 都可)

可選但有幫助: 熟悉 Path.Combine 以進行跨平台路徑處理。

設定 GroupDocs.Annotation for .NET

安裝此函式庫非常簡單。請選擇最符合您工作流程的方法。

安裝選項

方法 1:NuGet 套件管理員主控台(我偏好的方法)

Install-Package GroupDocs.Annotation -Version 25.4.0

方法 2:.NET CLI(適合命令列愛好者)

dotnet add package GroupDocs.Annotation --version 25.4.0

專業提示: 永遠固定版本(例如 -Version 23.12.0),以避免自動還原時出現破壞性變更。

授權設定(此部分很重要!)

GroupDocs.Annotation 需要有效的授權檔案。若未提供,試用版將在 30 天後受限。

如何初始化授權:

using GroupDocs.Annotation;

// This is your starting point for everything
Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/sample.pdf");

如何使用 GroupDocs.Annotation 提取 PDF 頁面?

要提取頁面,首先建立指向來源 PDF 的 Annotator 實例,接著建立 PdfSaveOptions 物件,於其中設定 FirstPage 與 LastPage 為目標範圍。最後,使用輸出路徑與選項物件呼叫 Save 方法;函式庫將產生僅包含這些頁面的新 PDF,且保留註解。

// Direct answer – the core extraction logic
var annotator = new Annotator("input.pdf");
var options = new PdfSaveOptions { FirstPage = 2, LastPage = 4 };
annotator.Save("output.pdf", options);

Annotator 類別讀取文件,PdfSaveOptions 指定要保留的頁面,Save 則寫入僅包含這些頁面的新 PDF,並保留所有註解與表單欄位。

了解 Annotator 類別

Annotator 類別是 GroupDocs.Annotation 中所有文件操作任務的入口。它將檔案載入記憶體,提供註解方法,並提供匯出時的儲存選項。

string inputPath = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "sample.pdf");
using (Annotator annotator = new Annotator(inputPath))
{
    // All the magic happens inside this using block
    // The 'using' statement ensures proper cleanup when we're done
}

為何使用 using? Annotator 實作了 IDisposable;將其包在 using 區塊中可確保檔案句柄及時釋放,這在處理大量大型 PDF 時至關重要。

設定 SaveOptions 以提取頁面範圍

PdfSaveOptions 讓您精確指定要保留的頁面。設定 FirstPage 與 LastPage(皆為 1 起始)以定義連續範圍。

var options = new PdfSaveOptions
{
    FirstPage = 10,   // start at page 10
    LastPage = 15     // end at page 15
};

常見錯誤: 使用零基索引。頁碼在 GroupDocs.Annotation 中永遠從 1 開始。

var saveOptions = new Options.SaveOptions 
{
    FirstPage = 2,  // Start from page 2
    LastPage = 4    // End at page 4
};

儲存提取的頁面

設定完成後,呼叫 Save。此方法會寫入僅包含所選頁面的新檔案。

annotator.Save(Path.Combine("YOUR_OUTPUT_DIRECTORY", "result.pdf"), saveOptions);

完整範例

將所有步驟整合,即可得到可直接執行的程式碼片段。

using GroupDocs.Annotation;
using System.IO;

string inputPath = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "sample.pdf");
using (Annotator annotator = new Annotator(inputPath))
{
    var saveOptions = new Options.SaveOptions 
    {
        FirstPage = 2,  // Extract pages 2-4
        LastPage = 4
    };
    
    annotator.Save(Path.Combine("YOUR_OUTPUT_DIRECTORY", "extracted_pages.pdf"), saveOptions);
}

智慧路徑管理(專業開發者技巧)

硬編碼檔案路徑會導致程式碼脆弱。將路徑集中於靜態輔助類別,可一次變更即切換環境。

集中式路徑常數

public static class PathHelper
{
    public const string InputFolder = @"C:\Docs\Input";
    public const string OutputFolder = @"C:\Docs\Output";

    public static string GetInputPath(string fileName) =>
        Path.Combine(InputFolder, fileName);

    public static string GetOutputPath(string fileName) =>
        Path.Combine(OutputFolder, fileName);
}
namespace PathManagement
{
    public static class Constants
    {
        private const string DocumentDirectory = "YOUR_DOCUMENT_DIRECTORY";
        private const string OutputDirectory = "YOUR_OUTPUT_DIRECTORY";

        // These methods make path management a breeze
        public static string GetDocumentFilePath(string fileName)
        {
            return Path.Combine(DocumentDirectory, fileName);
        }

        public static string GetOutputFilePath(string fileName)
        {
            return Path.Combine(OutputDirectory, fileName);
        }
    }
}

在提取邏輯中使用輔助類別

var source = PathHelper.GetInputPath("contract.pdf");
var target = PathHelper.GetOutputPath("contract_pages_10_15.pdf");
using var annotator = new Annotator(source);
var options = new PdfSaveOptions { FirstPage = 10, LastPage = 15 };
annotator.Save(target, options);
string inputPath = Constants.GetDocumentFilePath("sample.pdf");
string outputPath = Constants.GetOutputFilePath("extracted_pages.pdf");

using (Annotator annotator = new Annotator(inputPath))
{
    var saveOptions = new Options.SaveOptions 
    {
        FirstPage = 2,
        LastPage = 4
    };
    
    annotator.Save(outputPath, saveOptions);
}

好處:

  • 在開發、測試與正式環境只需一次更新。
  • 降低拼寫錯誤與路徑相關例外的風險。
  • 程式碼更乾淨、可讀性更高。

真實案例應用(實際使用情境)

法律產業

  • 合約管理: 自動提取簽名頁(例如第 48‑50 頁)以供存檔。
  • 文件發現: 從數千份 PDF 中僅抽取相關章節,節省大量人工時間。

教育

  • 章節提取: 教師可透過提取特定章節來製作自訂學習資料。
  • 研究: 學生從多篇論文中抽取方法論章節以進行文獻回顧。

金融

  • 執行摘要: 分析師提取季報的前 5 頁,以快速向利害關係人簡報。
  • 合規性: 隔離需要監管審查的政策章節。

醫療與研究

  • 醫療紀錄: 從大型病患檔案中抽取實驗室結果或影像報告,同時保留醫師備註。
  • 臨床試驗: 提取同意書與資料表以供分析,且不會洩露無關內容。

進階技巧與竅門

高效處理多份文件

當有一批 PDF 時,盡可能重複使用單一 Annotator 實例,或使用 Parallel.ForEach 進行平行處理。

string[] documentFiles = {"doc1.pdf", "doc2.pdf", "doc3.pdf"};

foreach (string docFile in documentFiles)
{
    string inputPath = Constants.GetDocumentFilePath(docFile);
    using (Annotator annotator = new Annotator(inputPath))
    {
        var saveOptions = new Options.SaveOptions 
        {
            FirstPage = 1,
            LastPage = 3  // Extract first 3 pages from each
        };
        
        string outputName = $"extracted_{docFile}";
        annotator.Save(Constants.GetOutputFilePath(outputName), saveOptions);
    }
}

錯誤處理最佳實踐

將每個操作包在 try‑catch 區塊中,並記錄有意義的訊息。這可防止單一損壞檔案導致整批處理中斷。

try
{
    using (Annotator annotator = new Annotator(inputPath))
    {
        // Your extraction code here
    }
}
catch (Exception ex)
{
    // Log the error and handle gracefully
    Console.WriteLine($"Error processing document: {ex.Message}");
}

大型 PDF 的記憶體管理

對於超過 300 頁的 PDF,建議透過設定 PdfLoadOptions 只串流所需頁面,以 分塊 方式載入。

// Instead of extracting pages 1-100 at once, do it in smaller batches
for (int startPage = 1; startPage <= 100; startPage += 10)
{
    int endPage = Math.Min(startPage + 9, 100);
    
    var saveOptions = new Options.SaveOptions 
    {
        FirstPage = startPage,
        LastPage = endPage
    };
    
    // Process this batch
}

效能最佳化(讓它更快!)

記憶體管理最佳實踐

始終在使用 Annotator 時搭配 using 陳述式。此類別持有未受管理的資源,必須釋放。

// Good - resources are automatically cleaned up
using (Annotator annotator = new Annotator(inputPath))
{
    // Your code here
}

// Bad - resources might not get cleaned up properly
Annotator annotator = new Annotator(inputPath);
// Do stuff...
// annotator.Dispose(); // You might forget this!

大型文件的最佳化

  • 非高峰期處理: 在低流量時段排程批次作業。
  • 基於任務的平行化: 在建構 UI 友好應用時,將同步呼叫包在 Task.Run 中。
  • 監控: 使用 Stopwatch 追蹤執行時間,以找出瓶頸。
using System.Diagnostics;

Stopwatch stopwatch = Stopwatch.StartNew();

using (Annotator annotator = new Annotator(inputPath))
{
    var saveOptions = new Options.SaveOptions 
    {
        FirstPage = 1,
        LastPage = 10
    };
    
    annotator.Save(outputPath, saveOptions);
}

stopwatch.Stop();
Console.WriteLine($"Page extraction completed in {stopwatch.ElapsedMilliseconds} ms");

疑難排解常見問題

「找不到檔案」錯誤

直接答案: 確認傳遞給 Annotator 的路徑存在且執行程序可存取。使用 PathHelper 以避免拼寫錯誤。

if (!File.Exists(sourcePath))
    throw new FileNotFoundException($"Input file not found: {sourcePath}");
string inputPath = Constants.GetDocumentFilePath("sample.pdf");

if (!File.Exists(inputPath))
{
    throw new FileNotFoundException($"Input file not found: {inputPath}");
}

「無效的頁面範圍」錯誤

直接答案: 確保 FirstPage ≥ 1、LastPage ≤ 文件頁數,且 FirstPage ≤ LastPage。可透過 annotator.DocumentInfo.PagesCount 取得頁數。

int totalPages = annotator.DocumentInfo.PagesCount;
if (options.FirstPage < 1 || options.LastPage > totalPages)
    throw new ArgumentOutOfRangeException("Page range is outside the document bounds.");
// You'd need to implement GetPageCount() method or check the document properties
int totalPages = GetDocumentPageCount(inputPath);

if (saveOptions.LastPage > totalPages)
{
    throw new ArgumentException($"Last page ({saveOptions.LastPage}) exceeds document length ({totalPages})");
}

大檔案的記憶體問題

  • 以較小批次處理。
  • 若在 IIS 下執行,提升應用程式集區的記憶體上限。
  • 即時釋放每個 Annotator 實例(使用 using)。

授權相關問題

將 GroupDocs.Annotation.lic 檔案放置於可執行檔同一資料夾,或以程式方式使用 License.SetLicense("path/to/license") 設定授權路徑。

與其他系統的整合

ASP.NET Core Web API 範例

公開一個端點,接收 PDF、提取請求的頁面範圍,並回傳新檔案。

[ApiController]
[Route("api/[controller]")]
public class DocumentController : ControllerBase
{
    [HttpPost("extract-pages")]
    public IActionResult ExtractPages([FromBody] PageExtractionRequest request)
    {
        try
        {
            // Your GroupDocs extraction code here
            return Ok("Pages extracted successfully");
        }
        catch (Exception ex)
        {
            return BadRequest($"Error: {ex.Message}");
        }
    }
}

Entity Framework 整合

提取完成後,將中繼資料(原始檔名、提取範圍、輸出路徑)存入資料庫,以作稽核追蹤。

using (var context = new DocumentContext())
{
    var document = context.Documents.First(d => d.Id == documentId);
    
    // Extract pages
    using (Annotator annotator = new Annotator(document.FilePath))
    {
        // Extraction code...
    }
    
    // Update database
    document.LastProcessed = DateTime.Now;
    document.ExtractedPageCount = (saveOptions.LastPage - saveOptions.FirstPage) + 1;
    context.SaveChanges();
}

常見問題

Q:我可以在一次呼叫中提取非連續頁面(例如第 1、5、9 頁)嗎?
A:GroupDocs.Annotation 僅支援透過 FirstPage 與 LastPage 的連續範圍。若需非連續頁面,必須針對每個範圍分別執行提取。

Q:一次能提取的最大頁數是多少?
A:沒有硬性上限,但提取 500 頁以上 可能需要額外記憶體;對於極大型文件建議使用批次處理。

Q:頁面提取會保留註解與表單欄位嗎?
A:會——所有註解、評論與互動式表單欄位皆會保留在輸出 PDF 中。

Q:我可以從受密碼保護的 PDF 提取頁面嗎?
A:當然可以。建立 Annotator 時提供密碼即可(例如 new Annotator("file.pdf", "password"))。

Q:如何在提取前預覽頁面?
A:使用 annotator.DocumentInfo.PagesCount 與 annotator.GetPageImage(pageNumber) 產生縮圖,以進行驗證。

結論

您現在已擁有使用 GroupDocs.Annotation for .NET 進行 提取 PDF 頁面 的完整工具箱:

  • 安裝並授權函式庫。
  • 初始化 Annotator 並使用 FirstPage/LastPage 設定 PdfSaveOptions。
  • 以集中式輔助類別管理檔案路徑。
  • 對大型批次套用錯誤處理、記憶體管理與效能技巧。

接下來的步驟:嘗試提取不同的頁面範圍,將此邏輯整合至現有的文件工作流程服務,並探索 GroupDocs.Annotation 的註解編輯功能,以實現更豐富的文件處理。


最後更新: 2026-05-26
測試環境: GroupDocs.Annotation 23.12 for .NET
作者: GroupDocs

重要連結:

相關教學