`

C# 在PDF中添加不同类型的注释(5种)

阅读更多

向文档添加注释,是一种比较常用的向读者传递某些重要信息的手段。通过编程的方式来添加PDF注释,我们可以自定义注释的外观、类型及其他一些个性化的设置,这种可供选择的操作在编程中提供了更多的实用性。因此,接下来,本文将介绍添加几种不同类型的PDF注释的方法。主要包含以下5种:

  • 添加弹出式注释(Popup Annotation)
  • 添加自由文本注释(Free Text Annotation)
  • 添加链接式注释(Link Annotation)
  • 添加多边形注释(Polygon Annotation)
  • 添加线性注释(Line Annotation)

 

工具使用

代码操作

1.弹出式注释(Popup Annotation

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

namespace Annotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化PdfDocument类实例,并加载测试文档            
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            //获取第一页
            PdfPageBase page = doc.Pages[0];

            //调用方法FindText()查找需要添加注释的字符串
            PdfTextFind[] results = page.FindText("IPCC").Finds;

            //指定注释添加的位置
            float x = results[0].Position.X - doc.PageSettings.Margins.Top;
            float y = results[0].Position.Y - doc.PageSettings.Margins.Left + results[0].Size.Height - 23;

            //创建弹出式注释
            RectangleF rect = new RectangleF(x, y, 10, 0);
            PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect); 

            //添加注释内容,并设置注释的图标类型和颜色 
            popupAnnotation.Text = "IPCC,This is a scientific and intergovernmental body under the auspices of the United Nations.";
            popupAnnotation.Icon = PdfPopupIcon.Help; 
            popupAnnotation.Color = Color.DarkOliveGreen;

            //添加注释到文件
            page.AnnotationsWidget.Add(popupAnnotation);

            //保存并打开文档
            doc.SaveToFile("Annotation.pdf");
            System.Diagnostics.Process.Start("Annotation.pdf");
        }
    }
}

 在选择注释标签类型时,有以下几种类型可供选择:

 



 

添加效果:

 

 

2. 自由文本注释(Free Text Annotation 

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

namespace FreeTextAnnotation_pdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument类对象,加载测试文档
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            PdfPageBase page = doc.Pages[0];
            //初始化RectangleF类,指定注释添加的位置、注释图标大小
            RectangleF rect = new RectangleF(50, 500, 100, 40);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

            ////添加注释内容
            textAnnotation.Text = "This is just a sample, please refer the original article to see more!";

            //设置注释属性,包括字体、字号、注释边框粗细、边框颜色、填充颜色等
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9);
            PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.White;
            textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
            textAnnotation.Color = Color.Transparent;
            textAnnotation.Opacity = 0.8f;
            //添加注释到页面
            page.AnnotationsWidget.Add(textAnnotation);

            //保存并打开文档
            doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
         }
    }
}

 

 添加效果:

 


 

3. 链接式注释(Link Annotation

 

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

namespace FreeTextAnnotation_pdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument类对象,加载测试文档
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            PdfPageBase page = doc.Pages[0];
            //初始化RectangleF类,指定注释添加的位置、注释图标大小
            RectangleF rect = new RectangleF(50, 500, 100, 40);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

            //添加注释内容
            textAnnotation.Text = "This is just a sample, Click here to read the original file!";

            //设置注释属性,包括字体、字号、注释边框粗细、边框颜色、填充颜色等
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9);
            PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.White;
            textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
            textAnnotation.Color = Color.Transparent;

            //添加需要链接到的文件地址,并添加链接到注释
            string filePath = @"C:\Users\Administrator\Desktop\original.pdf";
            PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath);
            page.AnnotationsWidget.Add(link);

            //添加注释到页面
            page.AnnotationsWidget.Add(textAnnotation);

            //保存并打开文档
            doc.SaveToFile("LinkAnnotation.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("LinkAnnotation.pdf");
        }
    }
}

 

添加效果:

 


 

4. 多边形注释(Polygon Annotation

using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
using System.Drawing;

namespace PolygonAnnotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument类对象,加载测试文档
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("sample.pdf");
            //获取文档第一页
            PdfPageBase page = pdf.Pages[0];

            //实例化PdfPolygonAnnotation类,指定多边形各顶点位置
            PdfPolygonAnnotation polygon = new PdfPolygonAnnotation(page, new PointF[] { new PointF(0, 30), new PointF(30, 15), new PointF(60, 30),
    new PointF(45, 50), new PointF(15, 50), new PointF(0, 30)});  

            //指定多边形注释的边框颜色、注释内容、作者、边框类型、修订时间等属性
            polygon.Color = Color.CornflowerBlue;
            polygon.Text = "This article is created by Mia, permit read ONLY.";
            polygon.Author = "Editor's Note";
            polygon.Subject = "polygon annotation demo";
            polygon.BorderEffect = PdfBorderEffect.BigCloud;
            polygon.ModifiedDate = DateTime.Now;

            //添加注释到页面
            page.AnnotationsWidget.Add(polygon);
            //保存并打开文档
            pdf.SaveToFile("Polygon_Annotation.pdf");
            System.Diagnostics.Process.Start("Polygon_Annotation.pdf");
        }
    }
}

 

添加效果:



 

5. 线性注释(Line Annotation

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace LineAnnotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化PdfDocument类,加载文档
            PdfDocument document = new PdfDocument();
            document.LoadFromFile("sample.pdf");
            PdfPageBase page = document.Pages[0];

            //在页面指定位置绘制Line类型注释,并添加注释的文本内容
            int[] linePoints = new int[] { 100,300, 180, 300 };
            PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text");

            //设置线条粗细、指向
            lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid;
            lineAnnotation.lineBorder.BorderWidth = 1;
            lineAnnotation.LineIntent = PdfLineIntent.LineDimension;

            //设置线性注释的头、尾形状、标记的类型
            lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Circle;
            lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond;
            lineAnnotation.Flags = PdfAnnotationFlags.Default;

            //设置注释颜色
            lineAnnotation.InnerLineColor = new PdfRGBColor(Color.Green);
            lineAnnotation.BackColor = new PdfRGBColor(Color.Green);

            lineAnnotation.LeaderLineExt = 0;
            lineAnnotation.LeaderLine = 0;

            //添加注释到页面
            page.AnnotationsWidget.Add(lineAnnotation);

            //保存并打开文档
            document.SaveToFile("LineAnnotation.pdf");
            System.Diagnostics.Process.Start("LineAnnotation.pdf");

        }
    }
}

 

效果图:


 

 (本文完)

 

  • 大小: 15.5 KB
  • 大小: 336.9 KB
  • 大小: 169.5 KB
  • 大小: 157.9 KB
  • 大小: 228.1 KB
  • 大小: 83.1 KB
0
0
分享到:
评论

相关推荐

    使用C#给PDF文档添加注释的实现代码

    整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格...

    C#制作PDF文件(附Demo源码)

    用C#制作PDF文件全攻略 目 录 前 言 3 第一部分 iText的简单应用 4 第一章 创建一个Document 4 第一步 创建一个Document实例: 5 第二步 创建Writer实例 6 第三步 打开Document 6 第四步 添加内容 10 第五步,关闭 ...

    C# 中操作API,清晰PDF

    接着添加下面的代码来声明一个 API : [DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type); 此处 DllImport 属性被用来从不可控代码中调用一方法。” User32.dll...

    C#添加、获取、删除PDF附件实例代码

    在PDF文档中,我们可以添加同类型的或其他类型的文档作为附件内容,而PDF中附件也可以分为两种存在形式,一种是附件以普通文件形式存在,另一种是以注释的形式存在。在下面的示例中介绍了如何分别添加以上两种形式的...

    C#pdf制作,itextsharp文件外加在网上下的一个制作表格的案例

    itextsharp文件外加在网上下的一个制作表格的案例 第一部分 iText的简单应用 第一章 创建一个Document 第一步 创建一个Document实例: 第二步 创建Writer实例 第三步 打开Document 第四步 添加内容 第五步,...

    asp·net生成PDF详解

    用C#制作PDF文件全攻略 丽水市汽车运输集团有限公司信息中心 苟安廷 目 录 前 言 3 第一部分 iText的简单应用 4 第一章 创建一个Document 4 第一步 创建一个Document实例: 5 第二步 创建Writer实例 6 第三步 打开...

    Programming C#(第4版)(中文版)part1

    第5章 继承与多态 104 具体化与一般化 104 继承 107 多态 107 抽象类 115 万类之根:Object 119 类型的装箱和拆箱 121 嵌套类 124 第6章 操作符重载 127 使用operator关键字 127 支持其他.NET语言 128 创建有用的...

    C#代码实现PDF文档操作类

    本篇文章给大家介绍使用pdf文档操作C#代码,本文代码非常简单,代码附有注释,需要注意的是:需要添加itextsharp.dll引用才可以正常通过编译,感兴趣的朋友可以参考下

Global site tag (gtag.js) - Google Analytics