如何使用C#在PDF文件添加圖片印章
文檔中添加印章可以起一定的作用,比如,防止文件隨意被使用,或者確保文檔內(nèi)容的安全性和權(quán)威性。C#添加圖片印章其實(shí)也有很多實(shí)現(xiàn)方法,這里我使用的是免費(fèi)的第三方軟件Free Spire.PDF,向大家闡述如何以編程的方式在PDF文件中添加圖片印章。
具體步驟如下:
在此之前,我們需要添加dll文件作為引用。添加引用 → 瀏覽 → Spire.XLS folder → Bin → .NET 2.0/3.5/4.0/4.5/4.0 ClientProfile → Spire.XLS.dll.
第一步:首先新建一個(gè)PDF文檔對(duì)象并加載要添加印章的文檔。
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"E:\Visual Studio\Sample\template7\sample.pdf");
第二步:獲取文檔的第一頁(yè)。
PdfPageBase page = doc.Pages[0];
第三步:新建一個(gè)PdfRubberStampAnnotation對(duì)象,指定其注釋的范圍和大小。
PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(-5,-5), new SizeF(60, 60)));
第四步:實(shí)例化一個(gè)PdfAppearance對(duì)象。
PdfAppearance loApprearance = new PdfAppearance(loStamp);
第五步:加載用作印章的圖片。
PdfImage image = PdfImage.FromFile(@"C:\Users\Administrator\Pictures\sample.jpg");
第六步:新建一個(gè)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);
運(yùn)行前的pdf文檔:

運(yùn)行后的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);
}
}
}
通過(guò)此組件,我們除了可以快速地在PDF文件中添加圖片印章,還可以在PDF文件中添加圖片和文字水印以及添加圖片背景,可以參考一下,也許對(duì)你有幫助。謝謝瀏覽。
相關(guān)文章
一個(gè)C#開(kāi)發(fā)者重溫C++的心路歷程
作為一個(gè)C#開(kāi)發(fā)為什么要重新學(xué)習(xí)C++呢?因?yàn)樵贑#在很多業(yè)務(wù)場(chǎng)景需要調(diào)用一些C++編寫(xiě)的COM組件,如果不了解C++,那么,很容易。。。注定是要被C++同事忽悠的2019-05-05
C# 數(shù)獨(dú)求解算法的實(shí)現(xiàn)
這篇文章主要介紹了C# 數(shù)獨(dú)求解算法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
C#實(shí)現(xiàn)大數(shù)字運(yùn)算的實(shí)例代碼
這篇文章介紹了C#實(shí)現(xiàn)大數(shù)字運(yùn)算的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10
C# Fiddler插件實(shí)現(xiàn)網(wǎng)站離線(xiàn)瀏覽功能
本文主要介紹了C# Fiddler插件實(shí)現(xiàn)網(wǎng)站離線(xiàn)瀏覽功能的原理與方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
基于C#實(shí)現(xiàn)獲取本地磁盤(pán)目錄
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)獲取本地磁盤(pán)目錄的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取公共父節(jié)點(diǎn)的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取公共父節(jié)點(diǎn)的方法,需要的朋友可以參考下2014-08-08

