当前位置: 移动技术网 > IT编程>开发语言>.net > C# 在PDF中绘制动态图章

C# 在PDF中绘制动态图章

2018年08月29日  | 移动技术网IT编程  | 我要评论

刀削面的来历,736晋江网736,化物语h

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

步骤 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(5, 5));
template.graphics.drawstring(s2, font2, brush, new pointf(2, 28));

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

//创建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);

步骤 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[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");
        }
    }
}
view code

 

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

感谢阅读。

(本文完)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网