您的位置:首页 > 编程语言 > C#

C#如何给PDF文档添加注释

2017-11-21 00:00 531 查看
摘要: PDF类型的文档是比较常用的一种的文本格式,应用广泛。在做文档时,可以对文档添加注释来阐明文档中某些内容的意义,或者向读者传达必要的信息。在对PDF添加注释的方法中,可以通过C#代码语言来添加。这里提供了一个添加注释的方法。

使用C#语言添加PDF注释当中,需要使用到Spire.PDF for .NET。可以在官网上下载免费版(https://www.e-iceblue.cn/Downloads/Free-Spire-PDF-NET.html)。

注意,需要添加dll文件到程序中,并且也要添加using 指令

1.给文本添加注释

主要代码如下:

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");

效果示例:



2.添加自由文本注释

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");

效果示例:



本文为转载文章,详细信息可以查看原文。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: