国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

如何使用C#在PDF文件添加圖片印章

2019-10-29 21:15:14
字體:
來源:轉載
供稿:網友

文檔中添加印章可以起一定的作用,比如,防止文件隨意被使用,或者確保文檔內容的安全性和權威性。C#添加圖片印章其實也有很多實現(xiàn)方法,這里我使用的是免費的第三方軟件Free Spire.PDF,向大家闡述如何以編程的方式在PDF文件中添加圖片印章。

具體步驟如下:

在此之前,我們需要添加dll文件作為引用。添加引用 → 瀏覽 → Spire.XLS folder → Bin → .NET 2.0/3.5/4.0/4.5/4.0 ClientProfile → Spire.XLS.dll.

第一步:首先新建一個PDF文檔對象并加載要添加印章的文檔。

PdfDocument doc = new PdfDocument();doc.LoadFromFile(@"E:/Visual Studio/Sample/template7/sample.pdf");

第二步:獲取文檔的第一頁。

PdfPageBase page = doc.Pages[0];

第三步:新建一個PdfRubberStampAnnotation對象,指定其注釋的范圍和大小。

PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(-5,-5), new SizeF(60, 60))); 

第四步:實例化一個PdfAppearance對象。  

PdfAppearance loApprearance = new PdfAppearance(loStamp);

第五步:加載用作印章的圖片。

PdfImage image = PdfImage.FromFile(@"C:/Users/Administrator/Pictures/sample.jpg");

第六步:新建一個PDF模板,并在模板里繪制圖片。

PdfTemplate template = new PdfTemplate(160, 160);template.Graphics.DrawImage(image, 0, 0);loApprearance.Normal = template;loStamp.Appearance = loApprearance;

第7步:在PDF文檔添加印章。

page.AnnotationsWidget.Add(loStamp);

第八步:保存文檔。

string output = "ImageStamp.pdf";doc.SaveToFile(output); 

運行前的pdf文檔:

C#,PDF,圖片印章

運行后的pdf文檔:

C#,PDF,圖片印章

全部代碼:

using System;using System.Drawing;using System.Windows.Forms;using Spire.Pdf;using Spire.Pdf.Annotations;using Spire.Pdf.Annotations.Appearance;using Spire.Pdf.Graphics;namespace addanimagestamptoaPDF_file{  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();    }    private void button1_Click(object sender, EventArgs e)    {      PdfDocument doc = new PdfDocument();      doc.LoadFromFile(@"E:/Visual Studio/Sample/template7/sample.pdf");       PdfPageBase page = doc.Pages[0];            PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(-5, -5), new SizeF(60, 60)));      PdfAppearance loApprearance = new PdfAppearance(loStamp);      PdfImage image = PdfImage.FromFile(@"C:/Users/Administrator/Pictures/sample.jpg");       PdfTemplate template = new PdfTemplate(160, 160);      template.Graphics.DrawImage(image, 0,0);      loApprearance.Normal = template;      loStamp.Appearance = loApprearance;              page.AnnotationsWidget.Add(loStamp);      string output = "ImageStamp.pdf";      doc.SaveToFile(output);      }  }}

通過此組件,我們除了可以快速地在PDF文件中添加圖片印章,還可以在PDF文件中添加圖片和文字水印以及添加圖片背景,可以參考一下,也許對你有幫助。謝謝瀏覽。

 

注:相關教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 贺州市| 常宁市| 宁夏| 陇川县| 额济纳旗| 沾化县| 马鞍山市| 正阳县| 普格县| 昌乐县| 沿河| 通许县| 江源县| 西平县| 靖远县| 荆门市| 潼关县| 馆陶县| 西林县| 通道| 万年县| 巴南区| 万盛区| 赣州市| 克山县| 望奎县| 南开区| 太白县| 五指山市| 德格县| 渝北区| 策勒县| 乐昌市| 壶关县| 普格县| 泸定县| 科尔| 陕西省| 茂名市| 云安县| 红安县|