'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.Pdf.AcroFormsImportsGrapeCity.Documents.TextImportsSystem.Security.Cryptography.X509Certificates '' This sample demonstrates how to sign the created PDF with a .pfx file,'' using a SignatureField.'' The sample then loads the signed file back into another GcPdfDocument instance'' and verifies the signature.PublicClassSignDocFunctionCreatePDF(ByVal stream AsStream) AsIntegerDim doc = NewGcPdfDocument()Dim page = doc.NewPage()Dim tf = NewTextFormat() With {.Font = StandardFonts.Times, .FontSize = 14} page.Graphics.DrawString("Hello, World!" + vbLf +"Signed by DsPdfWeb SignDoc sample.", tf, NewPointF(72, 72)) '' Init a test certificate:Dim pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx")Dim cert = NewX509Certificate2(File.ReadAllBytes(pfxPath), "qq",X509KeyStorageFlags.MachineKeySetOrX509KeyStorageFlags.PersistKeySetOrX509KeyStorageFlags.Exportable)Dim sp = NewSignatureProperties() With { .SignatureBuilder = NewPkcs7SignatureBuilder() With {.CertificateChain = NewX509Certificate2() {cert}}, .Location = "DsPdfWeb Demo Browser", .SignerName = "DsPdfWeb", .SigningDateTime = Util.TimeNow() } '' Init a signature field to hold the signature:Dim sf = NewSignatureField() sf.Widget.Rect = NewRectangleF(72, 72 * 2, 72 * 4, 36) sf.Widget.Page = page sf.Widget.BackColor = Color.LightSeaGreen sf.Widget.DefaultAppearance.Font = StandardFonts.Helvetica sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}{vbCrLf}Location: {sp.Location}"'' Add the signature field to the document: doc.AcroForm.Fields.Add(sf) '' Connect the signature field and signature props: sp.SignatureField = sf '' Sign and save the document:'' NOTES:'' - Signing and saving is an atomic operation, the two cannot be separated.'' - The stream passed to the Sign() method must be readable. doc.Sign(sp, stream) '' Rewind the stream to read the document just created'' into another GcPdfDocument and verify the signature: stream.Seek(0, SeekOrigin.Begin)Dim doc2 = NewGcPdfDocument() doc2.Load(stream)Dim sf2 = DirectCast(doc2.AcroForm.Fields(0), SignatureField)IfNot sf2.Value.VerifySignatureValue() ThenThrowNew Exception("Failed to verify the signature")EndIf'''' Done (the generated and signed document has already been saved to 'stream').Return doc.Pages.CountEndFunctionEndClass