如何添加和編輯 PDF 註釋

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

註解允許使用者在文件的特定部分新增評論、提醒或附加資訊。 它們提升了協作和交流能力,使用戶能夠對共享內容進行註釋、評論並提供背景資訊。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

添加註解範例

PDF 註釋允許在 PDF 頁面上添加類似「便利貼」的評論。 通過使用 Annotations 屬性的 Add 方法,可以以程式化方式添加註釋。

提示
所有頁面索引均遵循基於零的索引。

:path=/static-assets/pdf/content-code-examples/how-to/annotation-add-annotation.cs
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

帶有註釋的PDF

上方 PDF 文件中的註解可以使用 Chrome 瀏覽器查看。


檢索和編輯註解範例

檢索和編輯 PDF 註釋可以通過增強清晰度、準確性和可用性來改善協作。 透過 Annotations 屬性訪問註釋集合,並使用新資訊更新屬性,如 Title、Contents、X、Y 等。

:path=/static-assets/pdf/content-code-examples/how-to/annotation-edit-annotation.cs
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")
VB   C#

已編輯註解的PDF

上方 PDF 文件中的註解可以使用 Chrome 瀏覽器查看。


移除註解範例

使用以下方法輕鬆刪除不必要或過時的註釋:RemoveAtRemoveAllAnnotationsForPageClear

  • RemoveAt:移除具有指定索引的單個註釋。
  • RemoveAllAnnotationsForPage:移除指定頁面上的所有註解。
  • 清除:移除文件中的所有註釋。

移除單個註解

要移除單一註解,使用 RemoveAt 方法並根據註解集合的索引來指定相應的索引。

:path=/static-assets/pdf/content-code-examples/how-to/annotation-remove-single-annotation.cs
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")
VB   C#

已刪除 PDF 上的單個註解

上方 PDF 文件中的註解可以使用 Chrome 瀏覽器查看。

移除所有註解

若要移除特定頁面上的所有註解,請使用 RemoveAllAnnotationsForPage 方法並指定頁面索引。 如果您想要移除整個文件中的所有註解,只需在 Annotations 屬性上調用 Clear 方法。

:path=/static-assets/pdf/content-code-examples/how-to/annotation-remove-all-annotation.cs
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")
VB   C#

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。