C# 在PDF中绘制动态图章
我们知道,动态图章,因图章中的时间、日期可以动态的生成,因而具有较强的时效性。在本篇文章中将介绍通过C#编程在PDF中绘制动态图章的方法,该方法可自动获取当前系统登录用户名、日期及时间信息并生成图章。
使用工具
注:下载安装后,注意在程序中添加引用Spire.PDF.dll(dll文件可在安装路径下的Bin文件夹中获取)

C#代码示例(供参考)
步骤 1 :添加using指令
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
步骤 2 :创建文档,加载测试文件
//创建PdfDocument对象
PdfDocument doc = new PdfDocument(); //加载现有PDF文档
doc.LoadFromFile("sample.pdf");
步骤 3 :获取需要添加动态图章的页面
PdfPageBase page = doc.Pages[];
步骤 4 :创建印章模板、字体、画刷等
//创建模板对象
PdfTemplate template = new PdfTemplate(, ); //创建字体
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(, ), template.Size);
PdfLinearGradientBrush gradientBrush = new PdfLinearGradientBrush(rect, Color.White, Color.White, PdfLinearGradientMode.Horizontal); //创建圆角矩形路径
int CornerRadius = ;
PdfPath path = new PdfPath();
path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, , );
path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / );
步骤 5 :应用模板
//在模板上画圆角矩形路径,并用渐变色填充
template.Graphics.DrawPath(gradientBrush, path);
//在模板上画圆角矩形路径,并用红色填充路径
template.Graphics.DrawPath(PdfPens.Red, path);
步骤 6 :绘制印章上的文字、用户名、当前日期时间等
String s1 = "已审阅\n";
String s2 = System.Environment.UserName + "行政处 \n" + DateTime.Now.ToString("F");
template.Graphics.DrawString(s1, font1, brush, new PointF(, ));
template.Graphics.DrawString(s2, font2, brush, new PointF(, ));
步骤 7 :添加印章到PDF页面指定位置
//创建PdfRubberStampAnnotation对象,并指定其位置和大小
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(page.ActualSize.Width - , ), template.Size)); //创建PdfApperance对象,并将模板应用为一般状态
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template; //在印章上应用PdfApperance对象(即样式)
stamp.Appearance = apprearance; //将印章添加到PdfAnnotation集合
page.AnnotationsWidget.Add(stamp);
步骤 8 :保存并打开文档
doc.SaveToFile("output.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("output.pdf");
完成以上步骤后,调试运行程序,生成文档。在生成的文档中,文末已添加了动态的图章,如下图所示:

全部代码:
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[]; //创建模板对象
PdfTemplate template = new PdfTemplate(, ); //创建字体
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(, ), template.Size);
PdfLinearGradientBrush gradientBrush = new PdfLinearGradientBrush(rect, Color.White, Color.White, PdfLinearGradientMode.Horizontal); //创建圆角矩形路径
int CornerRadius = ;
PdfPath path = new PdfPath();
path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, , );
path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, , );
path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / ); //在模板上画圆角矩形路径,并用渐变色填充
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(, ));
template.Graphics.DrawString(s2, font2, brush, new PointF(, )); //创建PdfRubberStampAnnotation对象,并指定其位置和大小
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(page.ActualSize.Width - , ), 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");
}
}
}
以上是本次关于C#在PDF文档中绘制动态图章的方法介绍,在前面的文章中介绍了添加印章的到PDF文档的方法,有需要也可以查阅该文档。
感谢阅读。
(本文完)
C# 在PDF中绘制动态图章的更多相关文章
- C# 如何在PDF中绘制不同风格类型的文本
通过对控件Spire.PDF的测试,我们可以创建PDF文件并向文档中绘制文本.图片.表格.图形等内容,其中,对于绘制文本这一部分,Spire.PDF提供了三种字体类型来绘制文本,即: Standard ...
- 【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图
原文 [译]在Asp.Net中操作PDF - iTextSharp - 绘制矢量图 在上一篇iTextSharp文章中讲述了如何将现有的图片插入PDF中并对其进行操作.但有时,你需要在PDF中绘制不依 ...
- Java 处理PDF图章(印章)——图片图章、动态图章
图章(印章)是一种在合同.票据.公文等文件中表明法律效应.部门机关权威的重要指示物,常见于各种格式的文件.文档中.对于纸质文档可以手动盖章,但对于电子文档,则需要通过特定的方法来实现.本篇文档分享通过 ...
- Java 在PDF文档中绘制图形
本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...
- (转)原始图像数据和PDF中的图像数据
比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...
- ZBrush中的动态网格该怎么进行运用
DynaMesh是ZBrush最新的基础模型创建工具,该命令用于基本模型的起稿到中模的制作.使用DynaMesh完全不启用考虑模型的拓扑,可以从一个图形拉扯出整个模型的分支,本文将以一个实例简单介绍Z ...
- java 如何在pdf中生成表格
1.目标 在pdf中生成一个可变表头的表格,并向其中填充数据.通过泛型动态的生成表头,通过反射动态获取实体类(我这里是User)的get方法动态获得数据,从而达到动态生成表格. 每天生成一个文件夹存储 ...
- PDF创建及动态转换控件程序包ActivePDF Portfolio
ActivePDF Portfolio是将4个activePDF最优秀的服务器产品捆绑成一个价格适中的控件程序包.它提供了开发一个完整的服务器端的PDF解决方案所需的一切. 具体功能: activeP ...
- Android 绘制动态图
最近准备技能大赛,需要将从传感器中读出的数据在移动客户端以图的形式绘制出来,因为平时很少绘图,于是各种查资料,算是勉强做出来了. 以下是大赛理论效果图(左)和实际效果图(右),真的是理想很丰满,现实很 ...
随机推荐
- [LeetCode] Advantage Shuffle 优势洗牌
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- 什么是URL,URL格式
什么是URL: 互联网里有很多的网页,大家都需要能够互相访问,就比如在一栋大楼里,有很多的房间,不同房间里的人都想能去任意的其他房间里去,那怎么能够达到这样的想法呢? 很简单,每个房间都挂一个门牌号就 ...
- python从入门到实践-10章文件和异常(括号问题)
#!/user/bin/env python# -*- coding:utf-8 -*- # 1.从文件中读取数据with open('pi_digits.txt') as file_object: ...
- JavaWeb学习路线
一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...
- Python-常用字符串操作
name = 'shanbaoliang.exe' print(name.capitalize()) #将字符串首字母大写 print(name.center(50,'-')) #把字符串居中,并用特 ...
- [转]Setting Keystone v3 domains
http://www.florentflament.com/blog/setting-keystone-v3-domains.html The Openstack Identity v3 API, p ...
- ReactJs和React Native的联系和差异
1,React Js的目的 是为了使前端的V层更具组件化,能更好的复用,它能够使用简单的html标签创建更多的自定义组件标签,内部绑定事件,同时可以让你从操作dom中解脱出来,只需要操作数据就会改变相 ...
- 消息队列RabbitMq、ActiveMq、ZeroMq、kafka之间的比较
MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求.下面我列出这些MQ之间的对比数据和资料. 第一部 ...
- 如何将项目上传到GitHub?
如何将项目上传到GitHub? 1.注册GitHub账户 浏览器输入GitHub官网地址:https://github.com/ 进入后点击Sign In 然后点击Create an account ...
- 【Redis篇】Redis集群安装与初始
一.前述 本文将单台节点不同端口模拟集群方式. 二.具体搭建 前提是安装好redis具体可参考http://www.cnblogs.com/LHWorldBlog/p/8463269.html 1 ...