我们知道,动态图章,因图章中的时间、日期可以动态的生成,因而具有较强的时效性。在本篇文章中将介绍通过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中绘制动态图章的更多相关文章

  1. C# 如何在PDF中绘制不同风格类型的文本

    通过对控件Spire.PDF的测试,我们可以创建PDF文件并向文档中绘制文本.图片.表格.图形等内容,其中,对于绘制文本这一部分,Spire.PDF提供了三种字体类型来绘制文本,即: Standard ...

  2. 【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 绘制矢量图 在上一篇iTextSharp文章中讲述了如何将现有的图片插入PDF中并对其进行操作.但有时,你需要在PDF中绘制不依 ...

  3. Java 处理PDF图章(印章)——图片图章、动态图章

    图章(印章)是一种在合同.票据.公文等文件中表明法律效应.部门机关权威的重要指示物,常见于各种格式的文件.文档中.对于纸质文档可以手动盖章,但对于电子文档,则需要通过特定的方法来实现.本篇文档分享通过 ...

  4. Java 在PDF文档中绘制图形

    本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...

  5. (转)原始图像数据和PDF中的图像数据

    比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...

  6. ZBrush中的动态网格该怎么进行运用

    DynaMesh是ZBrush最新的基础模型创建工具,该命令用于基本模型的起稿到中模的制作.使用DynaMesh完全不启用考虑模型的拓扑,可以从一个图形拉扯出整个模型的分支,本文将以一个实例简单介绍Z ...

  7. java 如何在pdf中生成表格

    1.目标 在pdf中生成一个可变表头的表格,并向其中填充数据.通过泛型动态的生成表头,通过反射动态获取实体类(我这里是User)的get方法动态获得数据,从而达到动态生成表格. 每天生成一个文件夹存储 ...

  8. PDF创建及动态转换控件程序包ActivePDF Portfolio

    ActivePDF Portfolio是将4个activePDF最优秀的服务器产品捆绑成一个价格适中的控件程序包.它提供了开发一个完整的服务器端的PDF解决方案所需的一切. 具体功能: activeP ...

  9. Android 绘制动态图

    最近准备技能大赛,需要将从传感器中读出的数据在移动客户端以图的形式绘制出来,因为平时很少绘图,于是各种查资料,算是勉强做出来了. 以下是大赛理论效果图(左)和实际效果图(右),真的是理想很丰满,现实很 ...

随机推荐

  1. [Codeforces Round #516][Codeforces 1063B/1064D. Labyrinth]

    题目链接:1063B - Labyrinth/1064D - Labyrinth 题目大意:给定一个\(n\times m\)的图,有若干个点不能走,上下走无限制,向左和向右走的次数分别被限制为\(x ...

  2. python scrapy框架爬取豆瓣

    刚刚学了一下,还不是很明白.随手记录. 在piplines.py文件中 将爬到的数据 放到json中 class DoubanmoviePipelin2json(object):#打开文件 open_ ...

  3. R语言之Apriori算法

    ---恢复内容开始--- 1.概念 关联分析:用于发现隐藏在大型数据集中的有意义的联系 项集:0或多个项的集合.例如:{啤酒,尿布,牛奶,花生} 是一个4-项集,意义想象成爸爸去超市买啤酒和花生,给儿 ...

  4. 谈一谈从 Delphi 2009 之后就支援的重要功能 – 泛型 (Generic)

    前言 在C++的语言基础当中,除了物件导向.事件驱动的概念之外,模版设计(Template)也是非常重要的一环.然而,C++的开发人员能够善用模版设计的并不多.模版设计这个好物,一般还有一个名称,就是 ...

  5. 从协议入手,剖析OAuth2.0(译 RFC 6749)

    1.介绍      https://tools.ietf.org/html/rfc6749  传统的client-server授权模型,客户端通过使用凭证(通常的用户名和明文密码)访问服务端受保护的资 ...

  6. 私有云的难处—为什么需要CloudEngine?

    私有云的难处 ——我们为什么需要 CloudEngine? 郑昀 创建于2016/7/31 最后更新于2016/8/3 关键词: 容器.Docker.OpenStack.虚拟机.私有云.Mesos.配 ...

  7. android自动化必备之SDK

    进入到SDK包中,通过打开SDK manager.exe即可看到SDK管理界面,可能部分童靴发现一直在加载出不来,我们需要设置代理来解决: 选择工具栏上的Tools->Options打开如下窗口 ...

  8. linux各个服务器的软件自启动

    首先你需要编写一个shell脚本,也就是启动app的,当然还应该有stop的脚本 这里贴出我的,因为每个人的服务安装路劲不同,故启动不同,仅供参考.如有雷同,纯属你智障 web服务器: 应用服务器: ...

  9. Xcode 命令行工具 Command Line Tools

    xcode命令行工具包是一个小型独立包,可供下载独立于Xcode的和允许您执行命令行开发OS X. 在OS X10.9,就以及没有clt的下载安装包了,需要使用命令在线安装. xcode-select ...

  10. ORA-01034: ORACLE not available问题

    通过DBCA新建一个数据库后,执行以下命令报错:SQL> shutdown immediate;ERROR:ORA-01034: ORACLE not availableORA-27101: s ...