首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >如何使用C#代码向 PDF 文档添加多种类型的注释

如何使用C#代码向 PDF 文档添加多种类型的注释

原创
作者头像
用户12401097
发布2026-08-28 14:53:14
发布2026-08-28 14:53:14
1340
举报

PDF 注释工具可以帮助用户在 PDF 文档中高亮文本、添加便签、绘制图形以及直接插入评论。这些功能适用于提供反馈、记录笔记、标注设计稿以及进行文档协作等场景。掌握 PDF 注释功能,可以优化文档处理流程,提高处理 PDF 文件时的工作效率。

本文将介绍如何使用 C# 以编程方式向 PDF 文档添加各种类型的注释。

安装 PDF 处理组件

首先,需要将相关 DLL 文件添加为 .NET 项目中的引用。所需 DLL 文件可以通过官方渠道下载,或者通过 NuGet 包管理器进行安装。

安装完成后,即可在 .NET 项目中调用相关 API,实现 PDF 文档的创建、编辑和处理等功能。

代码语言:C#
复制
PM> Install-Package Spire.PDF

使用 C# 向 PDF 添加标记注释

PDF 中的标记注释(Markup Annotation)可以帮助用户选择文本并添加彩色背景,从而突出显示文档中的重要内容或引起对特定文本的关注。

在 C# 中,可以通过相关 PDF 处理接口实现标记注释功能。具体操作步骤如下:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 根据找到的文本创建 PdfTextMarkupAnnotation 对象。
  6. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  7. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf;
using Spire.Pdf.Texts;
using System.Drawing;

