天天看點

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

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

C#如何給PDF文檔添加注釋

全部代碼:

C#如何給PDF文檔添加注釋
C#如何給PDF文檔添加注釋
1             PdfDocument doc = new PdfDocument();
 2 
 3             PdfPageBase page = doc.Pages.Add();
 4 
 5             PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
 6 
 7             string text = "HelloWorld";
 8 
 9             PointF point = new PointF(200, 100);
10 
11             page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
12 
13  
14 
15             PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理者", "一般來說,這是每一種計算機程式設計語言中最基本、最簡單的程式", text, new PointF(0, 0), font);
16 
17             annotation1.Border = new PdfAnnotationBorder(0.75f);
18 
19             annotation1.TextMarkupColor = Color.Green;
20 
21             annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
22 
23             (page as PdfNewPage).Annotations.Add(annotation1);
24 
25             doc.SaveToFile("result.pdf");
26 
27             System.Diagnostics.Process.Start("result.pdf");      

View Code

添加自由文本注釋

同樣,給文檔添加自由文本注釋也相對簡單。

步驟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文檔添加注釋
C#如何給PDF文檔添加注釋
C#如何給PDF文檔添加注釋
1             PdfDocument doc = new PdfDocument();
 2 
 3             PdfPageBase page = doc.Pages.Add();
 4 
 5            
 6 
 7             RectangleF rect = new RectangleF(0, 40, 150, 50);
 8 
 9             PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
10 
11             textAnnotation.Text = "Free text annotation ";
12 
13         
14 
15             PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
16 
17             PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
18 
19             textAnnotation.Font = font;
20 
21             textAnnotation.Border = border;
22 
23             textAnnotation.BorderColor = Color. Purple;
24 
25             textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
26 
27             textAnnotation.Color = Color.Pink;
28 
29             textAnnotation.Opacity = 0.8f;
30 
31            
32 
33             page.AnnotationsWidget.Add(textAnnotation);
34 
35             doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
36 
37             System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");      

之前我也分享過如何在

C#裡面給PPT添加注釋

,也許對你有幫助。謝謝浏覽!