SignatureAppearance.cs
  1. //
  2. // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
  3. // © MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Drawing;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Pdf.AcroForms;
  11. using GrapeCity.Documents.Pdf.Graphics;
  12. using GrapeCity.Documents.Text;
  13. using System.Security.Cryptography.X509Certificates;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // このサンプルは、PDF を作成し、.pfx ファイルを使用して署名して、
  20. // 署名を表すカスタム AppearanceStream を指定する方法を示します。
  21. // このサンプルは、AppearanceStream を追加することを除き、 SignDoc と類似しています。
  22. public class SignatureAppearance
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. var doc = new GcPdfDocument();
  27. var page = doc.NewPage();
  28. var tf = new TextFormat() { Font = Common.Util.getFont(), FontSize = 12 };
  29. page.Graphics.DrawString("デジタル署名のサンプルです\n" +
  30. "このテキストの下の位置で署名されています。\n" +
  31. "注意:ブラウザのビルトインビューワによっては署名が表示されない場合があります。\n" +
  32. "署名に使用している証明書はテスト用です。",
  33. tf, new PointF(72, 72));
  34.  
  35. // テスト証明書を初期化します。
  36. var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx");
  37. var cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
  38. X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
  39. var sp = new SignatureProperties()
  40. {
  41. SignatureBuilder = new Pkcs7SignatureBuilder()
  42. {
  43. CertificateChain = new X509Certificate2[] { cert }
  44. },
  45. Location = "DioDocs for PDF サンプル",
  46. SignerName = "DioDocs",
  47. SigningDateTime = Common.Util.TimeNow(),
  48. };
  49.  
  50. // 署名を保持する署名フィールドを初期化します。
  51. var sf = new SignatureField();
  52. // 文書に署名フィールドを追加します。
  53. doc.AcroForm.Fields.Add(sf);
  54. // 署名フィールドと署名プロパティを結びつけます。
  55. sp.SignatureField = sf;
  56.  
  57. // 署名フィールドを設定します。
  58. sf.Widget.Rect = new RectangleF(page.Size.Width - 72 * 4, 72 * 2, 72 * 3, 72);
  59. sf.Widget.Page = page;
  60. // ウィジェットのビジュアルプロパティは、以下で設定された sf.Widget.AppearanceStreams.Normal.Default によってオーバーライドされます。
  61. sf.Widget.BackColor = Color.PeachPuff;
  62. sf.Widget.Border = new GrapeCity.Documents.Pdf.Annotations.Border()
  63. {
  64. Color = Color.SaddleBrown,
  65. Width = 3,
  66. };
  67. sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}\r\nLocation: {sp.Location}";
  68. // カスタム署名の外観ストリームを作成します。
  69. var rc = new RectangleF(PointF.Empty, sf.Widget.Rect.Size);
  70. var fxo = new FormXObject(doc, rc);
  71. rc.Inflate(-4, -4);
  72. fxo.Graphics.FillEllipse(rc, Color.CornflowerBlue);
  73. fxo.Graphics.DrawEllipse(rc, new GCDRAW.Pen(Color.RoyalBlue, 3));
  74. rc.Inflate(-5, -5);
  75. fxo.Graphics.DrawEllipse(rc, new GCDRAW.Pen(Color.LightSteelBlue, 1));
  76. fxo.Graphics.DrawString($"{sp.SignerName} によって署名されました\n{DateTime.Now.ToShortDateString()}",
  77. new TextFormat()
  78. {
  79. Font = Common.Util.getFont(),
  80. FontSize = 12,
  81. FontItalic = true,
  82. ForeColor = Color.Navy
  83. },
  84. fxo.Bounds,
  85. TextAlignment.Center, ParagraphAlignment.Center, false);
  86. sf.Widget.AppearanceStreams.Normal.Default = fxo;
  87.  
  88. // ウィジェットの外観ストリームが使用されるように署名の外観をリセットします。
  89. sp.SignatureAppearance = null;
  90.  
  91. // 署名して文書を保存します。
  92. // 注意:
  93. // - 署名と保存は一連の操作であり、2つは分離できません。
  94. // - Sign() メソッドに渡されたストリームは読み込み可能でなければなりません。
  95. doc.Sign(sp, stream);
  96.  
  97. // 終了(生成および署名された文書はすでに 'stream' に保存されています)。
  98. return doc.Pages.Count;
  99. }
  100. }
  101. }
  102.