0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#  PDFでダイナミックスタンプを追加 

Posted at

ダイナミックスタンプ(Dynamic Stamp)はコンピューターのユーザー情報パネルから情報を取得して、ユーザーと日付などの情報を表示できる動的なスタンです。今日はSpire.PDFという無料で使いやすいライブラリを利用してPDFでダイナミックスタンプを作成する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.PDF無料版をダウンロードしてください。

f:id:lendoris:20210729160327p:plain

2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。

(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。)

 f:id:lendoris:20210729160335p:plain

```c# using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Annotations.Appearance; using Spire.Pdf.Graphics;

namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
//PdfDocument obejctを作成します。
PdfDocument doc = new PdfDocument();

        //PDFをロードします。
        doc.LoadFromFile(@"C:\Users\Administrator.SD-20151030NEMY\Desktop\java输出\吾輩.pdf");

        //スタンプを作成するページを取得します。
        PdfPageBase page = doc.Pages[4];

        //テンプレートオブジェクトを作成します。
        PdfTemplate template = new PdfTemplate(180, 50);

        //フォントを設定します。
        PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.SinoTypeSongLight, 16f, PdfFontStyle.Bold | PdfFontStyle.Italic);
        PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("ms mincho", 10f), true);

        //ソリッドブラシとグラデーションブラシを作成します。

        PdfSolidBrush brush = new PdfSolidBrush(Color.Purple);
        RectangleF rect = new RectangleF(new PointF(0, 0), template.Size);
        PdfLinearGradientBrush gradientBrush = new PdfLinearGradientBrush(rect, Color.White, Color.LightBlue, 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.Purple, path);

        //テンプレートでテキスト、ユーザー名と日付を描きます。
        String s1 = "検証済みです。";
        String s2 = System.Environment.UserName + " " + 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 - 250, 200), template.Size));

        //PdfApperanceオブジェクトを作成し、テンプレートをnormalにします。
        PdfAppearance apprearance = new PdfAppearance(stamp);
        apprearance.Normal = template;

        //スタンプでPdfApperanceオブジェクトを適用します。
        stamp.Appearance = apprearance;

        //スタンプをPdfAnnotationに追加します。
        page.AnnotationsWidget.Add(stamp);

        //保存します。
        doc.SaveToFile("output.pdf", FileFormat.PDF);

    }
}
<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210729/20210729160449.png" alt="f:id:lendoris:20210729160449p:plain" width="554" height="521" loading="lazy" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?