如何使用C#在PDF文件添加图片印章
作者:mdxy-dxy 时间:2021-06-26 05:34:51
文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性。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,图片印章
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java matches类,Pattern类及matcher类用法示例
2022-02-21 22:39:52
Android编程常用技巧实例总结
2022-11-30 20:27:44
java实现FTP文件上传与文件下载
2023-08-16 08:28:38
springboot2.0如何通过fastdfs实现文件分布式上传
2022-03-20 16:49:24
![](https://img.aspxhome.com/file/2023/7/71917_0s.png)
java中Statement 与 PreparedStatement接口之间的关系和区别
2023-11-25 03:11:11
![](https://img.aspxhome.com/file/2023/9/60029_0s.png)
DUBBO 日志过滤器,输出dubbo 接口调用入参、出参等信息(最新推荐)
2023-10-19 22:53:54
![](https://img.aspxhome.com/file/2023/8/104208_0s.png)
Spring interceptor拦截器配置及用法解析
2023-06-26 06:08:15
Java shiro安全框架使用介绍
2023-07-09 05:24:35
![](https://img.aspxhome.com/file/2023/3/69653_0s.png)
Java使用easyExcel导出excel数据案例
2022-02-21 19:39:27
![](https://img.aspxhome.com/file/2023/0/62330_0s.png)
猜数游戏java实现代码
2023-01-05 18:39:28
![](https://img.aspxhome.com/file/2023/6/96036_0s.jpg)
SpringBoot文件上传功能的实现方法
2021-11-23 11:24:27
![](https://img.aspxhome.com/file/2023/2/63632_0s.png)
RegexOptions.IgnoreCase正则表达式替换,忽略大小写
2022-05-03 17:25:45
教你如何使用Java实现WebSocket
2021-08-22 20:39:02
![](https://img.aspxhome.com/file/2023/2/94372_0s.png)
C#获取路径的几种方式实例分析
2023-07-18 22:19:16
spring拓展之如何定义自己的namespace
2022-01-12 05:13:33
![](https://img.aspxhome.com/file/2023/3/79873_0s.png)
spring cloud gateway跨域全局CORS配置方式
2021-11-02 01:09:00
android自定义view之模拟qq消息拖拽删除效果
2023-01-29 11:48:34
![](https://img.aspxhome.com/file/2023/4/95014_0s.gif)
Java实现蓝桥杯G将军的示例代码
2022-05-01 08:44:58
java读写oracle的blob字段示例
2023-12-22 16:19:00
SpringBoot整合Shiro两种方式(总结)
2021-09-08 21:28:24
![](https://img.aspxhome.com/file/2023/6/65146_0s.png)