文档里加盖印章可以有效地声明文档的权威性及法律效应。在处理文档时,对于需要加盖印章的需求,本文示例中将介绍两种印章的添加方法,一种是印章以图片形式添加到PDF文档,一种是动态生成印章并添加到PDF文档。
使用工具:Spire.PDF for .NET
注意:在编程当中注意引用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");
}
}
}调试运行程序,生成文档。
2. 添加动态图章
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
namespace PDF动态图章
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//加载现有PDF文档
doc.LoadFromFile("sample.pdf");
//获取要添加动态印章的页面
PdfPageBase page = doc.Pages[1];
//创建模板对象
PdfTemplate template = new PdfTemplate(120, 60);
//创建字体
PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.SinoTypeSongLight, 16f, PdfFontStyle.Bold | PdfFontStyle.Italic);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", 10f), true);
//创建单色画刷和渐变画刷
PdfSolidBrush brush = new PdfSolidBrush(Color.Red);
RectangleF rect = new RectangleF(new PointF(0, 0), template.Size);
PdfLinearGradientBrush gradientBrush = new PdfLinearGradientBrush(rect, Color.Orange, Color.White, PdfLinearGradientMode.Horizontal);
//创建圆角矩形路径
int CornerRadius = 10;
PdfPath path = new PdfPath();
path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90);
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90);
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2);
//在模板上画圆角矩形路径,并用渐变色填充
template.Graphics.DrawPath(gradientBrush, path);
//在模板上画圆角矩形路径,并用红色填充路径
template.Graphics.DrawPath(PdfPens.Red, path);
//在模板上绘制印章文字、系统用户名、日期
String s1 = "已审阅\n";
String s2 = System.Environment.UserName + "行政处 \n" + DateTime.Now.ToString("F");
template.Graphics.DrawString(s1, font1, brush, new PointF(5, 5));
template.Graphics.DrawString(s2, font2, brush, new PointF(2, 28));
//创建PdfRubberStampAnnotation对象,并指定其位置和大小
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(page.ActualSize.Width - 300, 380), template.Size));
//创建PdfApperance对象,并将模板应用为一般状态
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template;
//在印章上应用PdfApperance对象(即样式)
stamp.Appearance = apprearance;
//将印章添加到PdfAnnotation集合
page.AnnotationsWidget.Add(stamp);
//保存文档
doc.SaveToFile("output.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("output.pdf");
}
}
}完成代码后,调试运行程序,生成文档。如下图所示:
(本文完)
关于添加动态图章详细操作步骤,可查看视频教程
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦

