VisualSignature.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.Pdf
  8. Imports GrapeCity.Documents.Pdf.AcroForms
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13. Imports System.Security.Cryptography.X509Certificates
  14.  
  15. '' This sample demonstrates how to create and sign a PDF with a .pfx file,
  16. '' using a SignatureField and a signature image.
  17. '' The sample then loads the signed file back into another GcPdfDocument instance
  18. '' and verifies the signature.
  19. '' This sample is identical to SignDoc, but adds an image representing the signature.
  20. Public Class VisualSignature
  21. Function CreatePDF(ByVal stream As Stream) As Integer
  22. Dim doc = New GcPdfDocument()
  23. Dim page = doc.NewPage()
  24. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  25. page.Graphics.DrawString("Hello, World!" + vbLf +
  26. "Signed by DsPdfWeb VisualSignature 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. {
  35. .SignatureBuilder = New Pkcs7SignatureBuilder() With {
  36. .CertificateChain = New X509Certificate2() {cert}
  37. },
  38. .Location = "DsPdfWeb Demo Browser",
  39. .SignerName = "DsPdfWeb"
  40. }
  41.  
  42. '' Add an image representing the signature:
  43. sp.SignatureAppearance.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "signature.png"))
  44. sp.SignatureAppearance.CaptionImageRelation = GrapeCity.Documents.Pdf.Annotations.CaptionImageRelation.ImageOnly
  45.  
  46. '' Init a signature field to hold the signature:
  47. Dim sf = New SignatureField()
  48. sf.Widget.Rect = New RectangleF(72, 72 * 2, 72 * 4, 36)
  49. sf.Widget.Page = page
  50. sf.Widget.BackColor = Color.LightSeaGreen
  51. sf.Widget.DefaultAppearance.Font = StandardFonts.Helvetica
  52. sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}{vbCrLf}Location: {sp.Location}"
  53. '' Add the signature field to the document:
  54. doc.AcroForm.Fields.Add(sf)
  55. '' Connect the signature field and signature props:
  56. sp.SignatureField = sf
  57.  
  58. '' Sign and save the document:
  59. '' NOTES:
  60. '' - Signing and saving is an atomic operation, the two cannot be separated.
  61. '' - The stream passed to the Sign() method must be readable.
  62. doc.Sign(sp, stream)
  63.  
  64. '' Rewind the stream to read the document just created
  65. '' into another GcPdfDocument and verify the signature:
  66. stream.Seek(0, SeekOrigin.Begin)
  67. Dim doc2 = New GcPdfDocument()
  68. doc2.Load(stream)
  69. Dim sf2 = CType(doc2.AcroForm.Fields(0), SignatureField)
  70. If Not sf2.Value.VerifySignatureValue() Then
  71. Throw New Exception("Failed to verify the signature")
  72. End If
  73.  
  74. '' Done (the generated and signed docment has already been saved to 'stream').
  75. Return doc.Pages.Count
  76. End Function
  77. End Class
  78.