'''' 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 generates and signs a PDF (using code that is similar to SignDoc sample),'' and then signs the generated PDF with a second signature without invalidating the original'' signature, by using incremental update (default when using the Sign() method).PublicClassSignIncrementalFunctionCreatePDF(ByVal stream AsStream) AsIntegerDim doc = NewGcPdfDocument() '' Load a signed document (we use code similar to the SignDoc sample): doc.Load(CreateAndSignPdf()) '' Init a second certificate:Dim pfxPath = Path.Combine("Resources", "Misc", "JohnDoe.pfx")Dim cert = NewX509Certificate2(File.ReadAllBytes(pfxPath), "secret",X509KeyStorageFlags.MachineKeySetOrX509KeyStorageFlags.PersistKeySetOrX509KeyStorageFlags.Exportable)Dim sp2 = NewSignatureProperties() With { .SignatureBuilder = NewPkcs7SignatureBuilder() With {.CertificateChain = NewX509Certificate2() {cert}}, .Location = "DsPdfWeb Demo Browser", .SignerName = "Jaime Smith", .SigningDateTime = Util.TimeNow() } '' Find the 2nd (not yet filled) signature field:Dim sfld2 = CType(doc.AcroForm.Fields("SecondSignature"), SignatureField)'' Connect the signature field and signature props:If sfld2 IsNothingThenThrowNew Exception("Unexpected: could not find 'SecondSignature' field")EndIf sp2.SignatureField = sfld2 '' Sign and save the document: doc.Sign(sp2, stream) '' Rewind the stream to read the document just created'' into another GcPdfDocument and verify all signatures: stream.Seek(0, SeekOrigin.Begin)Dim doc2 = NewGcPdfDocument() doc2.Load(stream)ForEach fld In doc2.AcroForm.FieldsIfTypeOf fld IsSignatureFieldThenDim sfld = CType(fld, SignatureField)IfNot sfld.Value.VerifySignatureValue() ThenThrowNew Exception($"Failed to verify signature for field {sfld.Name}")EndIfEndIfNext'''' Done (the generated and signed document has already been saved to 'stream').Return doc.Pages.CountEndFunction '' This method is almost exactly the same as the SignDoc sample,'' but adds a second signature field (does not sign it though):PrivateFunctionCreateAndSignPdf() AsStreamDim doc = NewGcPdfDocument()Dim page = doc.NewPage()Dim tf = NewTextFormat() With {.Font = StandardFonts.Times, .FontSize = 14} page.Graphics.DrawString("Hello, World!" + vbLf +"Signed TWICE by DsPdfWeb SignIncremental 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'' Add the signature field to the document: doc.AcroForm.Fields.Add(sf) '' Connect the signature field and signature props: sp.SignatureField = sf '' Add a second signature field:Dim sf2 = NewSignatureField() With {.Name = "SecondSignature"} sf2.Widget.Rect = NewRectangleF(72, 72 * 3, 72 * 4, 36) sf2.Widget.Page = page sf2.Widget.BackColor = Color.LightYellow'' Add the signature field to the document: doc.AcroForm.Fields.Add(sf2) Dim ms = NewMemoryStream() doc.Sign(sp, ms)Return msEndFunctionEndClass