C# 如何给现有/新建PDF文档添加注释或标注

这篇文章主要介绍如何使用免费版PDF组件Free Spire.PDF及C#编程语言给现有PDF文档或新建的PDF文档中的文本添加注释或标注(完整代码附在文章末尾)。

首先,下载 Free Spire.PDF 组件并安装后,打开Visual Studio创建项目并引用Spire.Pdf.dll,如下图:


接下来添加命名空间:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;

命名空间添加后,我们就可以使用其中的类和方法来实现添加注释。

给现有的PDF文档中的文本添加注释

文档:


接下来的部分主要阐述如何给以上PDF文档中的文本“唐朝”添加注释。

1. 从系统加载PDF文档。

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("唐朝.pdf");

2. 声明一个PdfTextFind类型的数组并创建一个新的PDF字体。

PdfTextFind[] result = null;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);

3. 获取文本,创建注释并添加到PDF页面。

遍历PDF文档的所有页面,获取需要添加注释的文本并将它们保存到声明的数组中,然后遍历数组,为每个文本创建注释并指定注释的作者、注释内容、注释的文本,位置,及字体等,然后设置注释的边框、颜色,最后将注释添加到页面上。

foreach (PdfPageBase page in doc.Pages)
{
    result =page.FindText("唐朝").Finds;
    foreach (PdfTextFind text in result)
    {
       PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("汤姆", "618年—907年", text.SearchText, text.Position, font);
       annotation.Border= new PdfAnnotationBorder(0.75f);
       annotation.TextMarkupColor= Color.Green; 
       text.SearchPage.AnnotationsWidget.Add(annotation);
    }
}

4. 保存文档。

doc.SaveToFile("输出.pdf");

以上代码需要注意的几点:

1. 这里我是将文档放在项目的Debug目录下的。你也可以从路径加载文档,如:doc.LoadFromFile(@"E:\Program Files\唐朝.pdf");

2. Free Spire.PDF组件提供了几个不同的PDF字体类,因为这里我们使用的文档是中文,所以要选择支持中文的字体类,如PdfTrueTypeFont类和PdfCjkStandardFont类(支持繁体中文)。同时,在使用系统字体时也要注意选择支持中文(Unicode)的字体,如“Arial Unicode MS”。

3. 这里我遍历了PDF文档的所有页面,如果你只需要对某一页的文本添加注释,则只需获取该页,如:PdfPageBase page =doc.Pages[0];

 

运行结果:


 

给新建的PDF文档添加注释

给新建PDF文档添加注释较为简单,代码如下:

//创建文档
PdfDocument doc1 = new PdfDocument();
PdfPageBase page1 =doc1.Pages.Add();
//向文档添加文本
PdfFont font1 = new PdfFont(PdfFontFamily.Helvetica,13);
string text1 = "HelloWorld";
PointF point = new PointF(200, 100);
page1.Canvas.DrawString(text1, font1, PdfBrushes.CadetBlue, point);
 
//给文本创建注释
PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "这是一个文本注释",text1, new PointF(0,0), font1);
annotation1.Border = new PdfAnnotationBorder(0.75f);
annotation1.TextMarkupColor = Color.Green;
annotation1.Location = new PointF(point.X +doc1.PageSettings.Margins.Left, point.Y + doc1.PageSettings.Margins.Left);
//将注释添加到页面
(page1 as PdfNewPage).Annotations.Add(annotation1);
//保存文档
doc1.SaveToFile("新建.pdf");

运行结果:



完整代码:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
 
namespace PDF文档添加注释
{
    class Program
    {
        static void Main(string[] args)
        {
            //给现有的PDF文档中的文本添加注释
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("唐朝.pdf");
            PdfTextFind[] result = null;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
            foreach (PdfPageBasepage in doc.Pages)
            {
               result = page.FindText("唐朝").Finds;
               foreach (PdfTextFind text in result)
               {
                   PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("汤姆", "618年—907年",text.SearchText, text.Postition, font);
                   annotation.Border = new PdfAnnotationBorder(0.75f);
                   annotation.TextMarkupColor = Color.Green;
                   text.SearchPage.AnnotationsWidget.Add(annotation);
               }
            }
            doc.SaveToFile("输出.pdf");
 
            //给新建的PDF文档添加注释
            PdfDocument doc1 = new PdfDocument();
            PdfPageBase page1 = doc1.Pages.Add();
            PdfFont font1 = new PdfFont(PdfFontFamily.Helvetica,13);
            string text1 = "HelloWorld";
            PointF point = new PointF(200, 100);
            page1.Canvas.DrawString(text1, font1, PdfBrushes.CadetBlue,point);
 
            PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "这是一个文本注释",text1, new PointF(0,0), font1);
            annotation1.Border = new PdfAnnotationBorder(0.75f);
            annotation1.TextMarkupColor = Color.Green;
 
            annotation1.Location = new PointF(point.X + doc1.PageSettings.Margins.Left,point.Y + doc1.PageSettings.Margins.Left);
            (page1 as PdfNewPage).Annotations.Add(annotation1);
 
            doc1.SaveToFile("新建.pdf");
        }
    }
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值