好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用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#里面给ppt添加注释 ,也许对你有帮助。谢谢浏览!

dy("nrwz");

查看更多关于使用C#给PDF文档添加注释的实现代码的详细内容...

  阅读:51次