您好,登錄后才能下訂單哦!
向文檔添加注釋,是一種比較常用的向讀者傳遞某些重要信息的手段。通過編程的方式來添加PDF注釋,我們可以自定義注釋的外觀、類型及其他一些個性化的設(shè)置,這種可供選擇的操作在編程中提供了更多的實用性。因此,本篇文章將介紹添加幾種不同類型的PDF注釋的方法。下面的示例中,借助控件總結(jié)了一些不同類型的注釋的具體操作,主要包含以下幾種
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];
//調(diào)用方法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;
//創(chuàng)建彈出式注釋
RectangleF rect = new RectangleF(x, y, 10, 0);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect);
//添加注釋內(nèi)容,并設(shè)置注釋的圖標類型和顏色
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#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace FreeTextAnnotation_pdf
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建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);
//添加注釋內(nèi)容
textAnnotation.Text = "This is just a sample, please refer the original article to see more!";
//設(shè)置注釋屬性,包括字體、字號、注釋邊框粗細、邊框顏色、填充顏色等
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#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace FreeTextAnnotation_pdf
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建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);
//添加注釋內(nèi)容
textAnnotation.Text = "This is just a sample, Click here to read the original file!";
//設(shè)置注釋屬性,包括字體、字號、注釋邊框粗細、邊框顏色、填充顏色等
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#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
using System.Drawing;
namespace PolygonAnnotation_PDF
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建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)});
//指定多邊形注釋的邊框顏色、注釋內(nèi)容、作者、邊框類型、修訂時間等屬性
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#
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類型注釋,并添加注釋的文本內(nèi)容
int[] linePoints = new int[] { 100,300, 180, 300 };
PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text");
//設(shè)置線條粗細、指向
lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid;
lineAnnotation.lineBorder.BorderWidth = 1;
lineAnnotation.LineIntent = PdfLineIntent.LineDimension;
//設(shè)置線性注釋的頭、尾形狀、flag類型
lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Circle;
lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond;
lineAnnotation.Flags = PdfAnnotationFlags.Default;
//設(shè)置注釋顏色
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");
}
}
}
添加效果:
以上為全部內(nèi)容。如需轉(zhuǎn)載,請注明出處!
感謝閱讀。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。