SignatureAppearance.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Drawing
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Pdf.AcroForms
  10. Imports GrapeCity.Documents.Pdf.Graphics
  11. Imports GrapeCity.Documents.Text
  12. Imports System.Security.Cryptography.X509Certificates
  13. Imports GCTEXT = GrapeCity.Documents.Text
  14. Imports GCDRAW = GrapeCity.Documents.Drawing
  15.  
  16. '' This sample demonstrates how to create and sign a PDF with a .pfx file,
  17. '' specifying a custom AppearanceStream to represent the signature.
  18. '' This sample is similar to SignDoc, except that it adds the AppearanceStream.
  19. Public Class SignatureAppearance
  20. Function CreatePDF(ByVal stream As Stream) As Integer
  21. Dim doc = New GcPdfDocument()
  22. Dim page = doc.NewPage()
  23. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  24. page.Graphics.DrawString(
  25. "Hello, World!" + vbLf +
  26. "Signed by DsPdfWeb SignatureAppearance sample.",
  27. tf, New PointF(72, 72))
  28.  
  29. '' Init a test certificate:
  30. Dim pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx")
  31. Dim cert = New X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
  32. X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet Or X509KeyStorageFlags.Exportable)
  33. Dim sp = New SignatureProperties() With {
  34. .SignatureBuilder = New Pkcs7SignatureBuilder() With {
  35. .CertificateChain = New X509Certificate2() {cert}
  36. },
  37. .Location = "DsPdfWeb Demo Browser",
  38. .SignerName = "DsPdfWeb",
  39. .SigningDateTime = Util.TimeNow()
  40. }
  41.  
  42. '' Create a signature field to hold the signature:
  43. Dim sf = New SignatureField()
  44. '' Add the signature field to the document:
  45. doc.AcroForm.Fields.Add(sf)
  46. '' Connect the signature field and signature props:
  47. sp.SignatureField = sf
  48.  
  49. '' Set up the signature field:
  50. sf.Widget.Rect = New RectangleF(page.Size.Width - 72 * 4, 72 * 2, 72 * 3, 72)
  51. sf.Widget.Page = page
  52. '' Widget visual props will be overridden by sf.Widget.AppearanceStreams.Normal.Default set below:
  53. sf.Widget.BackColor = Color.PeachPuff
  54. sf.Widget.Border = New GrapeCity.Documents.Pdf.Annotations.Border() With
  55. {
  56. .Color = Color.SaddleBrown,
  57. .Width = 3
  58. }
  59. sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}{vbLf}Location: {sp.Location}"
  60. '' Create custom signature appearance stream:
  61. Dim rc = New RectangleF(PointF.Empty, sf.Widget.Rect.Size)
  62. Dim fxo = New FormXObject(doc, rc)
  63. rc.Inflate(-4, -4)
  64. fxo.Graphics.FillEllipse(rc, Color.CornflowerBlue)
  65. fxo.Graphics.DrawEllipse(rc, New GCDRAW.Pen(Color.RoyalBlue, 3))
  66. rc.Inflate(-5, -5)
  67. fxo.Graphics.DrawEllipse(rc, New GCDRAW.Pen(Color.LightSteelBlue, 1))
  68. fxo.Graphics.DrawString($"Signed by {sp.SignerName}{vbLf}on {Util.TimeNow():yyyy-MM-dd}.",
  69. New TextFormat() With
  70. {
  71. .FontName = "Times New Roman",
  72. .FontSize = 14,
  73. .FontItalic = True,
  74. .ForeColor = Color.Navy
  75. },
  76. fxo.Bounds,
  77. TextAlignment.Center, ParagraphAlignment.Center, False)
  78. sf.Widget.AppearanceStreams.Normal.Default = fxo
  79.  
  80. '' Reset signature appearance so that the widget appearance stream is used:
  81. sp.SignatureAppearance = Nothing
  82.  
  83. '' Sign and save the document:
  84. '' NOTES:
  85. '' - Signing and saving is an atomic operation, the two cannot be separated.
  86. '' - The stream passed to the Sign() method must be readable.
  87. doc.Sign(sp, stream)
  88.  
  89. '' Done (the generated and signed docment has already been saved to 'stream').
  90. Return doc.Pages.Count
  91. End Function
  92. End Class
  93.