C# 添加PDF註釋(5種類型)

【前言】

向文檔添加註釋,是一種比較常用的向讀者傳遞某些重要信息的手段。通過編程的方式來添加PDF註釋,我們可以自定義註釋的外觀、類型及其他一些個性化的設置,這種可供選擇的操作在編程中提供了更多的實用性。因此,本篇文章將介紹添加幾種不同類型的PDF註釋的方法。下面的示例中,藉助控件總結了一些不同類型的註釋的具體操作,主要包含以下幾種

  • 添加彈出式註釋(Popup Annotation)
  • 添加自由文本註釋(Free Text Annotation)
  • 添加鏈接式註釋(Link Annotation)
  • 添加多邊形註釋(Polygon Annotation)
  • 添加線性註釋(Line Annotation)

【工具使用】


【代碼操作】

1.彈出式註釋(Popup Annotation)

C#

using Spire.Pdf;
using Spire.Pdf.General.Find;
using System.Drawing;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;

namespace Annotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化PdfDocument類實例,並加載測試文檔            
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            //獲取第一頁
            PdfPageBase page = doc.Pages[0];

            //調用方法FindText()查找需要添加註釋的字符串
            PdfTextFind[] results = page.FindText("IPCC").Finds;

            //指定註釋添加的位置
            float x = results[0].Position.X - doc.PageSettings.Margins.Top;
            float y = results[0].Position.Y - doc.PageSettings.Margins.Left + results[0].Size.Height - 23;

            //創建彈出式註釋
            RectangleF rect = new RectangleF(x, y, 10, 0);
            PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect); 

            //添加註釋內容,並設置註釋的圖標類型和顏色 
            popupAnnotation.Text = "IPCC,This is a scientific and intergovernmental body under the auspices of the United Nations.";
            popupAnnotation.Icon = PdfPopupIcon.Help; 
            popupAnnotation.Color = Color.DarkOliveGreen;

            //添加註釋到文件
            page.AnnotationsWidget.Add(popupAnnotation);

            //保存並打開文檔
            doc.SaveToFile("Annotation.pdf");
            System.Diagnostics.Process.Start("Annotation.pdf");
        }
    }
}

在選擇註釋標籤類型時,有以下幾種類型可供選擇
C# 添加PDF註釋(5種類型)
註釋添加效果:
C# 添加PDF註釋(5種類型)

2. 自由文本註釋(Free Text Annotation)

C#

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace FreeTextAnnotation_pdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建PdfDocument類對象,加載測試文檔
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            PdfPageBase page = doc.Pages[0];
            //初始化RectangleF類,指定註釋添加的位置、註釋圖標大小
            RectangleF rect = new RectangleF(50, 500, 100, 40);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

            //添加註釋內容
            textAnnotation.Text = "This is just a sample, please refer the original article to see more!";

            //設置註釋屬性,包括字體、字號、註釋邊框粗細、邊框顏色、填充顏色等
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9);
            PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.White;
            textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
            textAnnotation.Color = Color.Transparent;
            textAnnotation.Opacity = 0.8f;
            //添加註釋到頁面
            page.AnnotationsWidget.Add(textAnnotation);

            //保存並打開文檔
            doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
         }
    }
}

添加效果
C# 添加PDF註釋(5種類型)

3. 鏈接式註釋(Link Annotation)

C#

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace FreeTextAnnotation_pdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建PdfDocument類對象,加載測試文檔
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            PdfPageBase page = doc.Pages[0];
            //初始化RectangleF類,指定註釋添加的位置、註釋圖標大小
            RectangleF rect = new RectangleF(50, 500, 100, 40);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

            //添加註釋內容
            textAnnotation.Text = "This is just a sample, Click here to read the original file!";

            //設置註釋屬性,包括字體、字號、註釋邊框粗細、邊框顏色、填充顏色等
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9);
            PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.White;
            textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
            textAnnotation.Color = Color.Transparent;

            //添加需要鏈接到的文件地址,並添加鏈接到註釋
            string filePath = @"C:\Users\Administrator\Desktop\original.pdf";
            PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath);
            page.AnnotationsWidget.Add(link);

            //添加註釋到頁面
            page.AnnotationsWidget.Add(textAnnotation);

            //保存並打開文檔
            doc.SaveToFile("LinkAnnotation.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("LinkAnnotation.pdf");
        }
    }
}

添加效果:
C# 添加PDF註釋(5種類型)

4. 多邊形註釋(Polygon Annotation)

C#

using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
using System.Drawing;

namespace PolygonAnnotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建PdfDocument類對象,加載測試文檔
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("sample.pdf");
            //獲取文檔第一頁
            PdfPageBase page = pdf.Pages[0];

            //實例化PdfPolygonAnnotation類,指定多邊形各頂點位置
            PdfPolygonAnnotation polygon = new PdfPolygonAnnotation(page, new PointF[] { new PointF(0, 30), new PointF(30, 15), new PointF(60, 30),
    new PointF(45, 50), new PointF(15, 50), new PointF(0, 30)});  

            //指定多邊形註釋的邊框顏色、註釋內容、作者、邊框類型、修訂時間等屬性
            polygon.Color = Color.CornflowerBlue;
            polygon.Text = "This article is created by Mia, permit read ONLY.";
            polygon.Author = "Editor's Note";
            polygon.Subject = "polygon annotation demo";
            polygon.BorderEffect = PdfBorderEffect.BigCloud;
            polygon.ModifiedDate = DateTime.Now;

            //添加註釋到頁面
            page.AnnotationsWidget.Add(polygon);
            //保存並打開文檔
            pdf.SaveToFile("Polygon_Annotation.pdf");
            System.Diagnostics.Process.Start("Polygon_Annotation.pdf");
        }
    }
}

添加效果:
C# 添加PDF註釋(5種類型)

5. 線性註釋(Line Annotation)

C#

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace LineAnnotation_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化PdfDocument類,加載文檔
            PdfDocument document = new PdfDocument();
            document.LoadFromFile("sample.pdf");
            PdfPageBase page = document.Pages[0];

            //在頁面指定位置繪製Line類型註釋,並添加註釋的文本內容
            int[] linePoints = new int[] { 100,300, 180, 300 };
            PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text");

            //設置線條粗細、指向
            lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid;
            lineAnnotation.lineBorder.BorderWidth = 1;
            lineAnnotation.LineIntent = PdfLineIntent.LineDimension;

            //設置線性註釋的頭、尾形狀、flag類型
            lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Circle;
            lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond;
            lineAnnotation.Flags = PdfAnnotationFlags.Default;

            //設置註釋顏色
            lineAnnotation.InnerLineColor = new PdfRGBColor(Color.Green);
            lineAnnotation.BackColor = new PdfRGBColor(Color.Green);

            lineAnnotation.LeaderLineExt = 0;
            lineAnnotation.LeaderLine = 0;

            //添加註釋到頁面
            page.AnnotationsWidget.Add(lineAnnotation);

            //保存並打開文檔
            document.SaveToFile("LineAnnotation.pdf");
            System.Diagnostics.Process.Start("LineAnnotation.pdf");

        }
    }
}

添加效果:
C# 添加PDF註釋(5種類型)
以上爲全部內容。如需轉載,請註明出處!
感謝閱讀。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章