文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性。c#添加图片印章其实也有很多实现方法,这里我使用的是免费的第三方软件free spire.pdf,向大家阐述如何以编程的方式在pdf文件中添加图片印章
文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性。c#添加图片印章其实也有很多实现方法,这里我使用的是免费的第三方软件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文档:
运行后的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#如何在pdf文件添加图片印章的详细介绍的详细内容。