使用C#給PDF文檔添加注釋的實(shí)現(xiàn)代碼
整理文檔時(shí),我們可能會(huì)需要在一些或一段文字上添加注釋加以說(shuō)明,那如何以編程的方式實(shí)現(xiàn)呢?本文將實(shí)例講述C#中如何使用免費(fèi)組件給PDF文檔添加文本注釋,包括自由文本注釋。自由文本注釋能允許我們自定義它的風(fēng)格和外觀,非常具有實(shí)用價(jià)值。
首先,下載這個(gè)免費(fèi)版組件Free Spire.PDF。組件下載安裝后,Visual Studio創(chuàng)建C#控制臺(tái)項(xiàng)目,添加bin文件夾的.DLL作為引用以及以下命名空間:
using System; using System.Drawing; using System.Windows.Forms; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Annotations;
現(xiàn)在我們就來(lái)具體看看如何給新建的文檔添加注釋的。
步驟1:新建一個(gè)PDF文檔對(duì)象,再添加一個(gè)新頁(yè)面。
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步驟2:文檔中添加文本,并設(shè)置文本的位置、字體大小、顏色。
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:給文本添加注釋,并設(shè)置注釋的邊框、顏色及位置。
PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理員", "一般來(lái)說(shuō),這是每一種計(jì)算機(jī)編程語(yǔ)言中最基本、最簡(jiǎn)單的程序", 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:將注釋添加到頁(yè)面,最后保存文檔。
(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("管理員", "一般來(lái)說(shuō),這是每一種計(jì)算機(jī)編程語(yǔ)言中最基本、最簡(jiǎn)單的程序", 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");
添加自由文本注釋
同樣,給文檔添加自由文本注釋也相對(duì)簡(jiǎn)單。
步驟1:新建一個(gè)PDF文檔對(duì)象,并添加一個(gè)新頁(yè)面。
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步驟2:初始化一個(gè)PdfFreeTextAnnotation,然后自定義注釋的文本。
RectangleF rect = new RectangleF(0, 40, 150, 50); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); textAnnotation.Text = "Free text annotation ";
步驟3:設(shè)置注釋的屬性,包括字體、填充顏色、邊框顏色和透明度。
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:添加注釋到頁(yè)面。
page.AnnotationsWidget.Add(textAnnotation);
步驟5:保存并重新打開(kāi)文檔。
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");
之前我也分享過(guò)如何在C#里面給PPT添加注釋,也許對(duì)你有幫助。謝謝瀏覽!
相關(guān)文章
c#動(dòng)態(tài)類型,及動(dòng)態(tài)對(duì)象的創(chuàng)建,合并2個(gè)對(duì)象,map實(shí)例
下面小編就為大家?guī)?lái)一篇c#動(dòng)態(tài)類型,及動(dòng)態(tài)對(duì)象的創(chuàng)建,合并2個(gè)對(duì)象,map實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法
這篇文章主要介紹了C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法,涉及C#操作字符串的正則匹配與截取等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03c#自定義Attribute獲取接口實(shí)現(xiàn)示例代碼
這篇文章主要給大家介紹了關(guān)于c#自定義Attribute獲取接口實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Unity實(shí)現(xiàn)蘋果手機(jī)Taptic震動(dòng)
這篇文章主要介紹了Unity實(shí)現(xiàn)蘋果手機(jī)Taptic震動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10C# WinForm窗體編程中處理數(shù)字的正確操作方法
這篇文章主要介紹了C# WinForm窗體編程中處理數(shù)字的正確操作方法,本文給出了正確示例,并解釋了為什么要這么做,需要的朋友可以參考下2014-08-08C#獲取時(shí)間戳的方法及時(shí)間戳轉(zhuǎn)換問(wèn)題
本文主要介紹了C#獲取時(shí)間戳的方法及時(shí)間戳轉(zhuǎn)換問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02c#入門之循環(huán)語(yǔ)句使用詳解(for循環(huán)、do/while)
這篇文章主要介紹了c#入門之循環(huán)語(yǔ)句使用詳解,有for循環(huán)和do/while的示例,需要的朋友可以參考下2014-04-04C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法
下面小編就為大家?guī)?lái)一篇C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01