如何添加和编辑 PDF 注释

Chaknith related to 如何添加和编辑 PDF 注释
查克尼特·宾
2023年六月1日
更新 2024年十二月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

注释允许用户在文档的特定部分添加评论、提醒或附加信息。 它们增强了协作和沟通功能,使用户能够对 PDF 进行注释、评论,并为共享内容提供上下文。


开始使用IronPDF!

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

第一步:
green arrow pointer



添加注释示例

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");

带注释的 PDF

上述 PDF 文档中的注释可通过 Chrome 浏览器查看。


检索和编辑注释示例

检索和编辑 PDF 注释通过提高清晰度、准确性和可用性来改善协作。 通过Annotations属性访问注释集合,并使用新信息更新例如标题、内容、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");

带有编辑注释的 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");

删除 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");
Chaknith related to 移除所有注释
软件工程师
Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。