//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Collections.Generic;usingSystem.Security.Cryptography.X509Certificates; usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.Security;usingGrapeCity.Documents.Pdf.AcroForms; namespaceDsPdfWeb.Demos{// This sample shows how to sign an existing PDF file containing// an empty signature field with a signature that complies with// the various PAdES (PDF Advanced Electronic Signatures) levels.// Note that when run online, this sample simply returns an unsigned// PDF with an empty signature field. To actually sign the PDF// you will need to provide a valid .pfx file (see "JohnDoe.pfx").// The sample contains 5 methods that can be used to sign a PDF// with the various PAdES levels, the last 3 methods can be// used in a chain to incrementally increase the signature strength.// To check the signature compliance, open the signed PDF in Acrobat// Reader and inspect the signature properties.//// IMPORTANT! The code in this sample WILL NOT work correctly// unless DsPdf is licensed with a valid license key.// Email us.sales@mescius.com to obtain a trial key.publicclassPadesLevels {// Certificate issued by CA Cert Signing Authority, used for verification:staticX509Certificate2s_caCertRoot = newX509Certificate2(Path.Combine("Resources", "Misc", "CACertRoot.crt"));// TODO: replace with a real certificate:staticX509Certificate2s_cert = newX509Certificate2(Path.Combine("Resources", "Misc", "JohnDoe.pfx"), "secret"); staticPadesLevels() {// TODO: GcPdfDocument MUST BE LICENSED for the signatures to remain valid,// as otherwise the license nag text added when saving the PDF invalidates// incremental updates. Email us.sales@mescius.com to obtain a trial key.// GcPdfDocument.SetLicenseKey("my key"); } publicvoidCreatePDF(Stream stream, int paramsIdx = 0) {CreatePDF(stream, GetSampleParamsList()[paramsIdx]); } // While this sample includes code that does the actual signing and time stamping,// that code is NOT executed by the online demo. So the online sample driver// simply returns the PDF that includes info about the selected PAdES level,// and can be signed or time stamped using a valid certificate.// You can copy the corresponding code and use it in your applications.publicvoidCreatePDF(Stream stream, string[] sampleParams) {// TODO: change to true to actually run the signing/time stamping code:bool doSigning = false; // The unsigned PDF with a signature field to sign:var fn = Path.Combine("Resources", "PDFs", sampleParams[3]); if (doSigning) {// Running this code will produce 5 PDFs with increasing PAdES levels: // PAdES B-B:Do_B_B(fn);// PAdES B-T:Do_B_T(fn);// Note that the next 3 steps use PDFs produced by the previous step,// incrementally adding verification info:// PAdES B-LT:Do_B_LT("B-T.pdf");// PAdES B-LTA:Do_B_LTA("B-LT.pdf");// LTV Enabled:Do_B_LTA_LTV("B-LTA.pdf"); } // Copy the source PDF to output stream// to provide the demo result:usingvar fs = File.OpenRead(fn); fs.CopyTo(stream); } // Signs a PDF with a PAdES B-B Level signature:voidDo_B_B(string fn) {usingFileStream fs = File.OpenRead(fn);var doc = newGcPdfDocument(); doc.Load(fs); var signField = doc.AcroForm.Fields.FirstOrDefault(f_ => f_ isSignatureField);if (signField == null)thrownewException("Could not find a signature field."); var sp = newSignatureProperties() {SignatureField = signField,SignatureBuilder = newPkcs7SignatureBuilder(s_cert) {Format = Pkcs7SignatureBuilder.SignatureFormat.ETSI_CAdES_detached, }, };// Sign and save the PDF to a file: doc.Sign(sp, "B-B.pdf"); } // Signs a PDF with a PAdES B-T Level signature:voidDo_B_T(string fn) {usingFileStream fs = File.OpenRead(fn);var doc = newGcPdfDocument(); doc.Load(fs); var signField = doc.AcroForm.Fields.FirstOrDefault(f_ => f_ isSignatureField);if (signField == null)thrownewException("Could not find a signature field."); var sp = newSignatureProperties() {SignatureField = signField,SignatureBuilder = newPkcs7SignatureBuilder(s_cert) {Format = Pkcs7SignatureBuilder.SignatureFormat.ETSI_CAdES_detached, },TimeStamp = newTimeStamp(@"http://ts.ssl.com"), };// Sign and save the PDF to a file: doc.Sign(sp, "B-T.pdf"); } // Adds LTV information to a B-T level signature (e.g. as created by the do_B_T method above),// which makes the signature compliant with PAdES B-LT:voidDo_B_LT(string fn) {usingFileStream fs = File.OpenRead(fn);var doc = newGcPdfDocument(); doc.Load(fs); var signField = doc.AcroForm.Fields.FirstOrDefault(f_ => f_ isSignatureField);if (signField == null)thrownewException("Could not find a signature field."); // Get the signature and add verification information for it:var sig = (Signature)signField.Value;var vp = newDocumentSecurityStore.VerificationParams(); vp.Certificates = newX509Certificate2[] { s_caCertRoot };if (!doc.SecurityStore.AddVerification(sig, vp))thrownewException($"Could not add verification for {sig.Name}."); // Save the PDF to a file using incremental update so that the signature remains valid: doc.Save("B-LT.pdf", SaveMode.IncrementalUpdate); } // Adds time stamp to a signed PDF (e.g. as created by the do_B_LT method above),// which makes the document compliant with B-LTA level:voidDo_B_LTA(string fn) {usingFileStream fs = File.OpenRead(fn);var doc = newGcPdfDocument(); doc.Load(fs); var ts = newTimeStampProperties() {TimeStamp = newTimeStamp(@"http://ts.ssl.com"), };// Save the PDF to a file adding a time stamp to it: doc.TimeStamp(ts, "B-LTA.pdf"); } // Adds verification information for a PDF time-stamp (e.g. as created by the do_B_LTA method above)// which makes the signature LTV enabled:voidDo_B_LTA_LTV(string fn) {usingFileStream fs = File.OpenRead(fn);var doc = newGcPdfDocument(); doc.Load(fs); var signField = doc.AcroForm.Fields.FirstOrDefault(f_ => f_ isSignatureField);if (signField == null)thrownewException("Could not find a signature field."); // Get the signature and add verification information for it:var sig = (Signature)signField.Value;if (!doc.SecurityStore.AddVerification(sig))thrownewException($"Could not add verification for {sig.Name}."); // Save the PDF to a file using incremental update so that the signature remains valid: doc.Save("B-LTA_LTV.pdf", SaveMode.IncrementalUpdate); } publicstaticList<string[]> GetSampleParamsList() {// Strings are name, description, info, rest are arbitrary strings:returnnewList<string[]>() {newstring[] { "@b-sign/PAdES B-B Level", "How to sign a PDF complying with PAdES B-B level", "","PAdES-B-B.pdf" },newstring[] { "@b-sign/PAdES B-T Level", "How to sign a PDF complying with PAdES B-T level", "","PAdES-B-T.pdf" },newstring[] { "@b-sign/PAdES B-LT Level", "How to add LTV information to a signature", "","PAdES-B-LT.pdf" },newstring[] { "@b-sign/PAdES B-LTA Level", "How to time stamp a signed PDF", "","PAdES-B-LTA.pdf" },newstring[] { "@b-sign/LTV Enabled Signature", "How to make a signature LTV enabled", "","PAdES-B-LTA-LTV.pdf" }, }; } }}