IRONSOFTWAREHOME

如何在C#中使用IronPDF新增PDF書籤和大綱

Curtis Chau
Curtis Chau
Updated: 2026年5月20日

IronPDF允許您在C#中為PDF文件新增書籤(大綱),建立類似於內容表的導航功能。 新增單層或多層書籤以增強文件的可用性,幫助使用者快速跳至關鍵部分。 此功能在Windows、Linux和macOS環境中無縫運行。

快速入門:在C#中新增PDF書籤

使用IronPDF快速開始,為您的PDF文件新增書籤。 本指南演示了如何載入現有PDF,新增導航書籤,並保存更新後的文件。 非常適合尋求在C#項目中增強PDF功能的開發者。

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2複製並運行這段程式碼片段。

    var pdf = new IronPdf.PdfDocument("example.pdf");
    pdf.Bookmarks.AddBookMarkAtEnd("Chapter 1", 1);
    pdf.SaveAs("bookmarked.pdf");
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronPDF,透過免費試用
    arrow pointer

我如何在C#中處理PDF書籤?

在Adobe Acrobat Reader中,大綱(也稱為書籤)顯示在左側邊欄,提供了一種方便的方法來跳轉到文件的關鍵部分。 書籤作為交互式內容表功能,允許讀者有效地導航複雜的文件。

使用IronPDF,您可以導入PDF文件並對現有大綱執行各種操作,如新增、重新排序、編輯屬性和刪除書籤。 這使您能夠完全控制PDF文件的組織和結構,類似於如何合併或拆分PDF進行文件管理。

提示: 所有頁碼索引從零開始。

如何新增單層書籤?

在IronPDF中新增書籤是直截了當的。 使用AddBookmarkAtEnd方法,指定書籤名稱和對應的頁碼索引。 此功能與其他PDF操作如新增頁眉和頁腳或設置自訂邊距整合得很好,能夠建立專業文件。 下面是一個例子:

using IronPdf;

// Create a new PDF or edit an existing document.
PdfDocument pdf = PdfDocument.FromFile("existing.pdf");

// Add a bookmark
pdf.Bookmarks.AddBookMarkAtEnd("NameOfBookmark", 0);

// Add a sub-bookmark
pdf.Bookmarks.AddBookMarkAtEnd("NameOfSubBookmark", 1);

pdf.SaveAs("singleLayerBookmarks.pdf");

AddBookMarkAtStart將書籤插入列表的開頭。每個書籤引用特定的頁碼索引,從而實現文件內的精確導航。

單層書籤文件

如何建立多層書籤層次結構?

IronPDF允許您以樹結構新增書籤,這在保持大型PDF文件的可導航性方面特別有用。 這一特性在處理大量考卷、銷售報告或來自各日期和地點的收據記錄的單一PDF文件時非常有價值。 就像您可能會建立PDF表單以進行資料收集一樣,結構化書籤有助於將複雜資訊分層組織。

IPdfBookMark物件,允許您新增子書籤。 例如,使用Children.AddBookMarkAtEnd("Date1", 0)來將子書籤新增到"考試"書籤。 這種巢狀結構建立了一個層次組織,反映了文件的邏輯流程。 以下程式碼演示了這一概念:

using IronPdf;

// Load existing PDF document
PdfDocument pdf = PdfDocument.FromFile("examinationPaper.pdf");

// Assign IPdfBookMark object to a variable
var mainBookmark = pdf.Bookmarks.AddBookMarkAtEnd("Examination", 0);

// Add bookmark for days
var date1Bookmark = mainBookmark.Children.AddBookMarkAtStart("Date1", 1);

// Add bookmark for type of test
var paperBookmark = date1Bookmark.Children.AddBookMarkAtStart("Paper", 1);
paperBookmark.Children.AddBookMarkAtEnd("PersonA", 3);
paperBookmark.Children.AddBookMarkAtEnd("PersonB", 4);

// Add bookmark for days
var date2Bookmark = mainBookmark.Children.AddBookMarkAtEnd("Date2", 5);

// Add bookmark for type of test
var computerBookmark = date2Bookmark.Children.AddBookMarkAtStart("Computer", 5);
computerBookmark.Children.AddBookMarkAtEnd("PersonC", 6);
computerBookmark.Children.AddBookMarkAtEnd("PersonD", 7);

pdf.SaveAs("multiLayerBookmarks.pdf");

這種層次方法在處理需要詳細組織的複雜文件時尤其有價值。 巢狀結構允許使用者展開和折疊書籤部分,即使在擁有數百頁的文件中也能實現直觀導航。

多層書籤文件

如何從HTML標題自動生成書籤?

在將HTML渲染為PDF時,IronPDF可以自動生成來自文件的標題結構(h1-h6)或任何自訂CSS選擇器的層次大綱。 這消除了手動為每個部分調用AddBookMarkAtEnd的需求。

在調用RenderingOptions上啟用該功能:

using IronPdf;

var renderer = new ChromePdfRenderer();

// Master switch: auto-generate bookmarks from HTML headings (h1-h6) during rendering
renderer.RenderingOptions.AutoBookmarksFromHeadings = true;

var html = @"
<h1>Annual Report</h1>
<h2>Executive Summary</h2>
<p>Overview of the year.</p>
<h2>Financial Results</h2>
<h3>Revenue</h3>
<h3>Expenses</h3>
<h2>Outlook</h2>
<p>Looking forward.</p>";

var pdf = renderer.RenderHtmlAsPdf(html);

// pdf.Bookmarks is already populated with a hierarchical outline matching the heading structure
pdf.SaveAs("auto-bookmarked.pdf");

以下屬性控制將哪些元素設為書籤:

  • AutoBookmarksFromHeadings (false):開啟此功能。 設置為true時,IronPDF自動從您的HTML標題生成書籤大綱。
  • AutoBookmarkMinHeadingLevel (1):開始的最高級別標題。 設置為1以包含h1(文件頂部)。
  • AutoBookmarkMaxHeadingLevel (6):包含的最深標題級別。 設置為3以僅標記h1、h2和h3——忽略h4至h6。
  • AutoBookmarkCssSelectors (null):使用自訂的CSS選擇器而不是標題標籤。 例如:"[data-bookmark]"。

自訂標題級別和CSS選擇器

僅限於頂級標題時設置書籤:

renderer.RenderingOptions.AutoBookmarksFromHeadings = true;
renderer.RenderingOptions.AutoBookmarkMaxHeadingLevel = 3;

要用自訂元素而不是標題標記書籤,提供CSS選擇器:

renderer.RenderingOptions.AutoBookmarksFromHeadings = true;
renderer.RenderingOptions.AutoBookmarkCssSelectors = new[]
{
    "h1",
    ".chapter-title",
    "[data-bookmark]"
};

如何查詢HTML元素的渲染位置?

渲染後,IronPDF可以精確報告給定HTML元素最終位於哪一頁和哪個座標。 這取代了先渲染、提取文字和手動搜尋頁面這個較老的變通方法。

在渲染前,設置GetElementLocations:

using IronPdf;
using System;

var renderer = new ChromePdfRenderer();

// Configure which elements should be queryable after rendering
renderer.RenderingOptions.ElementQuerySelectors = new[] { "h1", ".kpi-card" };

var html = @"
<h1>Q4 Dashboard</h1>
<div class='kpi-card'>Revenue: $1.2M</div>
<div class='kpi-card'>Growth: 18%</div>
<h1>Q3 Comparison</h1>
<div class='kpi-card'>Previous: $1.0M</div>";

var pdf = renderer.RenderHtmlAsPdf(html);

// Retrieve the rendered page location of each matched element
foreach (var location in pdf.GetElementLocations())
{
    Console.WriteLine($"'{location.Text}' on page {location.PageIndex + 1} " +
                      $"at ({location.Rectangle.X}, {location.Rectangle.Y})");
}

pdf.SaveAs("dashboard.pdf");

List<RenderedElementLocation>,其中每個匹配元素有一個條目:

屬性型別它告訴您的內容
Textstring元素內的文字。
PageIndexint元素的頁碼位置(從0開始)。
RectangleIronSoftware.Drawing.Rectangle元素在頁面上的位置,以PDF點(1/72英寸)為單位。 原點為左下角。
ElementIndexint元素在原始HTML中的順序(從0開始)。
提示: 首次調用結果被快取。 如果在渲染後修改文件的註解並需要新座標,調用GetElementLocations調用時強制重新掃描。)]}

結合自動書籤和元素位置追蹤

自動書籤和元素位置查詢可以在單次渲染中結合。 例如,從h1/h2標題生成大綱的同時追蹤.invoice-total元素的頁面位置:

using IronPdf;
using System;

var renderer = new ChromePdfRenderer();

// Auto-generate bookmarks from top-level headings only
renderer.RenderingOptions.AutoBookmarksFromHeadings = true;
renderer.RenderingOptions.AutoBookmarkMaxHeadingLevel = 2;

// Also track the page location of invoice totals after rendering
renderer.RenderingOptions.ElementQuerySelectors = new[] { ".invoice-total" };

var html = @"
<h1>Invoice #2026-001</h1>
<h2>Line Items</h2>
<p>Services rendered for Q1.</p>
<p class='invoice-total'>Subtotal: $4,500.00</p>
<h1>Invoice #2026-002</h1>
<h2>Line Items</h2>
<p>Consulting hours for Q2.</p>
<p class='invoice-total'>Subtotal: $7,200.00</p>";

var pdf = renderer.RenderHtmlAsPdf(html);