namespace AddMarkupAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("In order to help make the Site a secure environment" +
                " for the purchase and sale of Marketplace Offerings, all users are required to accept and " +
                "comply with these Terms of Service.");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 设置注释文本
            String text = "添加的 PDF 标记注释。";

            // 遍历找到文本的边界区域
            for (int i = 0; i < textFragment.Bounds.Length; i++)
            {
                // 获取指定文本区域
                RectangleF rect = textFragment.Bounds[i];

                // 创建文本标记注释
                PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Administrator", text, rect);

                // 设置标记颜色
                annotation.TextMarkupColor = Color.Green;

                // 将注释添加到页面注释集合中
                page.Annotations.Add(annotation);
            }

            // 将结果保存为文件
            doc.SaveToFile("AddMarkups.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加自由文本注释

PDF 中的自由文本注释(Free Text Annotation)允许用户直接在文档中添加类似手写或输入的文本内容,就像在纸质文档上做笔记一样。

在 C# 中,可以通过相关 PDF 处理接口实现自由文本注释功能。以下是创建自由文本注释的具体步骤:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 指定添加注释的位置坐标(X 和 Y 坐标)。
  6. 创建 PdfFreeTextAnnotation 对象,并设置文本内容、字体、边框、颜色等属性。
  7. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  8. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddFreeTextAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF rect = textFragment.Bounds[0];

            // 获取添加注释的位置坐标
            float x = rect.Right;
            float y = rect.Bottom;

            // 创建自由文本注释
            RectangleF rectangle = new RectangleF(x, y, 130, 30);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rectangle);

            // 设置注释内容
            textAnnotation.Text = "这是一个自由文本注释。";

            // 设置注释的其他属性
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10f, PdfFontStyle.Bold);
            PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.Purple;
            textAnnotation.Color = Color.Green;
            textAnnotation.Opacity = 1.0f;

            // 将注释添加到页面注释集合中
            page.Annotations.Add(textAnnotation);

            // 保存结果文件
            doc.SaveToFile("FreeTextAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加弹出式注释

PDF 中的弹出式注释(Popup Annotation)允许用户添加一个可点击展开的小标签或评论。当用户点击该注释时,可以显示额外的信息或简短说明。

在 C# 中,可以通过 PDF 处理接口实现弹出式注释功能。以下是向 PDF 添加弹出式注释的具体步骤:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 指定添加注释的位置坐标(X 和 Y 坐标)。
  6. 创建 PdfPopupAnnotation 对象,并设置文本内容、图标、颜色等属性。
  7. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  8. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddPopupAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF textBound = textFragment.Bounds[0];

            // 获取添加注释的位置坐标
            float x = textBound.Right + 5;
            float y = textBound.Top - 15;

            // 创建弹出式注释
            RectangleF rectangle = new RectangleF(x, y, 30, 30);
            PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rectangle);

            // 设置注释文本内容
            popupAnnotation.Text = "这是一个弹出式注释。";

            // 设置注释图标和颜色
            popupAnnotation.Icon = PdfPopupIcon.Comment;
            popupAnnotation.Color = Color.Red;

            // 将注释添加到页面注释集合中
            page.Annotations.Add(popupAnnotation);

            // 保存结果文件
            doc.SaveToFile("PopupAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加形状注释

PDF 中的形状注释(Shape Annotation)是指在 PDF 文档中添加矩形、圆形、线条、箭头等图形元素,用于突出显示内容或提供额外信息。

在 C# 中,可以通过相关 PDF 处理接口创建自定义形状注释。具体操作步骤如下:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 指定添加注释的坐标位置。
  6. 创建 PdfPolyLineAnnotation 对象,并设置注释文本内容。
  7. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  8. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddShapeAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF textBound = textFragment.Bounds[0];

            // 获取添加注释的坐标
            float left = textBound.Left;
            float top = textBound.Top;
            float right = textBound.Right;
            float bottom = textBound.Bottom;

            // 创建形状注释
            PdfPolyLineAnnotation polyLineAnnotation = new PdfPolyLineAnnotation(page, new PointF[] {
                new PointF(left, top),
                new PointF(right, top),
                new PointF(right - 5, bottom),
                new PointF(left - 5, bottom),
                new PointF(left, top)
            });

            // 设置注释文本
            polyLineAnnotation.Text = "这是一个形状注释。";

            // 将注释添加到页面注释集合中
            page.Annotations.Add(polyLineAnnotation);

            // 保存结果文件
            doc.SaveToFile("ShapeAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加网页链接注释

PDF 中的网页链接注释(Web Link Annotation)可以帮助用户在文档中嵌入超链接。当读者点击该链接时,可以直接跳转到指定的网站页面。

在 C# 中,可以通过相关 PDF 处理接口实现网页链接注释功能。以下是添加网页链接注释的具体步骤:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 根据查找到的文本创建 PdfUrlAnnotation 对象。
  6. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  7. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddUrlAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF textBound = textFragment.Bounds[0];

            // 创建网页链接注释
            PdfUriAnnotation urlAnnotation = new PdfUriAnnotation(textBound, "https:\\\\www.e-iceblue.com\\");

            // 将注释添加到页面注释集合中
            page.Annotations.Add(urlAnnotation);

            // 保存结果文件
            doc.SaveToFile("UrlAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加文件链接注释

PDF 中的文件链接注释(File Link Annotation)是一种交互式链接功能,允许用户直接从 PDF 文档跳转或访问外部文件。

在 C# 中,可以通过相关 PDF 处理接口实现文件链接注释功能。以下是向 PDF 文档添加文件链接注释的具体步骤:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 根据查找到的文本创建 PdfFileLinkAnnotation 对象。
  6. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  7. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddFileLinkAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF textBound = textFragment.Bounds[0];

            // 创建文件链接注释
            PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(textBound, "C:\\Users\\Administrator\\Desktop\\Report.docx");

            // 将注释添加到页面注释集合中
            page.Annotations.Add(fileLinkAnnotation);

            // 保存结果文件
            doc.SaveToFile("FileLinkAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

使用 C# 向 PDF 添加文档链接注释

PDF 中的文档链接注释(Document Link Annotation)是一种超链接功能,允许用户在同一个 PDF 文档内的不同页面或章节之间进行跳转。

在 C# 中,可以通过相关 PDF 处理接口实现文档链接注释功能。以下是向 PDF 文档添加文档链接注释的具体步骤:

  1. 创建一个 PdfDocument 对象。
  2. 从指定文件路径加载 PDF 文档。
  3. 获取文档中的指定页面。
  4. 使用 PdfTextFinder 类提供的方法,在页面中查找目标文本。
  5. 根据查找到的文本创建 PdfDocumentLinkAnnotation 对象。
  6. 使用 PdfPageBase.Annotations.Add() 方法,将注释添加到页面中。
  7. 将处理后的文档保存为新的 PDF 文件。

完整示例代码如下:

代码语言:C#
复制
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using Spire.Pdf.General;
using System.Drawing;

namespace AddDocumentLinkAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 PdfDocument 对象
            PdfDocument doc = new PdfDocument();

            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // 获取指定页面
            PdfPageBase page = doc.Pages[0];

            // 基于页面创建 PdfTextFinder 对象
            PdfTextFinder finder = new PdfTextFinder(page);

            // 设置查找选项
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 查找指定文本所在位置
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // 获取第一个匹配结果
            PdfTextFragment textFragment = fragments[0];

            // 获取文本边界区域
            RectangleF textBound = textFragment.Bounds[0];

            // 创建文档链接注释
            PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(textBound);

            // 设置注释跳转目标
            documentLinkAnnotation.Destination = new PdfDestination(doc.Pages[1]);

            // 将注释添加到页面注释集合中
            page.Annotations.Add(documentLinkAnnotation);

            // 保存结果文件
            doc.SaveToFile("DocumentLinkAnnotation.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

总结

本文介绍了如何使用 C# 向 PDF 文档添加文档链接注释(Document Link Annotation)。通过创建 PDF 文档对象、加载目标文件、定位指定文本,并根据文本区域添加链接注释,可以实现 PDF 内部页面之间的快速跳转功能。

在实际应用中,文档链接注释可以用于创建目录导航、章节跳转、参考链接等交互功能,从而提升 PDF 文档的可读性和使用体验。通过设置注释的目标页面,用户可以在点击指定区域后直接跳转到 PDF 文档中的其他位置,实现更加高效的文档浏览和管理。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 安装 PDF 处理组件
  • 使用 C# 向 PDF 添加标记注释
  • 使用 C# 向 PDF 添加自由文本注释
  • 使用 C# 向 PDF 添加弹出式注释
  • 使用 C# 向 PDF 添加形状注释
  • 使用 C# 向 PDF 添加网页链接注释
  • 使用 C# 向 PDF 添加文件链接注释
  • 使用 C# 向 PDF 添加文档链接注释
  • 总结
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档