首頁  >  文章  >  後端開發  >  使用C#為PDF文件新增註解的範例程式碼分享(圖)

使用C#為PDF文件新增註解的範例程式碼分享(圖)

黄舟
黄舟原創
2017-03-25 11:35:521511瀏覽

本文將實例敘述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");

這是新增註解後的效果圖:

#全部程式碼:

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

這是新增自由文字註解的效果圖:

#全部程式碼:

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#為PDF文件新增註解的範例程式碼分享(圖)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn