使用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ì)你有幫助。謝謝瀏覽!
上一篇:C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例
欄 目:C#教程
下一篇:c# 以類名為參創(chuàng)建父類相同的類的實(shí)例代碼
本文標(biāo)題:使用C#給PDF文檔添加注釋的實(shí)現(xiàn)代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5959.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫(xiě)Windows事件日志的方法
- 01-10C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#獲取動(dòng)態(tài)生成的CheckBox值
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10C#中yield用法使用說(shuō)明


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載