加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 在PDF中绘制动态图章

发布时间:2020-12-16 01:08:27 所属栏目:百科 来源:网络整理
导读:我们知道,动态图章,因图章中的时间、日期可以动态的生成,因而具有较强的时效性。在本篇文章中将介绍通过C#编程在PDF中绘制动态图章的方法,该方法可自动获取当前系统登录用户名、日期及时间信息并生成图章。 使用工具 Spire.PDF for .NET 注:下载安装后

我们知道,动态图章,因图章中的时间、日期可以动态的生成,因而具有较强的时效性。在本篇文章中将介绍通过C#编程在PDF中绘制动态图章的方法,该方法可自动获取当前系统登录用户名、日期及时间信息并生成图章。

使用工具

  • Spire.PDF for .NET

注:下载安装后,注意在程序中添加引用Spire.PDF.dll(dll文件可在安装路径下的Bin文件夹中获取)

?

?

C#代码示例(供参考)

步骤 1 :添加using指令

using Spire.Pdf;
 Spire.Pdf.Annotations;
 Spire.Pdf.Annotations.Appearance;
 Spire.Pdf.Graphics;
 System;
using System.Drawing;

步骤 2 :创建文档,加载测试文件

//创建PdfDocument对象
PdfDocument doc = new PdfDocument();

加载现有PDF文档
doc.LoadFromFile("sample.pdf");

步骤 3 :获取需要添加动态图章的页面

PdfPageBase page = doc.Pages[1];

步骤 4 :创建印章模板、字体、画刷等

创建模板对象
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 =  PdfSolidBrush(Color.Red);
RectangleF rect = new RectangleF(new PointF(0,1)">0),template.Size);
PdfLinearGradientBrush gradientBrush =  PdfLinearGradientBrush(rect,Color.White,PdfLinearGradientMode.Horizontal);

创建圆角矩形路径
int CornerRadius = 10;
PdfPath path =  PdfPath();
path.AddArc(template.GetBounds().X,template.GetBounds().Y,CornerRadius,180,1)">90);
path.AddArc(template.GetBounds().X + template.Width - CornerRadius,1)">270,template.GetBounds().Y + template.Height - CornerRadius,1)">);
path.AddArc(template.GetBounds().X,template.GetBounds().Y + template.Height - CornerRadius,1)">90,1)">);
path.AddLine(template.GetBounds().X,template.GetBounds().X,template.GetBounds().Y + CornerRadius / 2);

步骤 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,5,1)">5));
template.Graphics.DrawString(s2,font2,1)">2,1)">28));

步骤 7 :添加印章到PDF页面指定位置

创建PdfRubberStampAnnotation对象,并指定其位置和大小
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new PointF(page.ActualSize.Width - 300,1)">380创建PdfApperance对象,并将模板应用为一般状态
PdfAppearance apprearance =  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(");

完成以上步骤后,调试运行程序,生成文档。在生成的文档中,文末已添加了动态的图章,如下图所示:

全部代码:

 System.Drawing;

namespace PDF动态图章
{
    class Program
    {
        static void Main(string[] args)
        {
            创建PdfDocument对象
            PdfDocument doc =  PdfDocument();

            加载现有PDF文档
            doc.LoadFromFile();

            获取要添加动态印章的页面
            PdfPageBase page = doc.Pages[1];

            创建模板对象
            PdfTemplate template = 创建字体
            PdfCjkStandardFont font1 =  PdfFontStyle.Italic);
            PdfTrueTypeFont font2 = 创建单色画刷和渐变画刷
            PdfSolidBrush brush =  PdfSolidBrush(Color.Red);
            RectangleF rect = 创建圆角矩形路径
            ;
            PdfPath path =  PdfPath();
            path.AddArc(template.GetBounds().X,1)">);
            path.AddArc(template.GetBounds().X + template.Width - CornerRadius,1)">);
            path.AddArc(template.GetBounds().X,1)">);
            path.AddLine(template.GetBounds().X,1)">2            template.Graphics.DrawPath(gradientBrush,path);
            在模板上画圆角矩形路径,并用红色填充路径
            template.Graphics.DrawPath(PdfPens.Red,path);

            在模板上绘制印章文字、系统用户名、日期
            String s1 = ;
            String s2 = System.Environment.UserName + );
            template.Graphics.DrawString(s1,1)">));
            template.Graphics.DrawString(s2,1)">28));

            创建PdfRubberStampAnnotation对象,并指定其位置和大小
            PdfRubberStampAnnotation stamp = 创建PdfApperance对象,并将模板应用为一般状态
            PdfAppearance apprearance =  PdfAppearance(stamp);
            apprearance.Normal = template;

            在印章上应用PdfApperance对象(即样式)
            stamp.Appearance = apprearance;

            将印章添加到PdfAnnotation集合
            page.AnnotationsWidget.Add(stamp);

            保存文档
            doc.SaveToFile();
        }
    }
}
View Code

?

以上是本次关于C#在PDF文档中绘制动态图章的方法介绍,在前面的文章中介绍了添加印章的到PDF文档的方法,有需要也可以查阅该文档。

感谢阅读。

(本文完)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读