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 绘制动态图
最近准备技能大赛,需要将从传感器中读出的数据在移动客户端以图的形式绘制出来,因为平时很少绘图,于是各种查资料,算是勉强做出来了. 以下是大赛理论效果图(左)和实际效果图(右),真的是理想很丰满,现实很 ...
随机推荐
- LVM学习笔记
LVM Logical Volume Manager Volume management creates a layer of abstraction over physical storage, a ...
- java内存分页计算
介绍三个最常用的分页算法 First(感觉这个最简单实用) //总记录数int rows=21; //每页显示的记录数int pageSize=5; //页数int pageSum=(rows-1)/ ...
- 分享我在 vue 项目中关于 api 请求的一些实现及项目框架
本文主要简单分享以下四点 如何使用 axios 如何隔离配置 如何模拟数据 分享自己的项目框架 本文主要目的为以下三点 希望能够帮到一些人 希望能够得到一些建议 奉上一个使用Vue的模板框架 我只是把 ...
- IOS微信点击input弹出输入法,关闭后页面留白解决方案
场景:IOS用微信点击input框弹出输入法后 不管你是输入信息,还是不输入直接点完成关闭输入法,都会导致页面被挤上去后产生留白,从而改变页面布局 解决方法: 给input添加 ...
- [Swift]LeetCode18. 四数之和 | 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- [Bash]LeetCode195. 第十行 | Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...
- [Swift]LeetCode217. 存在重复元素 | Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- PHP算法之二分查找
二分查找: 数组必须有序,且不重复. 一般实际工作中,很少有这样的数组,所以应用的很少,但是思想很好. 1 // 二分查找 2 $array = [10,14,23,33,45,56,65,77,89 ...
- CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机
使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...
- Kafka对Java程序员有多重要?连阿里都再用它处理亿万级数据统计
一.了解淘宝Kafka架构 在ActiveMQ.RabbitMQ.RocketMQ.Kafka消息中间件之间,我们为什么要选择Kafka?下面详细介绍一下,2012年9月份我在支付宝做余额宝研发,20 ...