如何添加和编辑 PDF 注释
注释允许用户在文档的特定部分添加评论、提醒或附加信息。 它们增强了协作和沟通功能,使用户能够对 PDF 进行注释、评论,并为共享内容提供上下文。
开始使用IronPDF!
立即在您的项目中开始使用IronPDF,并享受免费试用。
如何添加和编辑 PDF 注释
- 下载用于PDF注释的C#库
- 加载现有或渲染新的 PDF 文档
- 使用
Add
方法添加注释 - 检索和编辑 PDF 注释
- 从PDF文档中移除注释
添加注释示例
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 浏览器查看。
移除注释示例
可以通过以下方法轻松移除不必要或过时的注释:RemoveAt
、RemoveAllAnnotationsForPage
和Clear
。
- 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");