C# 添加PDF印章

文檔里加蓋印章可以有效地聲明文檔的權威性,特別是對於一些具有行政性質、法律性質或者是內部保密性質的文檔,添加印章就顯得尤爲重要了。因此,本文將介紹一種通過編程來實現PDF圖片印章添加的方法。
你需要使用的工具有:

* Free Spire.PDF for .NET 4.3 (社區版)

注意:在編程當中注意引用Spire.PDF.dll文件到項目,該dll文件可以在安裝文件下的Bin文件夾中獲取。
【C#】

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

namespace AddStamp_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建一個PdfDocument類對象,並加載PDF文檔
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");
            //獲取PDF文檔第一頁
            PdfPageBase page = doc.Pages[0];
            //新建一個PdfRubberStampAnnotation對象,指定其註釋的位置和大小
            PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(-5, -5), new SizeF(200, 200)));
            //實例化一個PdfAppearance對象,並加載作爲印章的圖片
            PdfAppearance loApprearance = new PdfAppearance(loStamp);
            PdfImage image = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\yz.jpg");
            //新建一個PDF模板,並在模板裏繪製圖片
            PdfTemplate template = new PdfTemplate(600, 800);
            template.Graphics.DrawImage(image, 0, 0);
            loApprearance.Normal = template;
            loStamp.Appearance = loApprearance;
            //添加印章到PDF文檔
            page.AnnotationsWidget.Add(loStamp);
            //保存並打開文檔
            string output = "ImageStamp.pdf";
            doc.SaveToFile(output);
            System.Diagnostics.Process.Start("ImageStamp.pdf");
        }
    }
}

調試運行程序,生成文檔(可在該程序項目文件夾bin>Debug中查看)
C# 添加PDF印章

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