c# pdf 頁數 – 使用 GroupDocs.Annotation 提取文件資訊

介紹

你是否曾經盯著一堆文件,想知道裡面到底有什麼內容卻不想逐一打開?你絕對不是唯一有這種困擾的人。無論你是在構建文件管理系統、處理法律檔案,或只是想整理公司數位資產,了解如何 在 C# 中提取文件資訊——包括 c# pdf 頁數——都可能是改變遊戲規則的關鍵。

手動檢查檔案屬性既耗時又容易出錯。使用 GroupDocs.Annotation for .NET,你可以自動化整個流程,僅用幾行程式碼即可取得檔案類型、頁數、文件大小等資訊。本教學將說明此功能的重要性、如何設定函式庫,以及即可使用的逐步程式碼範例。

快速回答

  • GroupDocs.Annotation 能提取什麼? 文件類型、頁數、大小以及基本的中繼資料。
  • 支援多少種格式? 超過 50 種輸入與輸出格式,包括 PDF、DOCX、XLSX、PPTX 以及常見的影像類型。
  • 是否能在不載入整個檔案的情況下取得 c# pdf 頁數? 可以——GetDocumentInfo() 只讀取頁數所需的標頭資訊。
  • 開發時需要授權嗎? 免費試用可用於測試;正式環境需購買完整授權。
  • API 在 Web 應用中是否為執行緒安全? 絕對安全——只要正確釋放 Annotator 實例即可。

什麼是 c# pdf 頁數?

c# pdf 頁數 是指透過 API 查詢 PDF 檔案時回傳的總頁數。使用 GroupDocs.Annotation,你可以透過 GetDocumentInfo() 方法即時取得此數值,無需渲染整份文件。此頁數來源於 PDF 的內部結構,讓你在不開啟檔案的情況下就能決定後續的處理、儲存或顯示方式,特別適用於批次驗證、合規檢查以及 UI 預覽等需要考慮頁數限制的情境。

為何使用 GroupDocs.Annotation 提取文件資訊?

GroupDocs.Annotation 支援 50+ 種文件格式,且能在不將整個檔案載入記憶體的情況下處理上百頁的檔案,於一般伺服器上可在 200 ms 內返回中繼資料。這樣的速度與格式廣度,使其成為大規模自動化、合規檢查與智慧內容分類的理想選擇。

前置條件與設定

需要的環境

  • Visual Studio 2019 或更新版本(Community 版亦可)
  • .NET Framework 4.6.1+ 或 .NET Core 2.0+
  • 基本的 C# 知識與 NuGet 使用經驗

安裝 GroupDocs.Annotation

將函式庫加入專案非常簡單,請選擇你偏好的方式:

選項 1:套件管理員主控台(建議)

Install-Package GroupDocs.Annotation -Version 25.4.0

選項 2:.NET CLI

dotnet add package GroupDocs.Annotation --version 25.4.0

選項 3:套件管理員 UI
如果你比較喜歡點擊而非輸入,只要在 NuGet 套件管理員 UI 中搜尋「GroupDocs.Annotation」並安裝最新版本即可。

取得授權

  1. 從免費試用開始:從 GroupDocs 網站 下載——無任何限制。
  2. 需要更多時間? 取得 臨時授權 以延長評估。
  3. 準備上線? 在部署前 購買完整授權。

小技巧: 免費試用會加上浮水印,但對於學習與測試程式碼已足夠。

如需查看最新變更,請參閱 release notes。

如何使用 GroupDocs.Annotation 取得 c# pdf 頁數?

Annotator 是負責載入文件並提供註解與中繼資料功能的主要類別。
使用 new Annotator("file.pdf") 載入 PDF,然後呼叫 GetDocumentInfo()——PageCount 屬性即可在兩行程式碼內回傳精確的頁數。GetDocumentInfo() 會回傳一個 DocumentInfo 物件,內含頁數、檔案類型、大小等中繼資料。此方法僅讀取必要的標頭資料,即使是大型 PDF 也能高效處理。

