如何在 C# 中新增和編輯 PDF 註解

如何在 C# 中新增和編輯 PDF 註釋

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

註解允許使用者在文件的特定部分新增註解、提醒或其他資訊。 它們增強了在處理 PDF 時的協作和溝通,使得用戶能夠註解、評論並為共享內容提供上下文。

快速入門:使用 IronPDF 為 PDF 添加註釋

本快速指南演示了如何使用 IronPDF 在 C# 中毫不費力地為 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 頁面上新增類似"便條"的註解。 透過使用 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
$vbLabelText   $csharpLabel

帶註解的 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");
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 文件

上述 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");
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 上的單一註解

上述 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")
$vbLabelText   $csharpLabel

準備好看看您還能做些什麼嗎? 在此查看我們的教學頁面: 編輯 PDFs

常見問題解答

如何在 C# 中為 PDF 加入註解?

您可以使用 IronPDF 在 C# 中為 PDF 新增註解。首先,下載 PDF 註解的 C# 函式庫。載入現有的 PDF 文件或建立新的 PDF 文件,然後使用 Annotations 屬性的 Add 方法插入註解或備註。

如何使用 C# 編輯 PDF 中的現有註解?

若要使用 C# 編輯 PDF 中的現有註解,可透過 IronPDF 的 Annotations 屬性存取註解集合。使用新資訊更新 Title、Contents、X 和 Y 等屬性,以修改註解。

使用 C# 從 PDF 移除註解的過程是什麼?

您可以使用 IronPDF 從 PDF 移除註解,方法包括 RemoveAtt 移除特定註解、RemoveAllAnnotationsForPage 移除頁面上的所有註解,或 Clear 移除文件中的所有註解。

我可以在網頁瀏覽器中檢視 PDF 註解嗎?

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

在 PDF 文件中加入註解有什麼好處?

使用 IronPdf 為 PDF 文件新增註解,可讓使用者新增註解、提醒事項或其他資訊,改善共用內容的溝通和上下文,從而增強協作能力。

如何找到數位簽章和加密服務的說明文件?

數位簽章、編輯、加密和保護服務的文件可在 IronSecureDoc 的官方文件頁面找到:https://ironsoftware.com/enterprise/securedoc/docs/。

有哪些 C# 方法可用於管理 PDF 註解?

IronPDF 提供多種 C# 方法來管理 PDF 註解,包括用於新增的 Add 方法、用於移除特定註解的 RemoveAt 方法,以及用於清除所有註解的 Clear 方法。

在處理註解時,IronPDF 是否與 .NET 10 完全相容?

是的,IronPDF 與 .NET 10 完全相容(: by design),並支援註解操作 - 新增、編輯、移除 - 與 .NET 10 專案中的正常操作一樣,不需要特殊的工作方式。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 16,493,056 | Version: 2025.11 剛發表