//// 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 demonstrates how to create and sign a PDF with a .pfx file,// using a SignatureField.// The sample then loads the signed file back into another GcPdfDocument instance// and verifies the signature. // See also VisualSignature which is similar but adds a visual representation// of the signature.publicclassSignDoc {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();var page = doc.NewPage();var tf = newTextFormat() { Font = StandardFonts.Times, FontSize = 14 }; page.Graphics.DrawString("Hello, World!\n" +"Signed by DsPdfWeb SignDoc 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; sf.Widget.DefaultAppearance.Font = StandardFonts.Helvetica; sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}\r\nLocation: {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);var doc2 = newGcPdfDocument(); doc2.Load(stream);SignatureField sf2 = (SignatureField)doc2.AcroForm.Fields[0];if (!sf2.Value.VerifySignatureValue())thrownewException("Failed to verify the signature"); // Done (the generated and signed document has already been saved to 'stream').return doc.Pages.Count; } }}