C# 在PDF中繪製動態圖章

我們知道,動態圖章,因圖章中的時間、日期可以動態的生成,因而具有較強的時效性。在本篇文章中將介紹通過C#編程在PDF中繪製動態圖章的方法,該方法可自動獲取當前系統登錄用戶名、日期及時間信息並生成圖章。

使用工具

  • 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文檔中繪製動態圖章的方法介紹。

感謝閱讀。

(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章