使用C#给PDF文档添加注释的示例代码分享(图)

黄舟
发布: 2017-03-25 11:35:52
原创
1528 人浏览过

本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值

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

首先,下载这个免费版组件Free Spire.PDF。组件下载安装后,Visual Studio创建C#控制台项目,添加bin文件夹的.DLL作为引用以及以下命名空间:

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;
登录后复制

现在我们就来具体看看如何给新建的文档添加注释的。

步骤1:新建一个PDF文档对象,再添加一个新页面。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
登录后复制
登录后复制

步骤2:文档中添加文本,并设置文本的位置、字体大小、颜色。

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
string text = "HelloWorld";
PointF point = new PointF(200, 100);
page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
登录后复制

步骤3:给文本添加注释,并设置注释的边框、颜色及位置。

PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font);
annotation1.Border = new PdfAnnotationBorder(0.75f);
annotation1.TextMarkupColor = Color.Green;
annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
登录后复制

步骤4:将注释添加到页面,最后保存文档。

(page as PdfNewPage).Annotations.Add(annotation1);
doc.SaveToFile("result.pdf");
登录后复制

这是添加注释后的效果图:

全部代码:

PdfDocument doc = new PdfDocument();
      PdfPageBase page = doc.Pages.Add();
      PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
      string text = "HelloWorld";
      PointF point = new PointF(200, 100);
      page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
 
      PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font);
      annotation1.Border = new PdfAnnotationBorder(0.75f);
      annotation1.TextMarkupColor = Color.Green;
      annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
      (page as PdfNewPage).Annotations.Add(annotation1);
      doc.SaveToFile("result.pdf");
      System.Diagnostics.Process.Start("result.pdf");
登录后复制

添加自由文本注释

同样,给文档添加自由文本注释也相对简单。

步骤1:新建一个PDF文档对象,并添加一个新页面。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
登录后复制
登录后复制

步骤2:初始化一个PdfFreeTextAnnotation,然后自定义注释的文本。

RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
textAnnotation.Text = "Free text annotation ";
登录后复制

步骤3:设置注释的属性,包括字体、填充颜色、边框颜色和透明度。

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
textAnnotation.Font = font;
textAnnotation.Border = border;
textAnnotation.BorderColor = Color. Purple;
textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
textAnnotation.Color = Color. Pink;
textAnnotation.Opacity = 0.8f;
登录后复制

步骤4:添加注释到页面。

page.AnnotationsWidget.Add(textAnnotation);
登录后复制

步骤5:保存并重新打开文档。

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
登录后复制

这是添加自由文本注释的效果图:

全部代码:

PdfDocument doc = new PdfDocument();
      PdfPageBase page = doc.Pages.Add();
      
      RectangleF rect = new RectangleF(0, 40, 150, 50);
      PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
      textAnnotation.Text = "Free text annotation ";
    
      PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
      PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
      textAnnotation.Font = font;
      textAnnotation.Border = border;
      textAnnotation.BorderColor = Color. Purple;
      textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
      textAnnotation.Color = Color.Pink;
      textAnnotation.Opacity = 0.8f;
      
      page.AnnotationsWidget.Add(textAnnotation);
      doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
      System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
登录后复制

以上是使用C#给PDF文档添加注释的示例代码分享(图)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!