`

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# Winfrom PDF 在线预览控件

    在实际应用中,这样的工具可以使开发者轻松地在C# Winform界面中集成PDF处理功能,提升用户体验。例如,用户可以查看PDF文档的每一页,放大缩小,以及进行划线、高亮、添加文本等注释操作。 为了在C# Winform应用中...

    C# 在PDF中创建和填充域

    C# 在PDF中创建和填充域 本文主要介绍了C# 在PDF中创建和填充域的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧。 1. 创建和填充域的需求 众所周知,PDF文档通常是不能编辑和修改的。如果用户需要在...

    纯C#代码显示PDF文档

    5. **图片缓存**:由于PDF页面可能较多,直接在内存中保存所有图片可能导致资源消耗过大。因此,通常会使用缓存机制。可以创建一个字典来存储已转换的图片,键为页面索引,值为对应的Bitmap对象。这样,只有在需要时...

    C#中PDF文件转WORD文件(完整版)

    在C#编程环境中,将PDF文件转换为Word文档是一项常见的任务,这可能涉及到文本提取、格式保留以及图像处理等多个方面。下面将详细讲解如何在C#中实现这一过程,包括必要的库、步骤以及可能遇到的问题。 1. **库的...

    PDF压缩c#版本

    它支持.NET Framework和.NET Core平台,涵盖了各种PDF处理场景,如创建、编辑、合并、分割、加密、解密PDF文档,以及添加水印、注释、表单等。在图片压缩方面,Spire.Pdf提供了高效的方法,可以调整图片质量,从而...

    C# 创建PDF文件

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用、Web应用以及游戏等领域。当涉及到创建PDF文件时,C#提供了一些库和工具来帮助开发者实现这一功能。在VS2008环境下,尽管相对较老,但仍可以...

    C# PDF文件操作组件

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用、Web应用以及移动应用时。PDF(Portable Document Format)文件格式是用于共享和查看文档的标准,它保持了原始文档的格式和布局。因此,C#中的...

    c# pdfiumviewer pdf viewer

    5. **事件处理**:控件还提供了一系列的事件,如`PageChanged`事件,当用户在PDF中切换页面时触发;`MouseClick`事件,可以监听用户的点击行为,实现自定义交互功能。 6. **性能优化**:PDFiumViewer在设计时考虑了...

    c#生成各种PDF文件

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows应用程序、Web应用程序以及桌面应用程序时。PDF(Portable Document Format)文件格式则是通用的文档共享标准,它允许用户在不同的设备和操作系统上查看...

    c# PDF拆分合并工具源码

    标题中的“C# PDF拆分合并工具源码”指的是一个基于C#编程语言开发的软件,主要用于处理PDF文档,能够实现PDF文件的合并与拆分功能。这种工具在日常工作中非常实用,尤其对于需要处理大量PDF文档的用户,能够极大地...

    C#-PDF阅读器-源码

    【C# PDF阅读器源码】是一个用于在C#环境下实现PDF文档查看功能的项目,主要面向学习者和开发者,提供了代码参考和学习借鉴的价值。这个项目可能包含了多个类和文件,用于处理PDF的解析、渲染和交互。下面将详细讨论...

    C# 完美展示Pdf的程序

    Foxit的OCX控件为C#开发者提供了一种在应用程序中嵌入PDF阅读器的方式,使得用户可以直接在应用内查看和操作PDF文档,而无需依赖外部的PDF阅读软件。 接下来,我们探讨如何在C#项目中集成和使用这个控件。首先,你...

    Spire.Pdf无水印 解决pdf转化图片中文乱码的问题

    在C#环境中,`Spire.Pdf`库提供了一套完整的API,覆盖了PDF的各个操作,如添加文本、图像、表格、链接、注释等。此外,还支持PDF加密、解密、合并、分割、水印添加等功能,是.NET开发者处理PDF文档的强大工具。 ...

    C#6.0 语言规范中文版.pdf

    转换:允许将值从一种类型转换为另一种类型,可以是隐式的,也可以是显式的。 表达式:是计算的语句,它由一个或多个操作数和运算符组成,可以产生一个值。 语句:是程序的构成单元,用于控制程序的执行流程。 ...

    C#教程.pdf

    - 本章介绍了C#中不同类型之间的转换方法,包括隐式转换和显式转换。 **第七章:表达式** - **7.1 操作符** - **算术操作符**:+、-、*、/、%等。 - **赋值操作符**:=、+=、-=、*=、/=等。 - **关系操作符**...

    c#实现PDF阅读器功能,能够正常运行阅读PDF文件

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发桌面应用程序时,它以其高效、易用和强大的库支持而受到青睐。PDF(Portable Document Format)阅读器是用于查看、打印和交互式处理PDF文件的应用程序。本项目是...

    C#导出PDF , iTextSharp

    在IT行业中,PDF(Portable Document Format)是一种广泛用于文档共享的标准格式,因为它能保持文档的原始格式和布局,无论在哪个设备上查看。C#作为.NET框架下的主要编程语言,经常被用来创建和处理PDF文件。...

    c#系列教程(pdf)

    - C#是一种面向对象的编程语言,由微软设计并在.NET框架中发挥核心作用。 - 它结合了C++的性能和Java的易用性,同时引入了许多现代编程语言的特性,如垃圾回收、强类型检查和XML支持。 - **C#语言的特点** - C#...

    用C#实现生成PDF文档(原码)

    这个示例代码包括了创建PDF文档、添加页面、添加内容和保存PDF文档等步骤,并提供了详细的注释来解释每个步骤的实现细节。 本文为读者提供了一个完整的指南,指导读者如何使用C#语言生成PDF文档,並且提供了详细的...

Global site tag (gtag.js) - Google Analytics