溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C# 在PDF中繪制動(dòng)態(tài)圖章

發(fā)布時(shí)間:2020-07-12 16:37:32 來源:網(wǎng)絡(luò) 閱讀:1157 作者:E_iceblue 欄目:編程語言

我們知道,動(dòng)態(tài)圖章,因圖章中的時(shí)間、日期可以動(dòng)態(tài)的生成,因而具有較強(qiáng)的時(shí)效性。在本篇文章中將介紹通過C#編程在PDF中繪制動(dòng)態(tài)圖章的方法,該方法可自動(dòng)獲取當(dāng)前系統(tǒng)登錄用戶名、日期及時(shí)間信息并生成圖章。

使用工具

  • Spire.PDF for .NET
    注:下載安裝后,注意在程序中添加引用Spire.PDF.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)
    C# 在PDF中繪制動(dòng)態(tài)圖章

    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動(dòng)態(tài)圖章
{
    class Program
    {
        static void Main(string[] args)
        {
            //創(chuàng)建PdfDocument對(duì)象
            PdfDocument doc = new PdfDocument();

            //加載現(xiàn)有PDF文檔
            doc.LoadFromFile("sample.pdf");

            //獲取要添加動(dòng)態(tài)印章的頁面
            PdfPageBase page = doc.Pages[1];

            //創(chuàng)建模板對(duì)象
            PdfTemplate template = new PdfTemplate(120, 60);

            //創(chuàng)建字體
            PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.SinoTypeSongLight, 16f, PdfFontStyle.Bold | PdfFontStyle.Italic);
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋體", 10f), true);

            //創(chuàng)建單色畫刷和漸變畫刷
            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);

            //創(chuàng)建圓角矩形路徑
            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);

            //在模板上繪制印章文字、系統(tǒng)用戶名、日期
            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));

            //創(chuàng)建PdfRubberStampAnnotation對(duì)象,并指定其位置和大小
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(page.ActualSize.Width - 300, 380), template.Size));

            //創(chuàng)建PdfApperance對(duì)象,并將模板應(yīng)用為一般狀態(tài)
            PdfAppearance apprearance = new PdfAppearance(stamp);
            apprearance.Normal = template;

            //在印章上應(yīng)用PdfApperance對(duì)象(即樣式)
            stamp.Appearance = apprearance;

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

            //保存文檔
            doc.SaveToFile("output.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("output.pdf");
        }
    }
}

完成代碼后,調(diào)試運(yùn)行程序,生成文檔。在生成的文檔中,文末已添加了動(dòng)態(tài)的圖章,如下圖所示:
C# 在PDF中繪制動(dòng)態(tài)圖章

以上是本次關(guān)于C#在PDF文檔中繪制動(dòng)態(tài)圖章的方法介紹。

感謝閱讀。

(本文完)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI