//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.AcroForms;usingGrapeCity.Documents.Text;usingSystem.Security.Cryptography.X509Certificates; namespaceDsPdfWeb.Demos{ // 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).publicclassSignIncremental {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument(); // Load a signed document (we use code similar to the SignDoc sample): doc.Load(CreateAndSignPdf());// Init a second certificate:var pfxPath = Path.Combine("Resources", "Misc", "JohnDoe.pfx");var cert = newX509Certificate2(File.ReadAllBytes(pfxPath), "secret",X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);var sp2 = newSignatureProperties() {SignatureBuilder = newPkcs7SignatureBuilder() {CertificateChain = newX509Certificate2[] { cert } },Location = "DsPdfWeb Demo Browser",SignerName = "DsPdfWeb",SigningDateTime = Common.Util.TimeNow(), };// Find the 2nd (not yet filled) signature field:var sfld2 = doc.AcroForm.Fields["SecondSignature"] asSignatureField;// Connect the signature field and signature props: sp2.SignatureField = sfld2 ?? thrownewException("Unexpected: could not find 'SecondSignature' field");// 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);var doc2 = newGcPdfDocument(); doc2.Load(stream);foreach (var fld in doc2.AcroForm.Fields)if (fld isSignatureField sfld)if (!sfld.Value.VerifySignatureValue())thrownewException($"Failed to verify signature for field {sfld.Name}"); // Done (the generated and signed document has already been saved to 'stream').return doc.Pages.Count; } // This method is almost exactly the same as the SignDoc sample,// but adds a second signature field (does not sign it though):privateStreamCreateAndSignPdf() {var doc = newGcPdfDocument();var page = doc.NewPage();var tf = newTextFormat() { Font = StandardFonts.Times, FontSize = 14 }; page.Graphics.DrawString("Hello, World!\n" +"Signed TWICE by DsPdfWeb SignIncremental sample.", tf, newPointF(72, 72)); // Init a test certificate:var pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx");var cert = newX509Certificate2(File.ReadAllBytes(pfxPath), "qq",X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);var sp = newSignatureProperties() {SignatureBuilder = newPkcs7SignatureBuilder() {CertificateChain = newX509Certificate2[] { cert } },Location = "DsPdfWeb Demo Browser",SignerName = "DsPdfWeb",SigningDateTime = Common.Util.TimeNow(), }; // Init a signature field to hold the signature:var 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:var sf2 = newSignatureField() { 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); var ms = newMemoryStream(); doc.Sign(sp, ms);return ms; } }}