基本設定與文件載入

using System;
using GroupDocs.Annotation;

class Program
{
    static void Main(string[] args)
    {
        // Initialize the annotator with a document path
        using (Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/input.pdf"))
        {
            Console.WriteLine("Document loaded successfully!");
            // We'll add the extraction code here next
        }
    }
}

提取文件中繼資料

DocumentInfo 封裝了檔案的提取屬性,讓你在應用程式中輕鬆讀取與使用。

using (Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/input.pdf"))
{
    // Extract document info
    IDocumentInfo info = annotator.Document.GetDocumentInfo();
    
    // Always check if info was retrieved successfully
    if (info == null || info.PageCount == 0)
    {
        throw new Exception("Unexpected document information!");
    }

    // Display the extracted information
    Console.WriteLine($"\
File type: {info.FileType}\
Number of pages: {info.PageCount}\
Document size: {info.Size} bytes.");
    
    // Convert size to more readable format
    string readableSize = FormatFileSize(info.Size);
    Console.WriteLine($"Document size (formatted): {readableSize}");
}

// Helper method to format file size
static string FormatFileSize(long bytes)
{
    string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
    int counter = 0;
    decimal number = bytes;
    
    while (Math.Round(number / 1024) >= 1)
    {
        number /= 1024;
        counter++;
    }
    
    return string.Format("{0:n1}{1}", number, suffixes[counter]);
}

加強資訊顯示

如果需要更多上下文——例如文件是否超過特定大小門檻——可以在基本範例上加入額外檢查與業務邏輯。

using (Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/input.pdf"))
{
    IDocumentInfo info = annotator.Document.GetDocumentInfo();
    
    if (info == null)
    {
        Console.WriteLine("Could not retrieve document information.");
        return;
    }

    // Comprehensive info display
    Console.WriteLine("=== Document Information ===");
    Console.WriteLine($"File Type: {info.FileType}");
    Console.WriteLine($"Page Count: {info.PageCount}");
    Console.WriteLine($"Size: {FormatFileSize(info.Size)}");
    
    // Additional checks you might want to perform
    if (info.PageCount > 100)
    {
        Console.WriteLine("⚠️  Large document detected - consider batch processing");
    }
    
    if (info.Size > 10 * 1024 * 1024) // 10MB
    {
        Console.WriteLine("⚠️  Large file size - may impact processing time");
    }
    
    Console.WriteLine("=== Analysis Complete ===");
}

常見問題與解決方案

問題 1:「找不到檔案」錯誤

直接回答: 確認檔案路徑為絕對路徑或正確根植於應用程式基礎目錄,並確保執行緒具有讀取權限。

string documentPath = @"C:\Documents\test.pdf";

// Always check if file exists first
if (!File.Exists(documentPath))
{
    Console.WriteLine($"File not found: {documentPath}");
    return;
}

try
{
    using (Annotator annotator = new Annotator(documentPath))
    {
        // Your code here
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error loading document: {ex.Message}");
}

問題 2:不支援的檔案格式

直接回答: 確認檔案副檔名屬於 50+ 支援的格式之一;若不支援,請先將其轉換為支援的類型再呼叫 GetDocumentInfo()。

try
{
    using (Annotator annotator = new Annotator(documentPath))
    {
        IDocumentInfo info = annotator.Document.GetDocumentInfo();
        
        if (info == null)
        {
            Console.WriteLine("Unsupported file format or corrupted document");
            return;
        }
        
        // Process the info
    }
}
catch (UnsupportedFileTypeException ex)
{
    Console.WriteLine($"File type not supported: {ex.Message}");
}

問題 3:大型文件的記憶體問題

直接回答: 在載入前實作大小檢查,並使用 using 陳述式確保 Annotator 實例被正確釋放,以防止記憶體洩漏。

const long MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB limit

FileInfo fileInfo = new FileInfo(documentPath);
if (fileInfo.Length > MAX_FILE_SIZE)
{
    Console.WriteLine("File too large for processing");
    return;
}

// Use proper disposal patterns
using (Annotator annotator = new Annotator(documentPath))
{
    // Process quickly and dispose
    IDocumentInfo info = annotator.Document.GetDocumentInfo();
    // Handle info immediately
}
// Annotator is automatically disposed here

真實案例應用

文件管理系統整合

使用者上傳檔案時,先提取中繼資料以決定儲存位置、索引策略或所需的審核流程。

public class DocumentProcessor
{
    public DocumentMetadata ProcessUpload(string filePath)
    {
        using (Annotator annotator = new Annotator(filePath))
        {
            IDocumentInfo info = annotator.Document.GetDocumentInfo();
            
            return new DocumentMetadata
            {
                FileName = Path.GetFileName(filePath),
                FileType = info.FileType.ToString(),
                PageCount = info.PageCount,
                FileSizeBytes = info.Size,
                ProcessedDate = DateTime.UtcNow,
                Category = DetermineCategory(info)
            };
        }
    }
    
    private string DetermineCategory(IDocumentInfo info)
    {
        // Business logic for automatic categorization
        if (info.FileType.ToString().Contains("Pdf"))
        {
            return info.PageCount > 20 ? "Legal Document" : "Standard Document";
        }
        
        return "Other";
    }
}

批次文件分析

在背景工作中處理資料夾內的所有檔案,記錄每份文件的頁數與類型以供報表使用。

public void AnalyzeDocumentFolder(string folderPath)
{
    string[] supportedExtensions = { ".pdf", ".docx", ".xlsx", ".pptx" };
    
    foreach (string file in Directory.GetFiles(folderPath))
    {
        if (!supportedExtensions.Contains(Path.GetExtension(file).ToLower()))
            continue;
            
        try
        {
            using (Annotator annotator = new Annotator(file))
            {
                IDocumentInfo info = annotator.Document.GetDocumentInfo();
                Console.WriteLine($"{Path.GetFileName(file)}: {info.FileType}, {info.PageCount} pages");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to process {file}: {ex.Message}");
        }
    }
}

合規與驗證

受規範限制的產業常要求文件必須在特定頁數或大小以下。利用提取的中繼資料自動拒絕不合規的上傳。

public bool ValidateDocument(string filePath, DocumentRequirements requirements)
{
    using (Annotator annotator = new Annotator(filePath))
    {
        IDocumentInfo info = annotator.Document.GetDocumentInfo();
        
        // Check file type
        if (requirements.AllowedTypes != null && 
            !requirements.AllowedTypes.Contains(info.FileType.ToString()))
        {
            return false;
        }
        
        // Check page count limits
        if (info.PageCount < requirements.MinPages || 
            info.PageCount > requirements.MaxPages)
        {
            return false;
        }
        
        // Check file size
        if (info.Size > requirements.MaxSizeBytes)
        {
            return false;
        }
        
        return true;
    }
}

效能最佳化技巧

1. 實作快取

對頻繁存取的檔案快取 DocumentInfo 結果,以避免重複 I/O 操作。

private static readonly Dictionary<string, IDocumentInfo> _infoCache = 
    new Dictionary<string, IDocumentInfo>();

public IDocumentInfo GetDocumentInfoCached(string filePath)
{
    string fileHash = GetFileHash(filePath);
    
    if (_infoCache.ContainsKey(fileHash))
    {
        return _infoCache[fileHash];
    }
    
    using (Annotator annotator = new Annotator(filePath))
    {
        IDocumentInfo info = annotator.Document.GetDocumentInfo();
        _infoCache[fileHash] = info;
        return info;
    }
}

2. 使用非同步處理多文件

利用 Task.Run 或非同步串流同時處理多個檔案,避免阻塞主執行緒。

public async Task<List<DocumentMetadata>> ProcessDocumentsAsync(string[] filePaths)
{
    var tasks = filePaths.Select(async path =>
    {
        return await Task.Run(() =>
        {
            using (Annotator annotator = new Annotator(path))
            {
                IDocumentInfo info = annotator.Document.GetDocumentInfo();
                return new DocumentMetadata(path, info);
            }
        });
    });
    
    return (await Task.WhenAll(tasks)).ToList();
}

3. 記憶體管理最佳實踐

  • 始終在 using 區塊中使用 Annotator。
  • 分批處理文件,而非一次全部處理。
  • 對於超過 100 MB 的檔案,建議先將檔案串流至磁碟,再讀取中繼資料。

疑難排解指南

授權相關問題

License 物件負責將你的產品授權檔案註冊至 GroupDocs 函式庫。請確保授權檔案放置於應用程式根目錄,且在任何其他 GroupDocs 呼叫之前先實例化 License 物件。

// Set license before using any GroupDocs functionality
License license = new License();
license.SetLicense("path/to/your/license.lic");

// Or use stream for embedded licenses
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourApp.license.lic"))
{
    license.SetLicense(stream);
}

效能問題

直接回答: 針對應用程式進行效能分析,將即時處理的檔案大小限制在 100 MB 以內,並為重複讀取啟用快取。

平台特定問題

直接回答: 使用 Path.Combine 進行跨平台路徑組合;Windows 使用反斜線,Linux 使用正斜線。

// Use Path.Combine for cross-platform compatibility
string documentPath = Path.Combine(baseDirectory, "documents", fileName);

處理受密碼保護的文件

直接回答: 在建立 Annotator 實例時傳入密碼,或在呼叫 GetDocumentInfo() 前於 LoadOptions 中設定密碼。

LoadOptions loadOptions = new LoadOptions() { Password = "your-password" };
using (Annotator annotator = new Annotator("protected-document.pdf", loadOptions))
{
    // Extract information normally
}

更新 GroupDocs.Annotation

直接回答: 執行 dotnet add package GroupDocs.Annotation --version <latest> 或使用 NuGet 套件管理員 UI 取得最新版本。

Update-Package GroupDocs.Annotation

常見問答

問:GroupDocs.Annotation 支援哪些文件格式的資訊提取?
答:GroupDocs.Annotation 支援超過 50 種文件格式,包括 PDF、DOCX、XLSX、PPTX、JPEG、PNG、TIFF、CAD 檔等。

問:我能提取文件的自訂中繼資料或屬性嗎?
答:GetDocumentInfo() 提供核心中繼資料,如檔案類型、頁數與大小。若需作者、建立日期等自訂屬性,可結合 GroupDocs.Annotation 與 GroupDocs.Metadata,或使用原生檔案格式的屬性 API。

問:如何處理受密碼保護的文件?
答:在建立 Annotator 實例時提供密碼,例如 new Annotator("secure.pdf", new LoadOptions { Password = "pwd" })。

問:有沒有辦法在不將整個檔案載入記憶體的情況下提取文件資訊?
答:可以——GetDocumentInfo() 只讀取檔案標頭,因而即使是上百頁的 PDF 也能保持低記憶體使用量。

問:我可以在 Web 應用或多執行緒環境中使用嗎?
答:絕對可以。GroupDocs.Annotation 為執行緒安全,只要為每個請求建立新的 Annotator 並及時釋放即可。

結論與後續步驟

你現在已掌握使用 GroupDocs.Annotation 提取 c# pdf 頁數、檔案類型與大小的完整、生產環境就緒方案。你已學會如何設定函式庫、取得中繼資料、處理常見問題,並對大型情境進行效能最佳化。

後續行動:

  1. 下載範例專案,使用真實檔案執行提供的佔位程式碼。
  2. 探索 GroupDocs.Annotation 的其他功能,如註解渲染與文件比較。
  3. 將中繼資料提取整合至現有的上傳管線,以自動化分類與驗證。

記住,可靠的文件處理始於正確的中繼資料。使用 GroupDocs.Annotation,你可以打造更智慧、更快速且更具擴充性的解決方案。


最後更新: 2026-06-01
測試環境: GroupDocs.Annotation 25.4.0 (latest)
作者: GroupDocs

重要連結:

相關教學