如何在C#中添加和编辑PDF注释

如何在 C# 中添加和编辑 PDF 注释

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

注释允许用户在文档的特定部分添加评论、提醒或附加信息。 它们在处理PDF时增强了协作和交流,使用户能够注释、评论和为共享内容提供背景。

快速入门:使用IronPDF向PDF添加注释

本快速指南展示了如何使用C#中的IronPDF轻松地向PDF文档添加文本注释。 只需几行代码,开发人员即可通过添加评论或注释来增强他们的PDF,提高文档的互动性和协作性。 首先加载您的PDF,然后使用AddTextAnnotation方法快速插入注释。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronPDF

    PM > Install-Package IronPdf

  2. 复制并运行这段代码。

    PdfDocument.FromFile("input.pdf")
        .Annotations.Add(new TextAnnotation(0) { Title="Note", Contents="Review this section.", X=50, Y=700 })
        .SaveAs("annotated.pdf");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronPDF,免费试用!
    arrow pointer


添加注释示例

PDF注释允许在PDF页面上添加类似"便签"的评论。 通过使用注释属性的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
$vbLabelText   $csharpLabel

带有注释的PDF

可以使用Chrome浏览器查看以上PDF文档中的注释。


检索和编辑注释示例

检索和编辑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")
$vbLabelText   $csharpLabel

带有编辑注释的PDF

可以使用Chrome浏览器查看以上PDF文档中的注释。


移除注释示例

使用以下方法轻松移除不需要或过时的注释:RemoveAtRemoveAllAnnotationsForPageClear

  • RemoveAt:使用指定索引移除单个注释。
  • RemoveAllAnnotationsForPage:移除指定页面上的所有注释。
  • Clear:移除文档中的所有注释。

移除单个注释

要移除单个注释,使用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")
$vbLabelText   $csharpLabel

移除了PDF上的单个注释

可以使用Chrome浏览器查看以上PDF文档中的注释。

移除所有注释

要移除特定页面上的所有注释,请使用RemoveAllAnnotationsForPage方法并指定页面索引。 如果想移除整个文档中的所有注释,只需在注释属性上调用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")
$vbLabelText   $csharpLabel

准备好看看您还能做些什么吗? 请查看我们的教程页面:编辑 PDF

常见问题解答

我如何在 C# 中向 PDF 添加注释?

你可以使用 IronPDF 在 C# 中向 PDF 添加注释。首先,下载用于 PDF 注释的 C# 库。加载现有的或创建新的 PDF 文档,然后使用注释属性的 Add 方法插入评论或注释。

我如何使用 C# 编辑 PDF 中现有的注释?

要使用 C# 编辑 PDF 中现有的注释,请通过 IronPDF 的注释属性访问注释集合。更新如标题、内容、X 和 Y 等属性以修改注释。

使用 C# 从 PDF 中删除注释的过程是什么?

你可以通过使用诸如 RemoveAt 来删除特定注释、RemoveAllAnnotationsForPage 来清除页面上的所有注释或 Clear 来删除文档中的所有注释等方法来使用 IronPDF 从 PDF 中删除注释。

我能在网络浏览器中查看 PDF 注释吗?

是的,使用 IronPDF 添加到 PDF 文档的注释可以在 Chrome 等网络浏览器中查看。

向 PDF 文档添加注释有什么好处?

使用 IronPDF 向 PDF 文档添加注释通过允许用户添加评论、提醒或额外信息来增强协作,改善共享内容的沟通和上下文。

我如何找到数字签名和加密服务的文档?

可以在 IronSecureDoc 的官方文档页面找到有关数字签名、涂抹、加密和保护服务的文档:https://Iron Software.com/enterprise/securedoc/docs/。

有哪些 C# 方法可用于管理 PDF 注释?

IronPDF 提供多种 C# 方法来管理 PDF 注释,包括用于添加的 Add 方法、用于删除特定注释的 RemoveAt 方法和用于清除所有注释的 Clear 方法。

IronPDF 在处理注释时是否与 .NET 10 完全兼容?

是的。IronPDF 与 .NET 10 完全兼容(这是设计使然),并且支持在 .NET 10 项目中像往常一样进行注释操作(添加、编辑、删除),无需特殊的变通方法。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 16,493,056 | Version: 2025.11 刚刚发布