我们知道,动态图章,因图章中的时间、日期可以动态的生成,因而具有较强的时效性。在本篇文章中将介绍通过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. JetBrains系列IDE快捷键大全(转载)

    编辑 快捷键组合 说明 Ctrl + Space 代码自动完成提示(选择) Alt + Enter 显示意图动作和快速修复 Ctrl + P 参数信息 (在调用方法参数忘记的时候,提示) Ctrl + ...

  2. CDN(Content Delivery Network)技术原理概要

    简介 CDN(Content Delivery Network)即内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度等功能,使用户就近获取所需内容,提高用户访问响应速度和 ...

  3. Spring Security中html页面设置hasRole无效的问题

    Spring Security中html页面设置hasRole无效的问题 一.前言 学了几天的spring Security,偶然发现的hasRole和hasAnyAuthority的区别.当然,可能 ...

  4. 小程序textarea完美填坑

    相信做微信小程序的码友们都被textarea这个原生组件坑过,什么placeholder位置错乱,穿透弹窗或遮罩层,ios上输入法弹起后换行输入内容遮挡,删除输入内容时内容被遮挡等等... 反正综上所 ...

  5. 【javascript】函数中的this的四种绑定形式 — 大家准备好瓜子,我要讲故事啦~~

       javascript中的this和函数息息相关,所以今天,我就给大家详细地讲述一番:javascript函数中的this   一谈到this,很多让人晕晕乎乎的抽象概念就跑出来了,这里我就只说最 ...

  6. [Swift]LeetCode331. 验证二叉树的前序序列化 | Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  7. [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  8. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  9. [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  10. [Swift]LeetCode984. 不含 AAA 或 BBB 的字符串 | String Without AAA or BBB

    Given two integers A and B, return any string S such that: S has length A + B and contains exactly A ...