国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用C#給PDF文檔添加注釋的實現代碼

2019-10-29 21:15:14
字體:
來源:轉載
供稿:網友

整理文檔時,我們可能會需要在一些或一段文字上添加注釋加以說明,那如何以編程的方式實現呢?本文將實例講述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");

這是添加注釋后的效果圖:

C#,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");

這是添加自由文本注釋的效果圖:

C#,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添加注釋,也許對你有幫助。謝謝瀏覽!

 

注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋江市| 浦县| 南漳县| 抚远县| 肥东县| 泽库县| 娄底市| 勃利县| 铜川市| 临沂市| 乌拉特前旗| 定日县| 锡林浩特市| 台中市| 保定市| 汾西县| 兰西县| 原平市| 阿瓦提县| 红河县| 沧州市| 兴仁县| 东海县| 白银市| 延津县| 五河县| 平塘县| 聂荣县| 沙洋县| 渝北区| 巴楚县| 西吉县| 杭锦后旗| 育儿| 平山县| 中宁县| 广灵县| 乐清市| 平原县| 怀集县| 麦盖提县|