// Bookmarks are populated automatically; locations can be queried after rendering
foreach (var location in pdf.GetElementLocations())
{
    Console.WriteLine($"{location.Text} appears on page {location.PageIndex + 1}");
}

pdf.SaveAs("invoices.pdf");

我如何檢索和導航現有書籤?

IronPDF使您可以輕鬆檢索和查看PDF文件中的書籤。 在書籤樹中導航非常簡單,提供了不同部分的無縫存取。 此功能在處理需要編輯的現有PDF文件時,或在已標記書籤的部分中實現搜尋和替換文字等功能時至關重要。 請參考上方的多層書籤文件範例。

"考試"書籤有一個Children屬性,指向"日期1"和"日期2"書籤。 "日期1"書籤有一個NextBookmark屬性,指向"日期2"書籤。 此外,"日期1"書籤有一個Children屬性,包含"論文"書籤。 此互聯結構允許複雜的導航模式和文件組織。

要檢索打開的PDF文件中的所有書籤,使用GetAllBookmarks方法。 這將提供一個所有書籤的完整列表,允許您分析和利用書籤結構:

using IronPdf;

// Load existing PDF document
PdfDocument pdf = PdfDocument.FromFile("multiLayerBookmarks.pdf");

// Retrieve bookmarks list
var mainBookmark = pdf.Bookmarks.GetAllBookmarks();
請注意: 合併兩個具有相同書籤名稱的PDF可能會擾亂書籤列表。
警告: 僅支持從頁碼索引建立的書籤。 來自其他PDF元素的書籤將其頁碼索引值設置為-1。)]}

了解如何在下面的文章中從HTML生成PDF時建立目錄表:"使用IronPDF建立目錄表。"

準備好看看您還能做什麼嗎? 查看我們的教程頁面:組織PDFs

常見問題

如何在C#中向PDF文件新增書籤?

IronPDF使在C#中向PDF文件新增書籤變得簡單。您可以使用AddBookmarkAtEnd方法來新增單層書籤,指定書籤名稱和頁面索引。例如:pdf.Bookmarks.AddBookMarkAtEnd('Chapter 1', 1)。這樣可以建立類似於目錄的導航輔助,幫助使用者快速跳轉到關鍵部分。

AddBookmarkAtEnd和AddBookmarkAtStart方法之間有什麼區別?

IronPDF提供了兩種放置書籤的方法。AddBookMarkAtEnd方法將書籤附加到現有書籤列表的末尾,而AddBookMarkAtStart會在列表的開頭插入書籤。兩種方法都參考具體的頁面索引,以實現文件內的精確導航。

我可以建立具有多層結構的書籤層次嗎?

是的,IronPDF允許您以樹狀結構建立多層書籤層次。這對於組織包含巢狀部分的複雜文件特別有用,類似於您如何構建詳細的目錄和子章節。

書籤功能是否相容不同的操作系統?

IronPDF的書籤功能可在Windows、Linux和macOS環境中無縫運行。無論您的操作系統為何,您都可以新增、編輯與管理PDF書籤,確保在不同平台上的一致功能。

我可以對現有PDF書籤執行哪些操作?

使用IronPDF,您可以對現有PDF大綱執行各種操作,包括新增新書籤、重新排序、編輯書籤屬性以及刪除不需要的書籤。這使您可以完全控制PDF文件的組織與結構。

當使用者打開PDF時,書籤會如何顯示?

在Adobe Acrobat Reader和類似的PDF檢視器中,使用IronPDF建立的書籤會在左側側欄以大綱形式顯示。它們充當互動式目錄,允許讀者通過點擊快速導航至具體部分,從而有效地瀏覽複雜文件。

Does IronPDF support bookmark manipulation in existing PDF documents?

Yes, IronPDF allows for various operations on existing PDF outlines such as adding, reordering, editing properties, and deleting bookmarks, giving you full control over the PDF document's structure.

What environments does IronPDF support for adding bookmarks to PDFs?

IronPDF supports adding bookmarks to PDFs on Windows, Linux, and macOS environments, providing cross-platform compatibility for developers.

Can IronPDF track the rendered locations of HTML elements in a PDF?

IronPDF can report the rendered page and coordinates of specific HTML elements in a PDF using the `GetElementLocations` method, helping developers precisely locate elements within the PDF document.

What are the key steps to get started with adding bookmarks in IronPDF?

The key steps to add bookmarks in IronPDF include downloading IronPDF from NuGet, loading or rendering a new PDF document, adding single or multi-layer bookmarks, and saving the updated document.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 21,253,952版本:2026.10剛剛發布

立即獲取免費

立即獲取 30天試用金鑰。

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用密鑰。
不需要信用卡或建立賬戶
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.10

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.10

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
您的詳細資訊將始終保密。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
受到全球數百萬工程師的信任
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.10

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.10

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起