1. 首页 » 生活常识

脚注(C)添加方法指南

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何在PDF中添加该注释。

一、dll引用

步骤1:在Visual Studio中打开“解决方案资源管理器”- 鼠标右键点击“引用”-“管理NuGet包”。

C# 在PDF中添加墨迹注释Ink Annotation

步骤2:选择“浏览”-在搜索框中输入搜索内容,选择搜索结果,点击“安装”。

C# 在PDF中添加墨迹注释Ink Annotation

步骤3:依次点击“OK”-"接受",然后等待程序完成安装。

C# 在PDF中添加墨迹注释Ink Annotation

或者,通过官方渠道,下载包 Spire.PDF for .NET 到本地。解压后,将BIN文件夹下的Spire.Pdf.dll文件引用至VS程序。

二、代码示例

添加注释时,除了自定义各个点的位置及数量,也可以设置墨迹颜色、线条宽度、透明度、注释的内容、名称等。下面是代码实现的步骤:

创建PdfDocument类的对象,并通过PdfDocument.LoadFromFile(String fileName)方法加载PDF文档。通过PdfDocument.Pages[int Index]属性获取PDF指定页面。创建类型为int的对象集合,集合元素为各墨迹顶点。创建PdfInkAnnotation类的实例。并通过该类提供的属性设置墨迹颜色、宽度、注释内容等格式。调用PdfPageBase.AnnotationsWidget属性提供的PdfAnnotationCollection.Add(PdfAnnotation annotation)方法添加注释到PDF。最后,通过PdfDocument.SaveToFile(string filename, FileFormat fileFormat)方法保存PDF文档到指定路径。

C#

C# 在PDF中添加墨迹注释Ink Annotation

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

namespace InkAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF文档
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("test.pdf");

            //获取第一页
            PdfPageBase pdfPage = pdf.Pages[0];

            //设置墨迹坐标点位置
            List inkList = new List();           
            int[] intPoints = new int[]
            {
                370,700,
                120,720,
                110,760,
                220,800,
                270,790,
                350,770,
                350,670
            };
            inkList.Add(intPoints);
            //添加墨迹注释到PDF页面
            PdfInkAnnotation inkannotation = new PdfInkAnnotation(inkList);
            inkannotation.Color = Color.MediumVioletRed;
            inkannotation.Border.Width = 6;
            inkannotation.Opacity = 0.5f;
            inkannotation.Text = "This is an ink annotation. ";
            inkannotation.Name = "Manager";     
            pdfPage.AnnotationsWidget.Add(inkannotation);

            //保存文档
            Pdf.SaveToFile("AddInkAnnotation.pdf",FileFormat.PDF);
            System.Diagnostics.Process.Start("AddInkAnnotation.pdf");
        }
    }
}

C# 在PDF中添加墨迹注释Ink Annotation

注释效果:

C# 在PDF中添加墨迹注释Ink Annotation

—End—

声明:本文由"麦兜"发布,不代表"破站"立场,转载联系作者并注明出处:https://www.848586.cn/289351.html