整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述C#中如何使用免费组件给pdf文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值。
首先,下载这个免费版组件Free Spire.PDF。组件下载安装后,Visual Studio创建C#控制台项目,添加bin文件夹的.DLL作为引用以及以下命名空间:
using System; System.Drawing; System.windows.Forms; Spire.pdf; Spire.pdf.Graphics;using Spire.pdf.Annotations;
现在我们就来具体看看如何给新建的文档添加注释的。
步骤1:新建一个pdf文档对象,再添加一个新页面。
pdfdocument doc = new pdfdocument();pdfpageBase page = doc.Pages.Add();
步骤2:文档中添加文本,并设置文本的位置、字体大小、颜色。
pdf@R_502_6837@ @R_502_6837@ = new pdf@R_502_6837@(pdf@R_502_6837@Family.Helvetica,13);string text = "HelloWorld";PointF point = new PointF(200,1)">100);page.Canvas.DrawString(text,@R_502_6837@,pdfBrushes.Red,point);
步骤3:给文本添加注释,并设置注释的边框、颜色及位置。
pdfTextMarkupAnnotation annotation1 = new pdfTextMarkupAnnotation(管理员",1)">一般来说,这是每一种计算机编程语言中最基本、最简单的程序0,1)">0),@R_502_6837@);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");
这是添加注释后的效果图:
全部代码:
1 pdfdocument doc = pdfdocument(); 2 3 pdfpageBase page = doc.Pages.Add(); 4 5 pdf@R_502_6837@ @R_502_6837@ = ); 6 7 ; 8 9 PointF point = 10 11 page.Canvas.DrawString(text,point);12 13 14 15 pdfTextMarkupAnnotation annotation1 = 16 17 annotation1.border = 18 19 annotation1.TextMarkupcolor = color.Green;20 21 annotation1.Location = doc.PageSettings.margins.left);22 23 (page pdfNewPage).Annotations.Add(annotation1);24 25 doc.Savetofile(26 27 System.Diagnostics.Process.Start(");VIEw Code
添加自由文本注释
同样,给文档添加自由文本注释也相对简单。
步骤1:新建一个pdf文档对象,并添加一个新页面。
pdfdocument doc = pdfdocument();pdfpageBase page = doc.Pages.Add();
步骤2:初始化一个pdfFreeTextAnnotation,然后自定义注释的文本。
RectangleF rect = new RectangleF(40,1)">150,1)">50);pdfFreeTextAnnotation textAnnotation = pdfFreeTextAnnotation(rect);textAnnotation.Text = Free text annotation ";
步骤3:设置注释的属性,包括字体、填充颜色、边框颜色和透明度。
pdf@R_502_6837@ @R_502_6837@ = new pdf@R_502_6837@(pdf@R_502_6837@Family.TimesRoman,1)">10);pdfAnnotationborder border = pdfAnnotationborder(1f);textAnnotation.@R_502_6837@ = @R_502_6837@;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(");
这是添加自由文本注释的效果图:
全部代码:
5 7 RectangleF rect = 9 pdfFreeTextAnnotation textAnnotation = pdfFreeTextAnnotation(rect);11 textAnnotation.Text = 13 15 pdf@R_502_6837@ @R_502_6837@ = 17 pdfAnnotationborder border = pdfAnnotationborder(1f);19 textAnnotation.@R_502_6837@ = @R_502_6837@;21 textAnnotation.border = border;23 textAnnotation.bordercolor = color. Purple;25 textAnnotation.lineEndingStyle = pdflineEndingStyle.Circle;27 textAnnotation.color = color.Pink;28 29 textAnnotation.Opacity = 0.8f30 31 32 33 page.AnnotationsWidget.Add(textAnnotation);34 35 doc.Savetofile(36 37 System.Diagnostics.Process.Start(");VIEw Code
之前我也分享过如何在C#里面给PPT添加注释,也许对你有帮助。谢谢浏览!
总结
以上是内存溢出为你收集整理的C#如何给PDF文档添加注释全部内容,希望文章能够帮你解决C#如何给PDF文档添加注释所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)