资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#在PDF中绘制动态图章-创新互联

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

创新互联是专业的荔湾网站建设公司,荔湾接单;提供网站制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行荔湾网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

使用工具

  • Spire.PDF for .NET
    注:下载安装后,注意在程序中添加引用Spire.PDF.dll(dll文件可在安装路径下的Bin文件夹中获取)
    C# 在PDF中绘制动态图章

    C#代码示例(供参考)

    【C#】

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[1];

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

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

            //在模板上画圆角矩形路径,并用渐变色填充
            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(5, 5));
            template.Graphics.DrawString(s2, font2, brush, new PointF(2, 28));

            //创建PdfRubberStampAnnotation对象,并指定其位置和大小
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(page.ActualSize.Width - 300, 380), 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中绘制动态图章

以上是本次关于C#在PDF文档中绘制动态图章的方法介绍。

感谢阅读。

(本文完)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:C#在PDF中绘制动态图章-创新互联
本文URL:http://cdkjz.cn/article/cdphsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220