SignatureAppearance.cs
- //
- // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
- // © MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using GrapeCity.Documents.Drawing;
- using GrapeCity.Documents.Pdf;
- using GrapeCity.Documents.Pdf.AcroForms;
- using GrapeCity.Documents.Pdf.Graphics;
- using GrapeCity.Documents.Text;
- using System.Security.Cryptography.X509Certificates;
- using GCTEXT = GrapeCity.Documents.Text;
- using GCDRAW = GrapeCity.Documents.Drawing;
-
- namespace DsPdfWeb.Demos
- {
- // このサンプルは、PDF を作成し、.pfx ファイルを使用して署名して、
- // 署名を表すカスタム AppearanceStream を指定する方法を示します。
- // このサンプルは、AppearanceStream を追加することを除き、 SignDoc と類似しています。
- public class SignatureAppearance
- {
- public int CreatePDF(Stream stream)
- {
- var doc = new GcPdfDocument();
- var page = doc.NewPage();
- var tf = new TextFormat() { Font = Common.Util.getFont(), FontSize = 12 };
- page.Graphics.DrawString("デジタル署名のサンプルです\n" +
- "このテキストの下の位置で署名されています。\n" +
- "注意:ブラウザのビルトインビューワによっては署名が表示されない場合があります。\n" +
- "署名に使用している証明書はテスト用です。",
- tf, new PointF(72, 72));
-
- // テスト証明書を初期化します。
- var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx");
- var cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
- X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
- var sp = new SignatureProperties()
- {
- SignatureBuilder = new Pkcs7SignatureBuilder()
- {
- CertificateChain = new X509Certificate2[] { cert }
- },
- Location = "DioDocs for PDF サンプル",
- SignerName = "DioDocs",
- SigningDateTime = Common.Util.TimeNow(),
- };
-
- // 署名を保持する署名フィールドを初期化します。
- var sf = new SignatureField();
- // 文書に署名フィールドを追加します。
- doc.AcroForm.Fields.Add(sf);
- // 署名フィールドと署名プロパティを結びつけます。
- sp.SignatureField = sf;
-
- // 署名フィールドを設定します。
- sf.Widget.Rect = new RectangleF(page.Size.Width - 72 * 4, 72 * 2, 72 * 3, 72);
- sf.Widget.Page = page;
- // ウィジェットのビジュアルプロパティは、以下で設定された sf.Widget.AppearanceStreams.Normal.Default によってオーバーライドされます。
- sf.Widget.BackColor = Color.PeachPuff;
- sf.Widget.Border = new GrapeCity.Documents.Pdf.Annotations.Border()
- {
- Color = Color.SaddleBrown,
- Width = 3,
- };
- sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}\r\nLocation: {sp.Location}";
- // カスタム署名の外観ストリームを作成します。
- var rc = new RectangleF(PointF.Empty, sf.Widget.Rect.Size);
- var fxo = new FormXObject(doc, rc);
- rc.Inflate(-4, -4);
- fxo.Graphics.FillEllipse(rc, Color.CornflowerBlue);
- fxo.Graphics.DrawEllipse(rc, new GCDRAW.Pen(Color.RoyalBlue, 3));
- rc.Inflate(-5, -5);
- fxo.Graphics.DrawEllipse(rc, new GCDRAW.Pen(Color.LightSteelBlue, 1));
- fxo.Graphics.DrawString($"{sp.SignerName} によって署名されました\n{DateTime.Now.ToShortDateString()}",
- new TextFormat()
- {
- Font = Common.Util.getFont(),
- FontSize = 12,
- FontItalic = true,
- ForeColor = Color.Navy
- },
- fxo.Bounds,
- TextAlignment.Center, ParagraphAlignment.Center, false);
- sf.Widget.AppearanceStreams.Normal.Default = fxo;
-
- // ウィジェットの外観ストリームが使用されるように署名の外観をリセットします。
- sp.SignatureAppearance = null;
-
- // 署名して文書を保存します。
- // 注意:
- // - 署名と保存は一連の操作であり、2つは分離できません。
- // - Sign() メソッドに渡されたストリームは読み込み可能でなければなりません。
- doc.Sign(sp, stream);
-
- // 終了(生成および署名された文書はすでに 'stream' に保存されています)。
- return doc.Pages.Count;
- }
- }
- }
-