如何在 C# 中新增與編輯 PDF 註解
C# 開發人員可透過 IronPDF 的 Annotations.Add 方法與 TextAnnotation 類別,新增、編輯及移除文字留言、便條貼等 PDF 註解,以改善商務應用程式的文件協作與審閱流程。
您的企業在每年的PDF安全和合規訂閱上花費過多。考慮使用IronSecureDoc,它提供數位簽署、遮蔽、加密與保護等 SaaS 服務的管理解決方案,只需一次付款。探索IronSecureDoc文件
使用者可在文件的特定部分加入留言、提醒或其他資訊。處理共用 PDF 時,註解讓使用者能夠標記內容、留下意見並補充背景資訊,便於協作與溝通。
PDF 註解可支援多種商務流程。審閱人員可直接在文件上標記意見,團隊可在保留原始內容的情況下協作處理合約,品質保證團隊也可標記技術文件中的問題。無論是建立文件管理系統或改善既有的 PDF 工作流程,IronPDF 的註解功能都能與您的 C# PDF 建立及編輯功能整合。若需要進階安全與註解功能,請參閱完整的 PDF 安全性教學。
快速入門:使用 IronPDF 在 PDF 中新增註解本指南示範如何在 C# 中使用 IronPDF 為 PDF 文件新增文字註解。只需幾行程式碼,即可加入留言或備註,讓文件更便於互動與協作。先載入 PDF,建立 TextAnnotation 執行個體,再將它傳給 Annotations.Add 以插入註解。
-
1使用 IronPDF 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
PdfDocument.FromFile("input.pdf") .Annotations.Add(new TextAnnotation(0) { Title="Note", Contents="Review this section.", X=50, Y=700 }) .SaveAs("annotated.pdf");PdfDocument.FromFile("input.pdf") _ .Annotations.Add(New TextAnnotation(0) With {.Title = "Note", .Contents = "Review this section.", .X = 50, .Y = 700}) _ .SaveAs("annotated.pdf")PdfDocument.FromFile("input.pdf") _ .Annotations.Add(New TextAnnotation(0) With {.Title = "Note", .Contents = "Review this section.", .X = 50, .Y = 700}) _ .SaveAs("annotated.pdf") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPDF,並使用 免費試用
基本流程(5 個步驟)
- 下載用於新增 PDF 註解的 C# 程式庫
- 載入現有 PDF 文件或渲染新的 PDF 文件
- 使用
Add方法新增註解 - 擷取與編輯 PDF 註解
- 從 PDF 文件移除註解
如何在 PDF 中新增註解?
Annotations
Title
Contents
X
Y
您可以呼叫 Annotations 屬性的 Add 方法,以程式在 PDF 頁面上新增類似便條貼的註解。
PDF 中的文字註解功能類似於實體文件中的便條貼。 它們會以小圖示的形式出現在頁面上,點擊後會顯示完整的註解文字。 這種非侵入式的方法可保持文件的可讀性,同時提供必要的回饋機制。 在使用HTML到PDF轉換時,您可以在轉換後新增註解,以標記需要審閱的區域或提供其他背景。 此功能在結合使用JavaScript渲染時特別有用,以便為可能需要額外澄清的動態內容提供支援。
using IronPdf;
using IronPdf.Annotations;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>");
// Create a PDF annotation object on a specified page index
TextAnnotation annotation = new TextAnnotation(0)
{
Title = "This is the title",
Contents = "This is the long 'sticky note' comment content...",
X = 50,
Y = 700,
};
// Add the annotation
pdf.Annotations.Add(annotation);
pdf.SaveAs("annotation.pdf");using IronPdf;
using IronPdf.Annotations;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>");
// Create a PDF annotation object on a specified page index
TextAnnotation annotation = new TextAnnotation(0)
{
Title = "This is the title",
Contents = "This is the long 'sticky note' comment content...",
X = 50,
Y = 700,
};
// Add the annotation
pdf.Annotations.Add(annotation);
pdf.SaveAs("annotation.pdf");Imports IronPdf
Imports IronPdf.Annotations
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>")
' Create a PDF annotation object on a specified page index
Dim annotation As New TextAnnotation(0) With {
.Title = "This is the title",
.Contents = "This is the long 'sticky note' comment content...",
.X = 50,
.Y = 700
}
' Add the annotation
pdf.Annotations.Add(annotation)
pdf.SaveAs("annotation.pdf")Imports IronPdf
Imports IronPdf.Annotations
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>")
' Create a PDF annotation object on a specified page index
Dim annotation As New TextAnnotation(0) With {
.Title = "This is the title",
.Contents = "This is the long 'sticky note' comment content...",
.X = 50,
.Y = 700
}
' Add the annotation
pdf.Annotations.Add(annotation)
pdf.SaveAs("annotation.pdf")TextAnnotation類別提供多項可自訂的屬性:
Title:註解的標題文字,通常顯示於註解的彈出視窗Contents:註解的內文X,Y:註解圖示在頁面上的座標PageIndex:放置註解的頁面索引,從 0 開始
處理較複雜的文件流程時,可將註解與 IronPDF 的其他功能搭配使用,例如用數位簽章支援核准流程,或用浮水印管理文件版本。處理敏感文件時,也可搭配 PDF 權限與密碼,控制誰能檢視或修改註解。
文字註解可以設定哪些屬性?
TextAnnotation類別除了基本屬性,還提供其他可自訂的屬性:
Title:註解的標題文字,通常顯示於註解的彈出視窗Contents:註解的內文X,Y:註解圖示在頁面上的座標PageIndex:放置註解的頁面索引,從 0 開始Subject:用於分類註解的選填主旨Icon:註解的圖示樣式(例如便條、留言或說明)Open:註解的彈出視窗預設是否開啟
這些特性可讓開發人員建立豐富的上下文註解,加強文件溝通。 對於涉及多種文件類型的進階工作流程,考慮探索RTF到PDF轉換或Markdown到PDF轉換以在不同來源格式間保留註解。
PDF 註解的座標如何運作?
PDF 以頁面左下角為座標原點,許多 UI 框架則以左上角為原點。X 和 Y 屬性決定註解圖示在頁面上的位置,單位是點(1 點等於 1/72 英吋)。以程式定位註解時,請將原點的差異納入座標計算。
在計算註解的位置時,請記住標準的 US Letter 頁面是 8.5 x 11 英吋(612 x 792 點)。 A4 頁面尺寸為 595 x 842 點。 若要精確定位,您可能需要在放置註解之前,以程式化的方式擷取頁面尺寸。 當使用自訂紙張尺寸或實現基於內容的響應性註解放置時,這一點尤其重要。
帶註解的 PDF
上述 PDF 文件中的註解可使用 Chrome 瀏覽器檢視。
如何擷取並編輯現有註解?
擷取與編輯 PDF 註解有助於提高文件的清晰度、準確性及可用性,進而改善協作。請透過 Annotations 屬性存取註解集合,再以新資訊更新 Header、Body、X、Y 等屬性。
處理現有 PDF 時,尤其是由 URL 轉換或 DOCX 匯入產生的文件,您可能需要修改其他使用者或系統新增的註解。IronPDF 提供對整個註解集合的存取權,讓您能透過程式逐一檢查、識別並更新指定的註解。這項功能對建置文件審閱流程或整合既有文件管理系統很重要。
using IronPdf;
using IronPdf.Annotations;
using System.Linq;
PdfDocument pdf = PdfDocument.FromFile("annotation.pdf");
// Retrieve annotation collection
PdfAnnotationCollection annotationCollection = pdf.Annotations;
// Select the first annotation
TextAnnotation annotation = (TextAnnotation)annotationCollection.First();
// Edit annotation
annotation.Title = "New title";
annotation.Contents = "New content...";
annotation.X = 150;
annotation.Y = 800;
pdf.SaveAs("editedAnnotation.pdf");using IronPdf;
using IronPdf.Annotations;
using System.Linq;
PdfDocument pdf = PdfDocument.FromFile("annotation.pdf");
// Retrieve annotation collection
PdfAnnotationCollection annotationCollection = pdf.Annotations;
// Select the first annotation
TextAnnotation annotation = (TextAnnotation)annotationCollection.First();
// Edit annotation
annotation.Title = "New title";
annotation.Contents = "New content...";
annotation.X = 150;
annotation.Y = 800;
pdf.SaveAs("editedAnnotation.pdf");Imports IronPdf
Imports IronPdf.Annotations
Imports System.Linq
Private pdf As PdfDocument = PdfDocument.FromFile("annotation.pdf")
' Retrieve annotation collection
Private annotationCollection As PdfAnnotationCollection = pdf.Annotations
' Select the first annotation
Private annotation As TextAnnotation = CType(annotationCollection.First(), TextAnnotation)
' Edit annotation
annotation.Title = "New title"
annotation.Contents = "New content..."
annotation.X = 150
annotation.Y = 800
pdf.SaveAs("editedAnnotation.pdf")Imports IronPdf
Imports IronPdf.Annotations
Imports System.Linq
Private pdf As PdfDocument = PdfDocument.FromFile("annotation.pdf")
' Retrieve annotation collection
Private annotationCollection As PdfAnnotationCollection = pdf.Annotations
' Select the first annotation
Private annotation As TextAnnotation = CType(annotationCollection.First(), TextAnnotation)
' Edit annotation
annotation.Title = "New title"
annotation.Contents = "New content..."
annotation.X = 150
annotation.Y = 800
pdf.SaveAs("editedAnnotation.pdf")編輯程序會維護所有其他文件屬性與內容,確保僅修改指定的註解。 這在審閱工作流程中特別有用,在此流程中,註解可能需要根據文件修訂或需求變化進行更新。 若要完整管理文件,可搭配編輯中繼資料,以追蹤文件版本與修訂記錄。
為什麼我需要編輯現有註解?
在協同文件工作流程中,編輯註解變得非常重要,因為在此流程中,回饋需要根據修訂內容進行更新,模稜兩可的註解需要澄清,或者註解因內容變更而需要重新定位。 此能力可確保文件審閱在整個修訂週期中保持最新和相關。
在企業環境中,註解編輯通常會與核准工作流程整合,主管可能需要在最終核准前修改審核員的意見。 此外,當文件進行翻譯或本地化時,可能需要更新註解以反映語言變更或文化適應。 在這些情況下,IronPDF的UTF-8和國際語言支援確保註解在不同語言和字符集間正確顯示。
編輯註解時對其他文件屬性有何影響?
編輯程序會維護所有其他文件屬性與內容,確保僅修改指定的註解。 文件結構、格式、內嵌資源和其他註解保持不變,在保持 PDF 完整性的同時,允許針對特定的審查意見進行有針對性的更新。
這種選擇性的編輯方式對於保持文件的完整性至關重要,尤其是在必須避免文件被篡改的管制行業。 此過程會保留數位簽章、表格欄位、書籤以及所有其他 PDF 功能。 對於需要保留稽核記錄的文件,可在修改註解時一併追蹤修訂記錄。
編輯註解後的 PDF
上述 PDF 文件中的註解可使用 Chrome 瀏覽器檢視。
如何從 PDF 文件移除註解?
使用以下方法移除不必要或過時的註解:RemoveAt、RemoveAllAnnotationsForPage和Clear。
RemoveAt:使用指定索引移除單一註解。RemoveAllAnnotationsForPage:移除指定頁面的所有註解。Clear:移除文件中的所有註解。
定稿時,移除註解是重要的一環。納入回饋並完成必要修改後,您可能需要在發送最終版本前清除審閱意見。這項作業可搭配 IronPDF 的 PDF壓縮等功能,產生適合發送的乾淨版本。若文件需要封存,可在移除註解後轉為 PDF/A 格式,以符合長期保存規範。
如何移除單一註解?
若要移除單一註解,請將它在註解集合中的索引傳給 RemoveAt 方法。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");
// Remove a single annotation with specified index
pdf.Annotations.RemoveAt(1);
pdf.SaveAs("removeSingleAnnotation.pdf");using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");
// Remove a single annotation with specified index
pdf.Annotations.RemoveAt(1);
pdf.SaveAs("removeSingleAnnotation.pdf");Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")
' Remove a single annotation with specified index
pdf.Annotations.RemoveAt(1)
pdf.SaveAs("removeSingleAnnotation.pdf")Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")
' Remove a single annotation with specified index
pdf.Annotations.RemoveAt(1)
pdf.SaveAs("removeSingleAnnotation.pdf")以程式方式移除註解時,必須瞭解每次移除註解後,註解集合都會重新編排索引。 如果您需要移除多個特定的註解,請在集合中倒序進行,或先收集要移除的註解,然後再倒序移除。 此方法可避免索引移位問題,以免移除錯誤的註解。
移除單一註解前後的 PDF
前
後
上述 PDF 文件中的註解可使用 Chrome 瀏覽器檢視。
如何一次移除所有註解?
若要移除特定頁面上的所有註解,請呼叫 RemoveAllAnnotationsForPage 並指定頁面索引。若要移除整份文件中的所有註解,請呼叫 Annotations 屬性的 Clear 方法。
準備發送最終版本,或在文件版本管理系統中清除先前審閱階段留下的註解時,批次移除功能尤其有用。您也可以搭配編輯中繼資料,更新文件屬性以標明審閱狀態。若工作流程需要清除文件中的敏感內容,請參閱 PDF 清理選項,以移除包括註解在內的所有可能含有敏感內容的資訊。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");
// Remove all annotaions on a specified page
pdf.Annotations.RemoveAllAnnotationsForPage(0);
// Remove all annotaions on the document
pdf.Annotations.Clear();
pdf.SaveAs("removeAllAnnotation.pdf");using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");
// Remove all annotaions on a specified page
pdf.Annotations.RemoveAllAnnotationsForPage(0);
// Remove all annotaions on the document
pdf.Annotations.Clear();
pdf.SaveAs("removeAllAnnotation.pdf");Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")
' Remove all annotaions on a specified page
pdf.Annotations.RemoveAllAnnotationsForPage(0)
' Remove all annotaions on the document
pdf.Annotations.Clear()
pdf.SaveAs("removeAllAnnotation.pdf")Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")
' Remove all annotaions on a specified page
pdf.Annotations.RemoveAllAnnotationsForPage(0)
' Remove all annotaions on the document
pdf.Annotations.Clear()
pdf.SaveAs("removeAllAnnotation.pdf")何時應批次移除註解?
準備發送最終版本、建立不含註解的封存版本,或需要在版本管理流程中清除先前的審閱註解時,適合批次移除。這比逐一移除更省時,也能避免正式文件意外保留審閱意見。
此外,在為可能無法正確處理註解的自動處理系統準備文件,或將註解的 PDF 轉換為其他格式時,批次移除是必要的。 對於高度安全的環境,在分發前移除所有註解可防止內部審查意見或敏感回饋不慎外洩,而這些意見或回饋並非為外部受眾而設。
如何在 PDF 中新增可點擊的內部連結?
LinkAnnotation
DestinationType
Hidden
LinkAnnotation 類別會在頁面上建立可點擊的矩形區域,讓讀者前往同一份 PDF 的另一頁。這適合用於自訂目錄、交互參照、「回到頂端」按鈕及其他文件內導覽。和 TextAnnotation 一樣,您可透過 Annotations.Add 方法新增 LinkAnnotation,將它和留言及便條貼一起放在同一個集合中。
您可用 X、Y、Width 和 Height 設定位置與大小(單位為點,72 點等於 1 英吋),或直接指派 Rectangle。請在建構函式中指定連結所在頁面與目標頁面的索引,兩者都從 0 開始。
X、Y、Width和Height在設定前都會傳回 -1。未設定這些值會產生無效的註解,因此請務必指定可點擊區域。using IronPdf;
using IronPdf.Annotations;
// Load an existing multi-page PDF
PdfDocument pdf = PdfDocument.FromFile("multipage.pdf");
// Create a clickable link on page 1 (index 0) that jumps to page 6 (index 5).
// Page indexes are zero-based. The default DestinationType (Page) fits the
// whole destination page in the viewer when the link is clicked.
LinkAnnotation link = new LinkAnnotation(pageIndex: 0, destinationPageIndex: 5)
{
X = 72, // points from the LEFT edge of the page (72 points = 1 inch)
Y = 700, // BOTTOM edge of the clickable area (PDF origin is bottom-left)
Width = 200,
Height = 20,
Contents = "Go to Chapter 6"
};
// LinkAnnotation lives in the same collection as TextAnnotation
pdf.Annotations.Add(link);
pdf.SaveAs("internal-link.pdf");using IronPdf;
using IronPdf.Annotations;
// Load an existing multi-page PDF
PdfDocument pdf = PdfDocument.FromFile("multipage.pdf");
// Create a clickable link on page 1 (index 0) that jumps to page 6 (index 5).
// Page indexes are zero-based. The default DestinationType (Page) fits the
// whole destination page in the viewer when the link is clicked.
LinkAnnotation link = new LinkAnnotation(pageIndex: 0, destinationPageIndex: 5)
{
X = 72, // points from the LEFT edge of the page (72 points = 1 inch)
Y = 700, // BOTTOM edge of the clickable area (PDF origin is bottom-left)
Width = 200,
Height = 20,
Contents = "Go to Chapter 6"
};
// LinkAnnotation lives in the same collection as TextAnnotation
pdf.Annotations.Add(link);
pdf.SaveAs("internal-link.pdf");Imports IronPdf
Imports IronPdf.Annotations
' Load an existing multi-page PDF
Dim pdf As PdfDocument = PdfDocument.FromFile("multipage.pdf")
' Create a clickable link on page 1 (index 0) that jumps to page 6 (index 5).
' Page indexes are zero-based. The default DestinationType (Page) fits the
' whole destination page in the viewer when the link is clicked.
Dim link As New LinkAnnotation(pageIndex:=0, destinationPageIndex:=5) With {
.X = 72, ' points from the LEFT edge of the page (72 points = 1 inch)
.Y = 700, ' BOTTOM edge of the clickable area (PDF origin is bottom-left)
.Width = 200,
.Height = 20,
.Contents = "Go to Chapter 6"
}
' LinkAnnotation lives in the same collection as TextAnnotation
pdf.Annotations.Add(link)
pdf.SaveAs("internal-link.pdf")Imports IronPdf
Imports IronPdf.Annotations
' Load an existing multi-page PDF
Dim pdf As PdfDocument = PdfDocument.FromFile("multipage.pdf")
' Create a clickable link on page 1 (index 0) that jumps to page 6 (index 5).
' Page indexes are zero-based. The default DestinationType (Page) fits the
' whole destination page in the viewer when the link is clicked.
Dim link As New LinkAnnotation(pageIndex:=0, destinationPageIndex:=5) With {
.X = 72, ' points from the LEFT edge of the page (72 points = 1 inch)
.Y = 700, ' BOTTOM edge of the clickable area (PDF origin is bottom-left)
.Width = 200,
.Height = 20,
.Contents = "Go to Chapter 6"
}
' LinkAnnotation lives in the same collection as TextAnnotation
pdf.Annotations.Add(link)
pdf.SaveAs("internal-link.pdf")除了位置,還有幾項屬性可控制外觀及中繼資料。Color 設定連結的邊框顏色,Contents 提供描述文字或工具提示,Title 設定註解名稱。若要在現有內容上放置隱形的可點擊區域,請保留 Color 未設定,並設為 Hidden = true。本指南稍後會示範。
連結註解與文字註解有何差異?
TextAnnotation 類似便條貼,按一下留言圖示即可顯示文字。LinkAnnotation 是用於導覽的可點擊區域,可設為隱形或加上邊框,讓讀者前往目標頁面。兩者共用 Annotations 集合及 Add、RemoveAt、RemoveAllAnnotationsForPage、Clear 等操作,因此您可在同一份文件中混用並管理留言和導覽連結。
LinkAnnotation 指向同一份文件的頁面,可搭配 IronPDF 的大綱與書籤功能和自動產生的目錄功能使用。若要建立檢視器側邊欄的導覽樹狀結構,請使用書籤。若要在將 HTML 渲染為 PDF 時產生目錄,請使用目錄功能。若要在現有 PDF 上設定可點擊區域,請使用 LinkAnnotation。
如何控制目標頁面的顯示方式?
DestinationType 屬性使用現有的 BookmarkDestinations 列舉,控制讀者點擊連結後目標頁面的顯示方式。預設值 BookmarkDestinations.Page 會將整個目標頁面縮放至視窗可完整顯示。其他值可搭配 DestinationTop、DestinationLeft、DestinationRight、DestinationBottom 和 DestinationZoom,捲動到指定位置、設定縮放倍率或顯示指定的矩形區域。
BookmarkDestinations 值 | 點擊時的行為 | 使用的目標座標 |
|---|---|---|
Page(預設) | 縮放至視窗可完整顯示目標頁面。 | 無 |
PageY | 捲動到指定的垂直位置,並顯示頁面的完整寬度。 | DestinationTop |
PageX | 捲動到指定的水平位置,並顯示頁面的完整高度。 | DestinationLeft |
PageZoom | 移至指定位置並設定縮放倍率。 | DestinationLeft、DestinationTop、DestinationZoom |
PageRect | 縮放至視窗可完整顯示頁面上指定的矩形區域。 | DestinationLeft、DestinationBottom、DestinationRight、DestinationTop |
PageBounds | 依照頁面的邊界框縮放,使其完整顯示。 | 無 |
PageBoundsY | 與 PageY 相同,以頁面邊界框為準。 | DestinationTop |
PageBoundsX | 與 PageX 相同,以頁面邊界框為準。 | DestinationLeft |
DestinationZoom = 0表示沿用目前的縮放倍率,並非縮放至 0%。若要縮放至 150%,請將值設為 150。using IronPdf;
using IronPdf.Annotations;
using IronPdf.Bookmarks;
PdfDocument pdf = PdfDocument.FromFile("multipage.pdf");
// 1. Scroll to a specific vertical position on the destination page (full width shown)
LinkAnnotation scrollLink = new LinkAnnotation(0, 2)
{
X = 72, Y = 680, Width = 300, Height = 16,
DestinationType = BookmarkDestinations.PageY,
DestinationTop = 400 // scroll so y=400 sits at the top of the view
};
pdf.Annotations.Add(scrollLink);
// 2. Jump to a position AND set the zoom level
LinkAnnotation zoomLink = new LinkAnnotation(0, 3, "Zoom to figure")
{
X = 72, Y = 650, Width = 200, Height = 16,
DestinationType = BookmarkDestinations.PageZoom,
DestinationLeft = 100,
DestinationTop = 500,
DestinationZoom = 150 // 150% zoom; use 0 to inherit the current zoom
};
pdf.Annotations.Add(zoomLink);
// 3. Fit a specific rectangle on the destination page
LinkAnnotation rectLink = new LinkAnnotation(0, 4)
{
X = 72, Y = 620, Width = 200, Height = 16,
DestinationType = BookmarkDestinations.PageRect,
DestinationLeft = 50,
DestinationBottom = 100,
DestinationRight = 550,
DestinationTop = 700
};
pdf.Annotations.Add(rectLink);
pdf.SaveAs("link-destinations.pdf");using IronPdf;
using IronPdf.Annotations;
using IronPdf.Bookmarks;
PdfDocument pdf = PdfDocument.FromFile("multipage.pdf");
// 1. Scroll to a specific vertical position on the destination page (full width shown)
LinkAnnotation scrollLink = new LinkAnnotation(0, 2)
{
X = 72, Y = 680, Width = 300, Height = 16,
DestinationType = BookmarkDestinations.PageY,
DestinationTop = 400 // scroll so y=400 sits at the top of the view
};
pdf.Annotations.Add(scrollLink);
// 2. Jump to a position AND set the zoom level
LinkAnnotation zoomLink = new LinkAnnotation(0, 3, "Zoom to figure")
{
X = 72, Y = 650, Width = 200, Height = 16,
DestinationType = BookmarkDestinations.PageZoom,
DestinationLeft = 100,
DestinationTop = 500,
DestinationZoom = 150 // 150% zoom; use 0 to inherit the current zoom
};
pdf.Annotations.Add(zoomLink);
// 3. Fit a specific rectangle on the destination page
LinkAnnotation rectLink = new LinkAnnotation(0, 4)
{
X = 72, Y = 620, Width = 200, Height = 16,
DestinationType = BookmarkDestinations.PageRect,
DestinationLeft = 50,
DestinationBottom = 100,
DestinationRight = 550,
DestinationTop = 700
};
pdf.Annotations.Add(rectLink);
pdf.SaveAs("link-destinations.pdf");Imports IronPdf
Imports IronPdf.Annotations
Imports IronPdf.Bookmarks
Dim pdf As PdfDocument = PdfDocument.FromFile("multipage.pdf")
' 1. Scroll to a specific vertical position on the destination page (full width shown)
Dim scrollLink As New LinkAnnotation(0, 2) With {
.X = 72,
.Y = 680,
.Width = 300,
.Height = 16,
.DestinationType = BookmarkDestinations.PageY,
.DestinationTop = 400 ' scroll so y=400 sits at the top of the view
}
pdf.Annotations.Add(scrollLink)
' 2. Jump to a position AND set the zoom level
Dim zoomLink As New LinkAnnotation(0, 3, "Zoom to figure") With {
.X = 72,
.Y = 650,
.Width = 200,
.Height = 16,
.DestinationType = BookmarkDestinations.PageZoom,
.DestinationLeft = 100,
.DestinationTop = 500,
.DestinationZoom = 150 ' 150% zoom; use 0 to inherit the current zoom
}
pdf.Annotations.Add(zoomLink)
' 3. Fit a specific rectangle on the destination page
Dim rectLink As New LinkAnnotation(0, 4) With {
.X = 72,
.Y = 620,
.Width = 200,
.Height = 16,
.DestinationType = BookmarkDestinations.PageRect,
.DestinationLeft = 50,
.DestinationBottom = 100,
.DestinationRight = 550,
.DestinationTop = 700
}
pdf.Annotations.Add(rectLink)
pdf.SaveAs("link-destinations.pdf")Imports IronPdf
Imports IronPdf.Annotations
Imports IronPdf.Bookmarks
Dim pdf As PdfDocument = PdfDocument.FromFile("multipage.pdf")
' 1. Scroll to a specific vertical position on the destination page (full width shown)
Dim scrollLink As New LinkAnnotation(0, 2) With {
.X = 72,
.Y = 680,
.Width = 300,
.Height = 16,
.DestinationType = BookmarkDestinations.PageY,
.DestinationTop = 400 ' scroll so y=400 sits at the top of the view
}
pdf.Annotations.Add(scrollLink)
' 2. Jump to a position AND set the zoom level
Dim zoomLink As New LinkAnnotation(0, 3, "Zoom to figure") With {
.X = 72,
.Y = 650,
.Width = 200,
.Height = 16,
.DestinationType = BookmarkDestinations.PageZoom,
.DestinationLeft = 100,
.DestinationTop = 500,
.DestinationZoom = 150 ' 150% zoom; use 0 to inherit the current zoom
}
pdf.Annotations.Add(zoomLink)
' 3. Fit a specific rectangle on the destination page
Dim rectLink As New LinkAnnotation(0, 4) With {
.X = 72,
.Y = 620,
.Width = 200,
.Height = 16,
.DestinationType = BookmarkDestinations.PageRect,
.DestinationLeft = 50,
.DestinationBottom = 100,
.DestinationRight = 550,
.DestinationTop = 700
}
pdf.Annotations.Add(rectLink)
pdf.SaveAs("link-destinations.pdf")如何把連結放在現有文字上?
若要在現有文字上放置可點擊的連結,請使用 PdfPage.TextChunks 擷取文字,並用各文字區塊的 BoundingBox 設定連結大小。由於 PDF 的座標原點在頁面左下角,請將 BoundingBox.Bottom 指派給 Y。若使用 BoundingBox.Top,可點擊區域會落在可見文字上方。設定 Hidden = true 即可隱藏邊框,讓連結以不可見的方式覆蓋在渲染後的文字上。
這是讓靜態印刷目錄具有導覽功能的基礎。下方範例會掃描目錄頁面,依文字比對各項目,並在其上放置隱形的 LinkAnnotation,讓讀者前往正確的頁面。
using IronPdf;
using IronPdf.Annotations;
using IronPdf.Bookmarks;
using System;
using System.Collections.Generic;
PdfDocument pdf = PdfDocument.FromFile("report-with-toc.pdf");
// Zero-based index of the page that holds the table-of-contents text
int tocPageIndex = 1;
// Map each TOC entry's leading text to the page index it should jump to
var tocEntries = new Dictionary<string, int>
{
{ "Introduction", 2 },
{ "Methodology", 4 },
{ "Results", 6 },
{ "Conclusion", 9 },
};
// Walk the text chunks on the TOC page and overlay a clickable link on each match
foreach (var chunk in pdf.Pages[tocPageIndex].TextChunks)
{
string text = chunk.Contents.Trim();
foreach (var entry in tocEntries)
{
if (!text.StartsWith(entry.Key)) continue;
var box = chunk.BoundingBox;
pdf.Annotations.Add(new LinkAnnotation(tocPageIndex, entry.Value)
{
X = (int)box.Left,
Y = (int)box.Bottom, // Bottom, NOT Top (PDF origin is bottom-left)
Width = (int)Math.Max(box.Width, 400),
Height = (int)Math.Abs(box.Top - box.Bottom),
DestinationType = BookmarkDestinations.PageY,
DestinationTop = 792, // top of a US Letter page
Hidden = true // invisible overlay on the existing TOC text
});
break;
}
}
pdf.SaveAs("clickable-toc.pdf");using IronPdf;
using IronPdf.Annotations;
using IronPdf.Bookmarks;
using System;
using System.Collections.Generic;
PdfDocument pdf = PdfDocument.FromFile("report-with-toc.pdf");
// Zero-based index of the page that holds the table-of-contents text
int tocPageIndex = 1;
// Map each TOC entry's leading text to the page index it should jump to
var tocEntries = new Dictionary<string, int>
{
{ "Introduction", 2 },
{ "Methodology", 4 },
{ "Results", 6 },
{ "Conclusion", 9 },
};
// Walk the text chunks on the TOC page and overlay a clickable link on each match
foreach (var chunk in pdf.Pages[tocPageIndex].TextChunks)
{
string text = chunk.Contents.Trim();
foreach (var entry in tocEntries)
{
if (!text.StartsWith(entry.Key)) continue;
var box = chunk.BoundingBox;
pdf.Annotations.Add(new LinkAnnotation(tocPageIndex, entry.Value)
{
X = (int)box.Left,
Y = (int)box.Bottom, // Bottom, NOT Top (PDF origin is bottom-left)
Width = (int)Math.Max(box.Width, 400),
Height = (int)Math.Abs(box.Top - box.Bottom),
DestinationType = BookmarkDestinations.PageY,
DestinationTop = 792, // top of a US Letter page
Hidden = true // invisible overlay on the existing TOC text
});
break;
}
}
pdf.SaveAs("clickable-toc.pdf");Imports IronPdf
Imports IronPdf.Annotations
Imports IronPdf.Bookmarks
Imports System
Imports System.Collections.Generic
Dim pdf As PdfDocument = PdfDocument.FromFile("report-with-toc.pdf")
' Zero-based index of the page that holds the table-of-contents text
Dim tocPageIndex As Integer = 1
' Map each TOC entry's leading text to the page index it should jump to
Dim tocEntries As New Dictionary(Of String, Integer) From {
{"Introduction", 2},
{"Methodology", 4},
{"Results", 6},
{"Conclusion", 9}
}
' Walk the text chunks on the TOC page and overlay a clickable link on each match
For Each chunk In pdf.Pages(tocPageIndex).TextChunks
Dim text As String = chunk.Contents.Trim()
For Each entry In tocEntries
If Not text.StartsWith(entry.Key) Then Continue For
Dim box = chunk.BoundingBox
pdf.Annotations.Add(New LinkAnnotation(tocPageIndex, entry.Value) With {
.X = CInt(box.Left),
.Y = CInt(box.Bottom), ' Bottom, NOT Top (PDF origin is bottom-left)
.Width = CInt(Math.Max(box.Width, 400)),
.Height = CInt(Math.Abs(box.Top - box.Bottom)),
.DestinationType = BookmarkDestinations.PageY,
.DestinationTop = 792, ' top of a US Letter page
.Hidden = True ' invisible overlay on the existing TOC text
})
Exit For
Next
Next
pdf.SaveAs("clickable-toc.pdf")Imports IronPdf
Imports IronPdf.Annotations
Imports IronPdf.Bookmarks
Imports System
Imports System.Collections.Generic
Dim pdf As PdfDocument = PdfDocument.FromFile("report-with-toc.pdf")
' Zero-based index of the page that holds the table-of-contents text
Dim tocPageIndex As Integer = 1
' Map each TOC entry's leading text to the page index it should jump to
Dim tocEntries As New Dictionary(Of String, Integer) From {
{"Introduction", 2},
{"Methodology", 4},
{"Results", 6},
{"Conclusion", 9}
}
' Walk the text chunks on the TOC page and overlay a clickable link on each match
For Each chunk In pdf.Pages(tocPageIndex).TextChunks
Dim text As String = chunk.Contents.Trim()
For Each entry In tocEntries
If Not text.StartsWith(entry.Key) Then Continue For
Dim box = chunk.BoundingBox
pdf.Annotations.Add(New LinkAnnotation(tocPageIndex, entry.Value) With {
.X = CInt(box.Left),
.Y = CInt(box.Bottom), ' Bottom, NOT Top (PDF origin is bottom-left)
.Width = CInt(Math.Max(box.Width, 400)),
.Height = CInt(Math.Abs(box.Top - box.Bottom)),
.DestinationType = BookmarkDestinations.PageY,
.DestinationTop = 792, ' top of a US Letter page
.Hidden = True ' invisible overlay on the existing TOC text
})
Exit For
Next
Next
pdf.SaveAs("clickable-toc.pdf")可以建立「回到頂端」或指向同頁的連結嗎?
可以。系統完整支援同頁連結,也就是 pageIndex 與 destinationPageIndex 相同,這是建立「回到頂端」按鈕的常用方式。將同頁連結設為 DestinationType = BookmarkDestinations.PageY,並給定較高的 DestinationTop 值,點擊後便會捲動到目前頁面的頂端。您可以在每一頁的頁尾加上相同連結,讓長篇報告的每頁都能回到頂端。
使用 PDF 註解時有哪些最佳實務?
在您的應用程式中實作註解功能時,請考慮這些最佳實務:
- 座標系統:PDF 以頁面左下角為座標原點,許多 UI 框架則以左上角為原點。計算座標時,請留意這項差異。
- 效能最佳化:新增多個註解時,可採用批次操作,待全部新增後再儲存。這有助於提升效能,尤其是在處理大型 PDF 文件時。
- 註解可見性: 並非所有PDF檢視器以相同方式顯示註解。 在不同的檢視器中測試您附加註解的 PDF,以確保一致的使用者體驗。
- 與表單整合:註解可為 PDF 表單提供與內容相關的說明或操作指引,同時保留表單結構。
- 安全考量: 在處理敏感文件時,請記住註解可能包含機密資訊。 實作適當的安全措施來保護註解內容。
- 無障礙性:可新增註解,為身心障礙使用者補充背景資訊,以提升文件的無障礙性。這符合 PDF/UA 的無障礙文件要求。
為什麼註解的座標系統很重要?
源自左下角的 PDF 座標可能會讓習慣左上角原點系統的開發人員感到困惑。 不正確的座標計算可能會將註解放置在意想不到的位置,可能會遮蔽重要內容或出現在頁面外。 與 UI 框架或使用者輸入系統整合時,務必適當轉換座標。
在實作點擊新增註解等功能,或將使用者操作的螢幕座標轉換成 PDF 座標時,了解座標系統尤其重要。若定位需求較複雜,可考慮使用 IronPDF 的視窗與縮放功能,讓註解在不同檢視條件下都能正確顯示。
如何在新增多個註解時提升效能?
新增多個註解時,請先將所有註解新增至集合,然後再儲存文件,以進行批次作業。 此方法可減少檔案 I/O 作業,並大幅提升效能,尤其是在處理大型 PDF 或連續處理多個文件時。 大量處理時可加入進度指示,改善使用者體驗。
// Example of batch annotation processing
var annotations = new List<TextAnnotation>();
for (int i = 0; i < 100; i++)
{
annotations.Add(new TextAnnotation(0)
{
Title = $"Note {i}",
Contents = $"Content for note {i}",
X = 50 + (i * 10),
Y = 700 - (i * 20)
});
}
// Add all annotations at once
foreach (var annotation in annotations)
{
pdf.Annotations.Add(annotation);
}
// Save once after all additions
pdf.SaveAs("batch-annotated.pdf");// Example of batch annotation processing
var annotations = new List<TextAnnotation>();
for (int i = 0; i < 100; i++)
{
annotations.Add(new TextAnnotation(0)
{
Title = $"Note {i}",
Contents = $"Content for note {i}",
X = 50 + (i * 10),
Y = 700 - (i * 20)
});
}
// Add all annotations at once
foreach (var annotation in annotations)
{
pdf.Annotations.Add(annotation);
}
// Save once after all additions
pdf.SaveAs("batch-annotated.pdf");Imports System.Collections.Generic
' Example of batch annotation processing
Dim annotations As New List(Of TextAnnotation)()
For i As Integer = 0 To 99
annotations.Add(New TextAnnotation(0) With {
.Title = $"Note {i}",
.Contents = $"Content for note {i}",
.X = 50 + (i * 10),
.Y = 700 - (i * 20)
})
Next
' Add all annotations at once
For Each annotation In annotations
pdf.Annotations.Add(annotation)
Next
' Save once after all additions
pdf.SaveAs("batch-annotated.pdf")Imports System.Collections.Generic
' Example of batch annotation processing
Dim annotations As New List(Of TextAnnotation)()
For i As Integer = 0 To 99
annotations.Add(New TextAnnotation(0) With {
.Title = $"Note {i}",
.Contents = $"Content for note {i}",
.X = 50 + (i * 10),
.Y = 700 - (i * 20)
})
Next
' Add all annotations at once
For Each annotation In annotations
pdf.Annotations.Add(annotation)
Next
' Save once after all additions
pdf.SaveAs("batch-annotated.pdf")若要提升大量處理時的效能,可使用非同步處理或多執行緒技術平行執行註解作業。
如何確保不同 PDF 檢視器之間的相容性?
不同 PDF 檢視器顯示註解時,圖示樣式、彈出視窗的行為或定位方式可能有差異。請務必使用 Adobe Acrobat、Chrome、Edge 和行動 PDF 閱讀器等常見工具測試含有註解的 PDF,確認註解在使用者可能採用的平台上都能正確顯示並正常運作。
某些檢視器可能不支援所有註解類型,或以不同方式顯示。 為了達到最大的相容性,請堅持使用標準註解類型,並避免依賴檢視器特定的功能。 在部署到特定環境(例如Azure或AWS)時,請在目標環境的預設PDF檢視器中測試註解以確保行為一致。
若要進一步了解其他功能,請參閱編輯 PDF教學。
常見問題
如何使用IronPDF在C#中添加註解到PDF?
要在C#中通過IronPDF添加註解,您使用Annotations.Add方法並使用TextAnnotation類定義註解參數。載入PDF文件,創建具有所需屬性,如標題、內容和座標的註解實例,然後將其傳遞給Annotations.Add方法。
使用IronPDF可以添加哪些類型的PDF註解?
IronPDF支持多種註解類型,包括文本評論和便利貼。您可以添加註解以提供評論、反饋或為PDF的特定部分提供附加信息,增強協作和文件審查工作流程。
如何使用IronPDF編輯PDF中的現有註解?
要使用IronPDF編輯註解,訪問PDF文件中的Annotations集合。找到所需的註解並更新其屬性,如Title、Contents、X或Y,以修改其內容或位置。保存變更以更新文件。
PDF註解在商業工作流程中提供了哪些優勢?
PDF註解通過允許用戶在不改變原始內容的情況下添加反饋、上下文或提醒,來增強協作。這在文件審查、合同協作和質量保證過程中促進了溝通。
在添加註解時,PDF座標如何運作?
PDF座標起點位於頁面左下角。在使用X和Y座標定位註解時,確保計算此起點以正確放置註解。也應考慮US Letter和A4這樣的標準頁面尺寸以實現精確。
我可以使用IronPDF從PDF中刪除註解嗎?
是的,IronPDF允許您使用像RemoveAt這樣的方法刪除特定註解,使用RemoveAllAnnotationsForPage進行頁面級刪除,或使用Clear刪除文件中的所有註解。
添加文本註解時,哪些屬性可以在IronPDF中自訂?
您可以自訂TextAnnotation類的各種屬性,包括Title、Contents、X、Y、PageIndex、Subject、Icon和Open。這些屬性控制註解在PDF中的外觀和位置。
如何使用IronPDF在PDF內添加可點擊的內部鏈結?
要創建內部鏈結,使用IronPDF中的LinkAnnotation類。用位置和大小座標定義可點擊區域,並設置目標頁面。您可以使用DestinationType等屬性自訂外觀和目標行為。
為何測試跨不同閱讀器具有註解的PDF很重要?
不同的PDF閱讀器會以不同的方式呈現註解,影響圖示樣式和彈出行為。在流行的檢視器如Adobe Acrobat、Chrome和行動設備閱讀器中進行測試可確保在各平台上的一致外觀和功能。
使用IronPDF處理PDF註解有哪些最佳實踐?
最佳實踐包括了解從左下角開始的坐標系統、通過批量添加註解來優化性能、確保跨檢視器兼容性,以及考慮對註解內容的安全措施。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。