如何添加和编辑 PDF 注释

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

查克尼特·宾

注释允许用户为文档的特定部分添加注释、提醒或其他信息。在处理 PDF 文档时,注释可加强协作和交流,使用户能够对共享内容进行注释、评论并提供上下文。


适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronPDFNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。

适用于PDF的C# NuGet库 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 注释可提高清晰度、准确性和可用性,从而改善协作。通过注释属性访问注释集合,并根据新信息更新标题、内容、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 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。