diff --git a/Digital Signature/Adding-a-timestamp-to-PDF-document/.NET/Adding-a-timestamp-to-PDF-document/Program.cs b/Digital Signature/Adding-a-timestamp-to-PDF-document/.NET/Adding-a-timestamp-to-PDF-document/Program.cs index 0df6ac60..ab3ffd56 100644 --- a/Digital Signature/Adding-a-timestamp-to-PDF-document/.NET/Adding-a-timestamp-to-PDF-document/Program.cs +++ b/Digital Signature/Adding-a-timestamp-to-PDF-document/.NET/Adding-a-timestamp-to-PDF-document/Program.cs @@ -1,27 +1,16 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf; +using Syncfusion.Pdf; using Syncfusion.Pdf.Security; //Create a new pdf document. -PdfDocument document = new PdfDocument(); - -//Adds a new page. -PdfPage page = document.Pages.Add(); - -//Creates a digital signature. -PdfSignature signature = new PdfSignature(page, "Signature"); - -//Add the time stamp by using the server URI. -signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/")); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfDocument document = new PdfDocument()) { - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Adds a new page. + PdfPage page = document.Pages.Add(); + //Creates a digital signature. + PdfSignature signature = new PdfSignature(page, "Signature"); + //Add the time stamp by using the server URI. + signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/")); + //Save the PDF document + document.Save(Path.GetFullPath(@"Output/Output.pdf")); } -//Close the document. -document.Close(true); - diff --git a/Digital Signature/Adding-a-timestamp-to-an-existing-PDF/.NET/Adding-a-timestamp-to-an-existing-PDF/Program.cs b/Digital Signature/Adding-a-timestamp-to-an-existing-PDF/.NET/Adding-a-timestamp-to-an-existing-PDF/Program.cs index 499213c5..5ee344d2 100644 --- a/Digital Signature/Adding-a-timestamp-to-an-existing-PDF/.NET/Adding-a-timestamp-to-an-existing-PDF/Program.cs +++ b/Digital Signature/Adding-a-timestamp-to-an-existing-PDF/.NET/Adding-a-timestamp-to-an-existing-PDF/Program.cs @@ -1,30 +1,16 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf; +using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - //Load the PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Gets the page. -PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; - -//Creates a digital signature. -PdfSignature signature = new PdfSignature(page, "Signature"); - -//Add the time stamp by using the server URI. -signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/")); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); + //Gets the page. + PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; + //Creates a digital signature. + PdfSignature signature = new PdfSignature(page, "Signature"); + //Add the time stamp by using the server URI. + signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/")); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); } - -//Close the document. -loadedDocument.Close(true); diff --git a/Digital Signature/Adding-the-estimated-size-of-the-signature/.NET/Adding-the-estimated-size-of-the-signature/Program.cs b/Digital Signature/Adding-the-estimated-size-of-the-signature/.NET/Adding-the-estimated-size-of-the-signature/Program.cs index 688ba35a..28fedf77 100644 --- a/Digital Signature/Adding-the-estimated-size-of-the-signature/.NET/Adding-the-estimated-size-of-the-signature/Program.cs +++ b/Digital Signature/Adding-the-estimated-size-of-the-signature/.NET/Adding-the-estimated-size-of-the-signature/Program.cs @@ -1,32 +1,20 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Drawing; +using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Security; -//Creating a new PDF Document. -PdfDocument document = new PdfDocument(); - -//Adding a new page to the PDF document. -PdfPageBase page = document.Pages.Add(); - -//Creates a certificate instance from the PFX file with a private key. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion"); - -//Add a new signature to the PDF page. -PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); -signature.Bounds = new Rectangle(10, 20, 400, 200); - -//Sets the estimated size of the signature. -signature.EstimatedSignatureSize = 20000; - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new pdf document. +using (PdfDocument document = new PdfDocument()) { - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Adding a new page to the PDF document. + PdfPageBase page = document.Pages.Add(); + //Creates a certificate instance from the PFX file with a private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion"); + //Add a new signature to the PDF page. + PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); + signature.Bounds = new RectangleF(10, 20, 400, 200); + //Sets the estimated size of the signature. + signature.EstimatedSignatureSize = 20000; + //Save the PDF document + document.Save(Path.GetFullPath(@"Output/Output.pdf")); } - -//Close the document. -document.Close(true); diff --git a/Digital Signature/Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed/Program.cs b/Digital Signature/Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed/Program.cs index ef3774aa..3a96e3e8 100644 --- a/Digital Signature/Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed/Program.cs +++ b/Digital Signature/Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed/Program.cs @@ -1,15 +1,8 @@ -using System; -using System.IO; -using System.Reflection.Metadata; -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; - -// Open the signed PDF file for reading -using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - // Load the PDF document - PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream); - // Check if the document contains a form with fields if (loadedDocument.Form == null || loadedDocument.Form.Fields.Count == 0) { @@ -32,6 +25,6 @@ } } } - //Close the document - loadedDocument.Close(true); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); } \ No newline at end of file diff --git a/Digital Signature/Create-LTV-when-signing-PDF-documents-externally/.NET/Create-LTV-when-signing-PDF-documents-externally/Program.cs b/Digital Signature/Create-LTV-when-signing-PDF-documents-externally/.NET/Create-LTV-when-signing-PDF-documents-externally/Program.cs index c9b30676..9f78827e 100644 --- a/Digital Signature/Create-LTV-when-signing-PDF-documents-externally/.NET/Create-LTV-when-signing-PDF-documents-externally/Program.cs +++ b/Digital Signature/Create-LTV-when-signing-PDF-documents-externally/.NET/Create-LTV-when-signing-PDF-documents-externally/Program.cs @@ -1,75 +1,47 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf; +using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography.X509Certificates; - -//Get the stream from the document. -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - //Load an existing PDF document. -PdfLoadedDocument document = new PdfLoadedDocument(documentStream); - -//Get the page of the existing PDF document. -PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage; - -//Create a new PDF signature without PdfCertificate instance. -PdfSignature signature = new PdfSignature(document, loadedPage, null, "Signature1"); - -//Hook up the ComputeHash event. -signature.ComputeHash += Signature_ComputeHash; - -//Save the document into stream -MemoryStream stream = new MemoryStream(); -document.Save(stream); -//Close the document -document.Close(true); - - -//Load an existing PDF stream.. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); - -//Gets the first signature field of the PDF document -PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; -PdfSignature pdfSignature = signatureField.Signature; - -//Create X509Certificate2 from your certificate to create a long-term validity. -X509Certificate2 x509 = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion"); - -//Create LTV with your public certificates. -pdfSignature.CreateLongTermValidity(new List { x509 }); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); + //Get the page of the existing PDF document. + PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage; + //Create a new PDF signature without PdfCertificate instance. + PdfSignature signature = new PdfSignature(document, loadedPage, null, "Signature1"); + //Hook up the ComputeHash event. + signature.ComputeHash += Signature_ComputeHash; + //Save the document into stream + MemoryStream stream = new MemoryStream(); + document.Save(stream); + //Load an existing PDF stream.. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream)) + { + //Gets the first signature field of the PDF document + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + PdfSignature pdfSignature = signatureField.Signature; + //Create X509Certificate2 from your certificate to create a long-term validity. + X509Certificate2 x509 = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion"); + //Create LTV with your public certificates. + pdfSignature.CreateLongTermValidity(new List { x509 }); + //Save the PDF document. + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } } - -//Close the document. -loadedDocument.Close(true); - - void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars) { //Get the document bytes byte[] documentBytes = ars.Data; - SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true); - //Compute the signature using the specified digital ID file and the password X509Certificate2 certificate = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion"); CmsSigner cmsSigner = new CmsSigner(certificate); - //Set the digest algorithm SHA256 cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); - signedCms.ComputeSignature(cmsSigner); - //Embed the encoded digital signature to the PDF document ars.SignedData = signedCms.Encode(); } \ No newline at end of file diff --git a/Digital Signature/Customize-the-signed-date/.NET/Customize-the-signed-date/Program.cs b/Digital Signature/Customize-the-signed-date/.NET/Customize-the-signed-date/Program.cs index e9b53e70..4109a731 100644 --- a/Digital Signature/Customize-the-signed-date/.NET/Customize-the-signed-date/Program.cs +++ b/Digital Signature/Customize-the-signed-date/.NET/Customize-the-signed-date/Program.cs @@ -2,22 +2,19 @@ using Syncfusion.Pdf.Security; using Syncfusion.Drawing; -//Create a new PDF document instance. -PdfDocument document = new PdfDocument(); -//Add a new page to the PDF document. -PdfPage page = document.Pages.Add(); -//Create a digital signature and associate it with the added page. -PdfSignature signature = new PdfSignature(page, "Signature", new DateTime(2020, 12, 24, 10, 50, 10)); -//Set the timestamp server for the digital signature. -signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamp.digicert.com")); -//Set the signed name for the signature. -signature.SignedName = "Test"; -//Define the bounds for the signature (position and size on the page). -signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(200, 100)); -//Save the document to a file using a file stream. -using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new pdf document. +using (PdfDocument document = new PdfDocument()) { - document.Save(fileStream); -} -//Close the document to release resources. -document.Close(true); + //Add a new page to the PDF document. + PdfPage page = document.Pages.Add(); + //Create a digital signature and associate it with the added page. + PdfSignature signature = new PdfSignature(page, "Signature", new DateTime(2020, 12, 24, 10, 50, 10)); + //Set the timestamp server for the digital signature. + signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamp.digicert.com")); + //Set the signed name for the signature. + signature.SignedName = "Test"; + //Define the bounds for the signature (position and size on the page). + signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(200, 100)); + //Save the PDF document + document.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/Customized-revocation-validation/.NET/Customized-revocation-validation/Program.cs b/Digital Signature/Customized-revocation-validation/.NET/Customized-revocation-validation/Program.cs index 6d175c5a..82dd4b22 100644 --- a/Digital Signature/Customized-revocation-validation/.NET/Customized-revocation-validation/Program.cs +++ b/Digital Signature/Customized-revocation-validation/.NET/Customized-revocation-validation/Program.cs @@ -1,33 +1,24 @@ using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Gets the stream from the document -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Loads an existing signed PDF document -PdfLoadedDocument document = new PdfLoadedDocument(documentStream); - -// Gets the signature field -PdfLoadedSignatureField signatureField = document.Form.Fields[0] as PdfLoadedSignatureField; - -// Signature validation options -PdfSignatureValidationOptions options = new PdfSignatureValidationOptions(); - -// Sets the revocation type -options.RevocationValidationType = RevocationValidationType.Crl; - -// Validate signature and get validation result -PdfSignatureValidationResult result = signatureField.ValidateSignature(options); - -//Check whether the CRL is revoked -if (result.RevocationResult.IsRevokedCRL) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - Console.WriteLine("CRL is revoked"); -} -else -{ - Console.WriteLine("CRL is not revoked"); -} - -// Closes the document -document.Close(true); \ No newline at end of file + // Gets the signature field + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + // Signature validation options + PdfSignatureValidationOptions options = new PdfSignatureValidationOptions(); + // Sets the revocation type + options.RevocationValidationType = RevocationValidationType.Crl; + // Validate signature and get validation result + PdfSignatureValidationResult result = signatureField.ValidateSignature(options); + //Check whether the CRL is revoked + if (result.RevocationResult.IsRevokedCRL) + { + Console.WriteLine("CRL is revoked"); + } + else + { + Console.WriteLine("CRL is not revoked"); + } +} \ No newline at end of file diff --git a/Digital Signature/Deferred-signing-in-PDF-document/.NET/Deferred-signing-in-PDF-document/Program.cs b/Digital Signature/Deferred-signing-in-PDF-document/.NET/Deferred-signing-in-PDF-document/Program.cs index 0a9509e3..8e6c3bcd 100644 --- a/Digital Signature/Deferred-signing-in-PDF-document/.NET/Deferred-signing-in-PDF-document/Program.cs +++ b/Digital Signature/Deferred-signing-in-PDF-document/.NET/Deferred-signing-in-PDF-document/Program.cs @@ -1,6 +1,4 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Drawing; +using Syncfusion.Drawing; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography; @@ -20,57 +18,41 @@ static void Main(string[] args) static void CreateEmptySignedPDF() { - // Get the stream from the document. - FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Barcode.pdf"), FileMode.Open, FileAccess.Read); - //Load an existing PDF document. - PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); - + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Barcode.pdf")); //Creates a digital signature. PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); - //Sets the signature information. signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); signature.Settings.CryptographicStandard = CryptographicStandard.CADES; signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; - //Create an external signer. IPdfExternalSigner externalSignature = new SignEmpty("SHA1"); - //Add public certificates. System.Collections.Generic.List certificates = new System.Collections.Generic.List(); certificates.Add(new X509Certificate2(Convert.FromBase64String(PublicCert))); signature.AddExternalSigner(externalSignature, certificates, null); - //Save the document. - FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Output/EmptySignature.pdf"), FileMode.Create, FileAccess.ReadWrite); - loadedDocument.Save(inputFileStream); - + loadedDocument.Save(Path.GetFullPath(@"Output/EmptySignature.pdf")); //Close the PDF document. loadedDocument.Close(true); - inputFileStream.Close(); } static void DeferredSign() { //Create an external signer with a signed hash message. IPdfExternalSigner externalSigner = new ExternalSigner("SHA1", Program.SignedHash); - //Add public certificates. System.Collections.Generic.List publicCertificates = new System.Collections.Generic.List(); publicCertificates.Add(new X509Certificate2(Convert.FromBase64String(PublicCert))); - //Create an output file stream. FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/DeferredSign.pdf"), FileMode.Create, FileAccess.ReadWrite); - // Get the stream from the document FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Output/EmptySignature.pdf"), FileMode.Open, FileAccess.Read); - string pdfPassword = string.Empty; - //Replace an empty signature. PdfSignature.ReplaceEmptySignature(inputFileStream, pdfPassword, outputFileStream, "Signature", externalSigner, publicCertificates); - + inputFileStream.Close(); outputFileStream.Close(); } @@ -115,7 +97,7 @@ private void SignDocumentHash(byte[] documentHash) Program.SignedHash = rsa.SignData(documentHash, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); } else if (digitalID.PrivateKey is System.Security.Cryptography.RSAOpenSsl) - { + { System.Security.Cryptography.RSAOpenSsl rsa = (System.Security.Cryptography.RSAOpenSsl)digitalID.PrivateKey; Program.SignedHash = rsa.SignData(documentHash, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); } @@ -132,13 +114,11 @@ public string HashAlgorithm { get { return _hashAlgorithm; } } - public ExternalSigner(string hashAlgorithm, byte[] hash) { _hashAlgorithm = hashAlgorithm; _signedHash = hash; } - public byte[] Sign(byte[] message, out byte[] timeStampResponse) { //Set the signed hash message to replace an empty signature. @@ -147,4 +127,4 @@ public byte[] Sign(byte[] message, out byte[] timeStampResponse) return signedBytes; } } -} +} \ No newline at end of file diff --git a/Digital Signature/Draw-text-or-images-in-the-signature-appearance/.NET/Draw-text-or-images-in-the-signature-appearance/Program.cs b/Digital Signature/Draw-text-or-images-in-the-signature-appearance/.NET/Draw-text-or-images-in-the-signature-appearance/Program.cs index 84f5936b..c02ff42b 100644 --- a/Digital Signature/Draw-text-or-images-in-the-signature-appearance/.NET/Draw-text-or-images-in-the-signature-appearance/Program.cs +++ b/Digital Signature/Draw-text-or-images-in-the-signature-appearance/.NET/Draw-text-or-images-in-the-signature-appearance/Program.cs @@ -1,49 +1,31 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Drawing; +using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Security; -//Create a new PDF document. -PdfDocument document = new PdfDocument(); - -//Add a new page. -PdfPageBase page = document.Pages.Add(); - -//Create graphics for the page. -PdfGraphics graphics = page.Graphics; - -//Create a certificate instance from a PFX file with a private key. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion"); - -//Create a digital signature. -PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); - -//Set an image for signature field. -FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/signature.png"), FileMode.Open, FileAccess.Read); - -//Set an image for signature field. -PdfBitmap signatureImage = new PdfBitmap(imageStream); - -//Set the signature information. -signature.Bounds = new RectangleF(new PointF(0, 0), signatureImage.PhysicalDimension); -signature.ContactInfo = "johndoe@owned.us"; -signature.LocationInfo = "Honolulu, Hawaii"; -signature.Reason = "I am author of this document."; - -//Create appearance for the digital signature. -signature.Appearance.Normal.Graphics.DrawImage(signatureImage, signature.Bounds); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new pdf document. +using (PdfDocument document = new PdfDocument()) { - //Save the PDF document to file stream. - document.Save(outputFileStream); -} - -//Close the document. -document.Close(true); - - + //Add a new page. + PdfPageBase page = document.Pages.Add(); + //Create graphics for the page. + PdfGraphics graphics = page.Graphics; + //Create a certificate instance from a PFX file with a private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion"); + //Create a digital signature. + PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); + //Set an image for signature field. + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/signature.png"), FileMode.Open, FileAccess.Read); + //Set an image for signature field. + PdfBitmap signatureImage = new PdfBitmap(imageStream); + //Set the signature information. + signature.Bounds = new RectangleF(new PointF(0, 0), signatureImage.PhysicalDimension); + signature.ContactInfo = "johndoe@owned.us"; + signature.LocationInfo = "Honolulu, Hawaii"; + signature.Reason = "I am author of this document."; + //Create appearance for the digital signature. + signature.Appearance.Normal.Graphics.DrawImage(signatureImage, signature.Bounds); + //Save the PDF document + document.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs index 5b544e9f..997e903b 100644 --- a/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs +++ b/Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Program.cs @@ -9,41 +9,23 @@ internal class Program { static void Main(string[] args) { - //Open the Word document file stream. - using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/TestPDF.pdf"), FileMode.Open, FileAccess.Read)) + //Load the PDF document. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - - PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream); - - PdfLoadedPage ldPage = pdfLoadedDocument.Pages[3] as PdfLoadedPage; - + PdfLoadedPage ldPage = loadedDocument.Pages[3] as PdfLoadedPage; //Create a certificate instance from a PFX file with a private key. FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123"); - - PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1"); - + PdfSignature signature = new PdfSignature(loadedDocument, ldPage, pdfCert, "Signature1"); RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70)); - - signature.Bounds = GetRelativeBounds(ldPage, bounds); - PdfGraphics graphics = signature.Appearance.Normal.Graphics; - FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/TestImage.png"), FileMode.Open, FileAccess.Read); //Set an image for signature field. PdfBitmap signatureImage = new PdfBitmap(imageStream); - RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds); - - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) - { - //Save the PDF document to file stream. - pdfLoadedDocument.Save(outputFileStream); - } - //Closes the document - pdfLoadedDocument.Close(true); - + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); } } private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds) @@ -76,7 +58,6 @@ private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bound private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds) { graphics.Save(); - if (angle == PdfPageRotateAngle.RotateAngle90) { graphics.TranslateTransform(0, bounds.Height); @@ -102,5 +83,4 @@ private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphi graphics.Restore(); } -} - +} \ No newline at end of file diff --git a/Digital Signature/Enable-LTV-PDF-signature/.NET/Enable-LTV-PDF-signature/Program.cs b/Digital Signature/Enable-LTV-PDF-signature/.NET/Enable-LTV-PDF-signature/Program.cs index 4c6a88bb..132aa579 100644 --- a/Digital Signature/Enable-LTV-PDF-signature/.NET/Enable-LTV-PDF-signature/Program.cs +++ b/Digital Signature/Enable-LTV-PDF-signature/.NET/Enable-LTV-PDF-signature/Program.cs @@ -1,53 +1,35 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Drawing; +using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; //Creates a new PDF document. -PdfDocument document = new PdfDocument(); - -//Adds a new page. -PdfPageBase page = document.Pages.Add(); - -//Create graphics for the page. -PdfGraphics graphics = page.Graphics; - -//Creates a certificate instance from PFX file with private key. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate pdfCert = new PdfCertificate(certificateStream, "DigitalPass123"); - -//Creates a digital signature. -PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); -signature.Settings.CryptographicStandard = CryptographicStandard.CADES; -signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; - -//Save the document into stream -MemoryStream stream = new MemoryStream(); -document.Save(stream); -//Close the document -document.Close(true); - -//Load an existing PDF stream. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); - -//Gets the first signature field of the PDF document -PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; -PdfSignature pdfSignature = signatureField.Signature; - -//Enable LTV on Signature. -pdfSignature.EnableLtv = true; - - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfDocument document = new PdfDocument()) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} - -//Close the document. -loadedDocument.Close(true); - + //Adds a new page. + PdfPageBase page = document.Pages.Add(); + //Create graphics for the page. + PdfGraphics graphics = page.Graphics; + //Creates a certificate instance from PFX file with private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "DigitalPass123"); + //Creates a digital signature. + PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; + //Save the document into stream + MemoryStream stream = new MemoryStream(); + document.Save(stream); + //Load an existing PDF stream. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream)) + { + //Gets the first signature field of the PDF document + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + PdfSignature pdfSignature = signatureField.Signature; + //Enable LTV on Signature. + pdfSignature.EnableLtv = true; + //Save the PDF document. + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } +} \ No newline at end of file diff --git a/Digital Signature/Externally-sign-a-PDF-document/.NET/Externally-sign-a-PDF-document/Program.cs b/Digital Signature/Externally-sign-a-PDF-document/.NET/Externally-sign-a-PDF-document/Program.cs index 60d04bfb..c80dda3e 100644 --- a/Digital Signature/Externally-sign-a-PDF-document/.NET/Externally-sign-a-PDF-document/Program.cs +++ b/Digital Signature/Externally-sign-a-PDF-document/.NET/Externally-sign-a-PDF-document/Program.cs @@ -1,45 +1,30 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography.X509Certificates; -//Get the stream from the document. -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load the existing PDF document. -PdfLoadedDocument document = new PdfLoadedDocument(documentStream); - -//Creates a digital signature. -PdfSignature signature = new PdfSignature(document, document.Pages[0], null, "DigitalSignature"); -signature.ComputeHash += Signature_ComputeHash; - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Creates a digital signature. + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "DigitalSignature"); + signature.ComputeHash += Signature_ComputeHash; + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); } -//Close the document. -document.Close(true); - void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars) { //Get the document bytes. byte[] documentBytes = ars.Data; SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true); - //Compute the signature using the specified digital ID file and the password. X509Certificate2 certificate = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion"); - var cmsSigner = new CmsSigner(certificate); - + CmsSigner cmsSigner = new CmsSigner(certificate); //Set the digest algorithm SHA256. cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); signedCms.ComputeSignature(cmsSigner); - //Embed the encoded digital signature to the PDF document. ars.SignedData = signedCms.Encode(); } \ No newline at end of file diff --git a/Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document/Program.cs b/Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document/Program.cs index 5a6da695..e7734826 100644 --- a/Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document/Program.cs +++ b/Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document/Program.cs @@ -10,37 +10,25 @@ internal class Program { static void Main(string[] args) { - // Get the stream from the document - FileStream documentStream = new FileStream(Path.GetFullPath(@"../../../Data/Barcode.pdf"), FileMode.Open, FileAccess.Read); - - // Load the existing PDF document - PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); - - // Create a digital signature - PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); - - // Set the signature information - signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); - signature.Settings.CryptographicStandard = CryptographicStandard.CADES; - signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; - - // Create an external signer - IPdfExternalSigner externalSignature = new ExternalSigner("SHA1"); - - // Add public certificates - List certificates = new List(); - certificates.Add(new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123")); - signature.AddExternalSigner(externalSignature, certificates, null); - - // Create file stream - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + //Load the PDF document. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - // Save the PDF document to file stream - loadedDocument.Save(outputFileStream); - } + // Create a digital signature + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); + // Set the signature information + signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; + // Create an external signer + IPdfExternalSigner externalSignature = new ExternalSigner("SHA1"); + // Add public certificates + List certificates = new List(); + certificates.Add(new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123")); + signature.AddExternalSigner(externalSignature, certificates, null); - // Close the document - loadedDocument.Close(true); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } } // Create the external signer class and sign the document hash @@ -80,4 +68,4 @@ public byte[] Sign(byte[] message, out byte[] timeStampResponse) } } } -} +} \ No newline at end of file diff --git a/Digital Signature/Get-LTV-information/.NET/Get-LTV-information/Program.cs b/Digital Signature/Get-LTV-information/.NET/Get-LTV-information/Program.cs index 3dc56429..3fd01174 100644 --- a/Digital Signature/Get-LTV-information/.NET/Get-LTV-information/Program.cs +++ b/Digital Signature/Get-LTV-information/.NET/Get-LTV-information/Program.cs @@ -1,33 +1,22 @@ using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Gets the stream from the document -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Loads an existing signed PDF document -PdfLoadedDocument document = new PdfLoadedDocument(documentStream); - -// Gets the signature field -PdfLoadedSignatureField signatureField = document.Form.Fields[0] as PdfLoadedSignatureField; - -// Validates signature and get validation result -PdfSignatureValidationResult result = signatureField.ValidateSignature(); - -// Gets the LTV verification Information. -LtvVerificationInfo ltvVerificationInfo = result.LtvVerificationInfo; - -// Checks whether the signature document LTV is enabled. -bool isLtvEnabled = ltvVerificationInfo.IsLtvEnabled; - -// Checks whether the signature document has CRL embedded. -bool isCrlEmbedded = ltvVerificationInfo.IsCrlEmbedded; - -// Checks whether the signature document has OCSP embedded. -bool isOcspEmbedded = ltvVerificationInfo.IsOcspEmbedded; - -Console.WriteLine("LTV enabled: " + isLtvEnabled); -Console.WriteLine("CRL embedded: " + isCrlEmbedded); -Console.WriteLine("OCSP embedded: " + isOcspEmbedded); - -// Closes the document -document.Close(true); \ No newline at end of file +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) +{ + // Gets the signature field + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + // Validates signature and get validation result + PdfSignatureValidationResult result = signatureField.ValidateSignature(); + // Gets the LTV verification Information. + LtvVerificationInfo ltvVerificationInfo = result.LtvVerificationInfo; + // Checks whether the signature document LTV is enabled. + bool isLtvEnabled = ltvVerificationInfo.IsLtvEnabled; + // Checks whether the signature document has CRL embedded. + bool isCrlEmbedded = ltvVerificationInfo.IsCrlEmbedded; + // Checks whether the signature document has OCSP embedded. + bool isOcspEmbedded = ltvVerificationInfo.IsOcspEmbedded; + Console.WriteLine("LTV enabled: " + isLtvEnabled); + Console.WriteLine("CRL embedded: " + isCrlEmbedded); + Console.WriteLine("OCSP embedded: " + isOcspEmbedded); +} \ No newline at end of file diff --git a/Digital Signature/Get-images-from-the-existing-signed-signature-field/.NET/Get-images-from-the-existing-signed-signature-field/Program.cs b/Digital Signature/Get-images-from-the-existing-signed-signature-field/.NET/Get-images-from-the-existing-signed-signature-field/Program.cs index c085c06b..af024dc6 100644 --- a/Digital Signature/Get-images-from-the-existing-signed-signature-field/.NET/Get-images-from-the-existing-signed-signature-field/Program.cs +++ b/Digital Signature/Get-images-from-the-existing-signed-signature-field/.NET/Get-images-from-the-existing-signed-signature-field/Program.cs @@ -1,21 +1,15 @@ using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing; -namespace Get_images_from_the_existing_signed_signature_field { - internal class Program { - static void Main(string[] args) { - //Load an existing PDF file. - FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - PdfLoadedDocument ldoc = new PdfLoadedDocument(fileStream); - //Get the existing signed signature field. - PdfLoadedSignatureField loadedSignature = ldoc.Form.Fields[0] as PdfLoadedSignatureField; - //Get the image streams. - Stream[] imageStreams = loadedSignature.GetImages(); - for (int i = 0; i < imageStreams.Length; i++) { - File.WriteAllBytes(Path.GetFullPath(@"Output/" + i.ToString() + ".jpg"), (imageStreams[i] as MemoryStream).ToArray()); - } - //Close a PDF document. - ldoc.Close(true); - } +//Load an existing PDF file. +using (PdfLoadedDocument ldoc = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) +{ + //Get the existing signed signature field. + PdfLoadedSignatureField loadedSignature = ldoc.Form.Fields[0] as PdfLoadedSignatureField; + //Get the image streams. + Stream[] imageStreams = loadedSignature.GetImages(); + for (int i = 0; i < imageStreams.Length; i++) + { + File.WriteAllBytes(Path.GetFullPath(@"Output/" + i.ToString() + ".jpg"), (imageStreams[i] as MemoryStream).ToArray()); } } \ No newline at end of file diff --git a/Digital Signature/Get-number-of-digital-signatures/.NET/Get-number-of-digital-signatures/Program.cs b/Digital Signature/Get-number-of-digital-signatures/.NET/Get-number-of-digital-signatures/Program.cs index 35b0684b..e18d4fa9 100644 --- a/Digital Signature/Get-number-of-digital-signatures/.NET/Get-number-of-digital-signatures/Program.cs +++ b/Digital Signature/Get-number-of-digital-signatures/.NET/Get-number-of-digital-signatures/Program.cs @@ -4,35 +4,29 @@ int signedFieldsCount = 0; int unSignedFieldsCount = 0; -// Open the PDF document as a file stream -using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - // Load the PDF document from the file stream - using (PdfLoadedDocument doc = new PdfLoadedDocument(docStream)) + // Access the form inside the loaded PDF document + PdfLoadedForm form = loadedDocument.Form; + // Iterate through each field in the form field collection + foreach (PdfLoadedField field in form.Fields) { - // Access the form inside the loaded PDF document - PdfLoadedForm form = doc.Form; - - // Iterate through each field in the form field collection - foreach (PdfLoadedField field in form.Fields) + PdfLoadedSignatureField sigField = field as PdfLoadedSignatureField; + // Check whether the signature field is signed or not + if (sigField != null) { - PdfLoadedSignatureField sigField = field as PdfLoadedSignatureField; - // Check whether the signature field is signed or not - if (sigField != null) + if (sigField.IsSigned) + { + signedFieldsCount++; + } + else { - if (sigField.IsSigned) - { - signedFieldsCount++; - } - else - { - unSignedFieldsCount++; - } + unSignedFieldsCount++; } } - - // Display the total number of signature fields found in the PDF document - Console.WriteLine("The number of signed signature fields in the PDF document: " + signedFieldsCount); - Console.WriteLine("The number of unSigned signature fields in the PDF document: " + unSignedFieldsCount); } + // Display the total number of signature fields found in the PDF document + Console.WriteLine("The number of signed signature fields in the PDF document: " + signedFieldsCount); + Console.WriteLine("The number of unSigned signature fields in the PDF document: " + unSignedFieldsCount); } \ No newline at end of file diff --git a/Digital Signature/Implementing-PdfFormFieldVisibility-types/.NET/Implementing-PdfFormFieldVisibility-types/Program.cs b/Digital Signature/Implementing-PdfFormFieldVisibility-types/.NET/Implementing-PdfFormFieldVisibility-types/Program.cs index 99ae7ccd..d76ec9b8 100644 --- a/Digital Signature/Implementing-PdfFormFieldVisibility-types/.NET/Implementing-PdfFormFieldVisibility-types/Program.cs +++ b/Digital Signature/Implementing-PdfFormFieldVisibility-types/.NET/Implementing-PdfFormFieldVisibility-types/Program.cs @@ -3,30 +3,27 @@ using Syncfusion.Pdf; using Syncfusion.Drawing; -// Create a new PDF document -PdfDocument document = new PdfDocument(); -// Add a new PDF page -PdfPage page = document.Pages.Add(); -//Create font -PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12f); -//Draw the label text -page.Graphics.DrawString("First Name", font, PdfBrushes.Black, 10, 55); -//Create a text box -PdfTextBoxField firstNameTextBox = new PdfTextBoxField(page, "FirstNameTextBox"); -//Set bounds of the text box. -firstNameTextBox.Bounds = new RectangleF(100, 20, 200, 20); -//Set font -firstNameTextBox.Font = font; -//Set the text box default text -firstNameTextBox.Text = "John"; -//Set the text box field visibility. -firstNameTextBox.Visibility = PdfFormFieldVisibility.Visible; -//Add the textbox to the document -document.Form.Fields.Add(firstNameTextBox); -//Save the document. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new pdf document. +using (PdfDocument document = new PdfDocument()) { - document.Save(outputFileStream); -} -//Close the document -document.Close(true); \ No newline at end of file + // Add a new PDF page + PdfPage page = document.Pages.Add(); + //Create font + PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12f); + //Draw the label text + page.Graphics.DrawString("First Name", font, PdfBrushes.Black, 10, 55); + //Create a text box + PdfTextBoxField firstNameTextBox = new PdfTextBoxField(page, "FirstNameTextBox"); + //Set bounds of the text box. + firstNameTextBox.Bounds = new RectangleF(100, 20, 200, 20); + //Set font + firstNameTextBox.Font = font; + //Set the text box default text + firstNameTextBox.Text = "John"; + //Set the text box field visibility. + firstNameTextBox.Visibility = PdfFormFieldVisibility.Visible; + //Add the textbox to the document + document.Form.Fields.Add(firstNameTextBox); + //Save the PDF document + document.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/Multiple-digital-signature/.NET/Multiple-digital-signature/Program.cs b/Digital Signature/Multiple-digital-signature/.NET/Multiple-digital-signature/Program.cs index 56b99bdf..85a4ce1a 100644 --- a/Digital Signature/Multiple-digital-signature/.NET/Multiple-digital-signature/Program.cs +++ b/Digital Signature/Multiple-digital-signature/.NET/Multiple-digital-signature/Program.cs @@ -1,66 +1,50 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using Syncfusion.Pdf; //Load an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/SignatureFields.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Get the first page of the document. -PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; - -//Get the first signature field of the PDF document. -PdfLoadedSignatureField signatureField1 = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; - -//Create a certificate instance from a PFX file with a private key. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate certificate1 = new PdfCertificate(certificateStream, "syncfusion"); - -//Add a signature to the signature field. -signatureField1.Signature = new PdfSignature(loadedDocument, page, certificate1, "Signature", signatureField1); - -//Set an image for the signature field. -FileStream imageStream1 = new FileStream(Path.GetFullPath(@"Data/Student Signature.jpg"), FileMode.Open, FileAccess.Read); -PdfBitmap signatureImage = new PdfBitmap(imageStream1); - -//Insert an image in the signature appearance. -signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, 90, 20); - -//Save the document into the stream. -MemoryStream stream = new MemoryStream(); -loadedDocument.Save(stream); -//Close the document. -loadedDocument.Close(true); - -//Load the signed PDF document. -PdfLoadedDocument signedDocument = new PdfLoadedDocument(stream); - -//Load the PDF page. -PdfLoadedPage loadedPage = signedDocument.Pages[0] as PdfLoadedPage; - -//Get the first signature field of the PDF document. -PdfLoadedSignatureField signatureField2 = signedDocument.Form.Fields[1] as PdfLoadedSignatureField; - -//Add a signature to the signature field. -signatureField2.Signature = new PdfSignature(signedDocument, loadedPage, certificate1, "Signature", signatureField2); - -//Set an image for the signature field. -FileStream imageStream2 = new FileStream(Path.GetFullPath(@"Data/Teacher Signature.png"), FileMode.Open, FileAccess.Read); -PdfBitmap signatureImage1 = new PdfBitmap(imageStream2); - -//Draw an image in the signature appearance. -signatureField2.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage1, 0, 0, 90, 20); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/SignatureFields.pdf"))) { - //Save the PDF document to file stream. - signedDocument.Save(outputFileStream); -} -//Close the document. -signedDocument.Close(true); -loadedDocument.Close(true); -stream.Dispose(); \ No newline at end of file + + //Get the first page of the document. + PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; + //Get the first signature field of the PDF document. + PdfLoadedSignatureField signatureField1 = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Create a certificate instance from a PFX file with a private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate certificate1 = new PdfCertificate(certificateStream, "syncfusion"); + //Add a signature to the signature field. + signatureField1.Signature = new PdfSignature(loadedDocument, page, certificate1, "Signature", signatureField1); + //Set an image for the signature field. + FileStream imageStream1 = new FileStream(Path.GetFullPath(@"Data/Student Signature.jpg"), FileMode.Open, FileAccess.Read); + PdfBitmap signatureImage = new PdfBitmap(imageStream1); + //Insert an image in the signature appearance. + signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, 90, 20); + //Save the document into the stream. + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + + //Load the signed PDF document. + using (PdfLoadedDocument signedDocument = new PdfLoadedDocument(stream)) + { + //Load the PDF page. + PdfLoadedPage loadedPage = signedDocument.Pages[0] as PdfLoadedPage; + + //Get the first signature field of the PDF document. + PdfLoadedSignatureField signatureField2 = signedDocument.Form.Fields[1] as PdfLoadedSignatureField; + + //Add a signature to the signature field. + signatureField2.Signature = new PdfSignature(signedDocument, loadedPage, certificate1, "Signature", signatureField2); + + //Set an image for the signature field. + FileStream imageStream2 = new FileStream(Path.GetFullPath(@"Data/Teacher Signature.png"), FileMode.Open, FileAccess.Read); + PdfBitmap signatureImage1 = new PdfBitmap(imageStream2); + + //Draw an image in the signature appearance. + signatureField2.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage1, 0, 0, 90, 20); + + //Save the PDF document + signedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } +} \ No newline at end of file diff --git a/Digital Signature/Remove_existing_digital_signature_from_PDF/.NET/Remove_existing_digital_signature_from_PDF/Program.cs b/Digital Signature/Remove_existing_digital_signature_from_PDF/.NET/Remove_existing_digital_signature_from_PDF/Program.cs index 20555100..ddcb4889 100644 --- a/Digital Signature/Remove_existing_digital_signature_from_PDF/.NET/Remove_existing_digital_signature_from_PDF/Program.cs +++ b/Digital Signature/Remove_existing_digital_signature_from_PDF/.NET/Remove_existing_digital_signature_from_PDF/Program.cs @@ -1,23 +1,12 @@ -// See https://aka.ms/new-console-template for more information +using Syncfusion.Pdf.Parsing; -using Syncfusion.Pdf.Parsing; - -//Load an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/SignedPDF.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(docStream); - -//Get the signature field from PDF form field collection. -PdfLoadedSignatureField signatureField = pdfLoadedDocument.Form.Fields[0] as PdfLoadedSignatureField; - -//Remove signature field from form field collection. -pdfLoadedDocument.Form.Fields.Remove(signatureField); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - pdfLoadedDocument.Save(outputFileStream); -} - -//Close the document. -pdfLoadedDocument.Close(true); + //Get the signature field from PDF form field collection. + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Remove signature field from form field collection. + loadedDocument.Form.Fields.Remove(signatureField); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/Retrieve-certificate-details-from-an-existing-PDF/.NET/Retrieve-certificate-details-from-an-existing-PDF/Program.cs b/Digital Signature/Retrieve-certificate-details-from-an-existing-PDF/.NET/Retrieve-certificate-details-from-an-existing-PDF/Program.cs index 2334bda8..226e2c35 100644 --- a/Digital Signature/Retrieve-certificate-details-from-an-existing-PDF/.NET/Retrieve-certificate-details-from-an-existing-PDF/Program.cs +++ b/Digital Signature/Retrieve-certificate-details-from-an-existing-PDF/.NET/Retrieve-certificate-details-from-an-existing-PDF/Program.cs @@ -1,44 +1,28 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load an existing PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Get the signature field. -PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; - -//Get the certificate. -PdfCertificate certificate = signatureField.Signature.Certificate; - -//Get the signed date. -DateTime date = signatureField.Signature.SignedDate; - -//Get the signed name. -string name = signatureField.Signature.SignedName; - -//Gets the certificate subject's name. -string subjectName = certificate.SubjectName; - -//Gets the certificate issuer's name. -string issuerName = certificate.IssuerName; - -//Get certificate validation date information. -DateTime validFrom = certificate.ValidFrom; -DateTime validTo = certificate.ValidTo; - -//Close the document. -loadedDocument.Close(true); - -//Write the digital signature details in console window. -Console.WriteLine("Signed Name: " + name); -Console.WriteLine("Certificate subject name: " + subjectName); -Console.WriteLine("Certificate issuer's name: " + issuerName); -Console.WriteLine("Certificate validation from: " + validFrom); -Console.WriteLine("Certificate valid to:" + validTo); - -Console.ReadLine(); \ No newline at end of file +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) +{ + //Get the signature field. + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Get the certificate. + PdfCertificate certificate = signatureField.Signature.Certificate; + //Get the signed date. + DateTime date = signatureField.Signature.SignedDate; + //Get the signed name. + string name = signatureField.Signature.SignedName; + //Gets the certificate subject's name. + string subjectName = certificate.SubjectName; + //Gets the certificate issuer's name. + string issuerName = certificate.IssuerName; + //Get certificate validation date information. + DateTime validFrom = certificate.ValidFrom; + DateTime validTo = certificate.ValidTo; + //Write the digital signature details in console window. + Console.WriteLine("Signed Name: " + name); + Console.WriteLine("Certificate subject name: " + subjectName); + Console.WriteLine("Certificate issuer's name: " + issuerName); + Console.WriteLine("Certificate validation from: " + validFrom); + Console.WriteLine("Certificate valid to:" + validTo); +} \ No newline at end of file diff --git a/Digital Signature/Retrieve-digital-signature-information-from-PDF/.NET/Retrieve-digital-signature-information-from-PDF/Program.cs b/Digital Signature/Retrieve-digital-signature-information-from-PDF/.NET/Retrieve-digital-signature-information-from-PDF/Program.cs index 076b410f..787f90b3 100644 --- a/Digital Signature/Retrieve-digital-signature-information-from-PDF/.NET/Retrieve-digital-signature-information-from-PDF/Program.cs +++ b/Digital Signature/Retrieve-digital-signature-information-from-PDF/.NET/Retrieve-digital-signature-information-from-PDF/Program.cs @@ -1,22 +1,16 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/SignedPDF.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Get the signature field from PdfLoadedDocument form field collection. -PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; -PdfSignature signature = signatureField.Signature; - -//Extract the signature information. -Console.WriteLine("Digitally Signed by: " + signature.Certificate.IssuerName); -Console.WriteLine("Valid From: " + signature.Certificate.ValidFrom); -Console.WriteLine("Valid To: " + signature.Certificate.ValidTo); -Console.WriteLine("Hash Algorithm : " + signature.Settings.DigestAlgorithm); -Console.WriteLine("Cryptographics Standard : " + signature.Settings.CryptographicStandard); - -//Close the document. -loadedDocument.Close(true); +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) +{ + //Get the signature field from PdfLoadedDocument form field collection. + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + PdfSignature signature = signatureField.Signature; + //Extract the signature information. + Console.WriteLine("Digitally Signed by: " + signature.Certificate.IssuerName); + Console.WriteLine("Valid From: " + signature.Certificate.ValidFrom); + Console.WriteLine("Valid To: " + signature.Certificate.ValidTo); + Console.WriteLine("Hash Algorithm : " + signature.Settings.DigestAlgorithm); + Console.WriteLine("Cryptographics Standard : " + signature.Settings.CryptographicStandard); +} diff --git a/Digital Signature/Retrieve-revocation-certificate-information-from-digital-signature-embed-timestamp/.NET/Program.cs b/Digital Signature/Retrieve-revocation-certificate-information-from-digital-signature-embed-timestamp/.NET/Program.cs index a2e67cba..f7e9adbf 100644 --- a/Digital Signature/Retrieve-revocation-certificate-information-from-digital-signature-embed-timestamp/.NET/Program.cs +++ b/Digital Signature/Retrieve-revocation-certificate-information-from-digital-signature-embed-timestamp/.NET/Program.cs @@ -1,50 +1,48 @@ -// See https://aka.ms/new-console-template for more information -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates; -//Load a PDF document -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument document = new PdfLoadedDocument(docStream); -//Gets the signature field. -PdfLoadedSignatureField signatureField = document.Form.Fields[0] as PdfLoadedSignatureField; -//Validates signature and gets the validation result. -PdfSignatureValidationResult result = signatureField.ValidateSignature(); -//Gets signer certificates -PdfSignerCertificate[] certifcate = result.TimeStampInformation.SignerCertificates; -//Print the certifcate value -foreach (PdfSignerCertificate signerCertificate in certifcate) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - if (signerCertificate.OcspCertificate != null) + //Gets the signature field. + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Validates signature and gets the validation result. + PdfSignatureValidationResult result = signatureField.ValidateSignature(); + //Gets signer certificates + PdfSignerCertificate[] certifcate = result.TimeStampInformation.SignerCertificates; + //Print the certifcate value + foreach (PdfSignerCertificate signerCertificate in certifcate) { - Console.WriteLine("------------OCSP Certificate-------------"); - Console.WriteLine(); - foreach (X509Certificate2 item in signerCertificate.OcspCertificate.Certificates) + if (signerCertificate.OcspCertificate != null) { - Console.WriteLine("The OCSP Response was signed by " + item.SubjectName.Name); + Console.WriteLine("------------OCSP Certificate-------------"); + Console.WriteLine(); + foreach (X509Certificate2 item in signerCertificate.OcspCertificate.Certificates) + { + Console.WriteLine("The OCSP Response was signed by " + item.SubjectName.Name); + } + Console.WriteLine("Is Embedded: " + signerCertificate.OcspCertificate.IsEmbedded); + Console.WriteLine("ValidFrom: " + signerCertificate.OcspCertificate.ValidFrom); + Console.WriteLine("ValidTo: " + signerCertificate.OcspCertificate.ValidTo); + Console.WriteLine(); + continue; } - Console.WriteLine("Is Embedded: " + signerCertificate.OcspCertificate.IsEmbedded); - Console.WriteLine("ValidFrom: " + signerCertificate.OcspCertificate.ValidFrom); - Console.WriteLine("ValidTo: " + signerCertificate.OcspCertificate.ValidTo); - Console.WriteLine(); - continue; - } - if (signerCertificate.CrlCertificate != null) - { - Console.WriteLine("------------CRL Certificate--------------"); - Console.WriteLine(); - foreach (X509Certificate2 item in signerCertificate.CrlCertificate.Certificates) + if (signerCertificate.CrlCertificate != null) { - if (item != null) + Console.WriteLine("------------CRL Certificate--------------"); + Console.WriteLine(); + foreach (X509Certificate2 item in signerCertificate.CrlCertificate.Certificates) { - Console.WriteLine("The CRL was signed by " + item.SubjectName.Name); + if (item != null) + { + Console.WriteLine("The CRL was signed by " + item.SubjectName.Name); + } } + Console.WriteLine("Is Embedded: " + signerCertificate.CrlCertificate.IsEmbedded); + Console.WriteLine("ValidFrom: " + signerCertificate.CrlCertificate.ValidFrom); + Console.WriteLine("ValidTo: " + signerCertificate.CrlCertificate.ValidTo); + break; } - Console.WriteLine("Is Embedded: " + signerCertificate.CrlCertificate.IsEmbedded); - Console.WriteLine("ValidFrom: " + signerCertificate.CrlCertificate.ValidFrom); - Console.WriteLine("ValidTo: " + signerCertificate.CrlCertificate.ValidTo); - break; } -} -//Close the document. -document.Close(true); +} \ No newline at end of file diff --git a/Digital Signature/Retrieve-revocation-certificate-information/.NET/Retrieve-revocation-certificate-information/Program.cs b/Digital Signature/Retrieve-revocation-certificate-information/.NET/Retrieve-revocation-certificate-information/Program.cs index 05cf6bbc..a6c57185 100644 --- a/Digital Signature/Retrieve-revocation-certificate-information/.NET/Retrieve-revocation-certificate-information/Program.cs +++ b/Digital Signature/Retrieve-revocation-certificate-information/.NET/Retrieve-revocation-certificate-information/Program.cs @@ -1,54 +1,47 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates; -//Gets the stream from the document -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/DigitalSignature.pdf"), FileMode.Open, FileAccess.Read); -//Load an existing signed PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); - -//Get signature field. -PdfLoadedSignatureField loadedSignatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; -//X509Certificate2Collection to check the signer's identity using root certificates. -X509CertificateCollection collection = new X509CertificateCollection(); -//Create new X509Certificate2 with the root certificate. -X509Certificate2 certificate = new X509Certificate2(Path.GetFullPath(@"Data/Root.cer")); -//Add the certificate to the collection. -collection.Add(certificate); -//Create new X509Certificate2 with the intermediate certificate. -certificate = new X509Certificate2(Path.GetFullPath(@"Data/Intermediate0.cer")); -//Add the certificate to the collection. -collection.Add(certificate); -//Validate signature and get the validation result -PdfSignatureValidationResult result = loadedSignatureField.ValidateSignature(collection); - -foreach (PdfSignerCertificate signerCertificate in result.SignerCertificates) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - if (signerCertificate.OcspCertificate != null) + //Get signature field. + PdfLoadedSignatureField loadedSignatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //X509Certificate2Collection to check the signer's identity using root certificates. + X509CertificateCollection collection = new X509CertificateCollection(); + //Create new X509Certificate2 with the root certificate. + X509Certificate2 certificate = new X509Certificate2(Path.GetFullPath(@"Data/Root.cer")); + //Add the certificate to the collection. + collection.Add(certificate); + //Create new X509Certificate2 with the intermediate certificate. + certificate = new X509Certificate2(Path.GetFullPath(@"Data/Intermediate0.cer")); + //Add the certificate to the collection. + collection.Add(certificate); + //Validate signature and get the validation result + PdfSignatureValidationResult result = loadedSignatureField.ValidateSignature(collection); + foreach (PdfSignerCertificate signerCertificate in result.SignerCertificates) { - foreach (X509Certificate2 item in signerCertificate.OcspCertificate.Certificates) + if (signerCertificate.OcspCertificate != null) { - string subjectName = "The OCSP Response was signed by " + item.SubjectName.Name; - Console.WriteLine(subjectName); + foreach (X509Certificate2 item in signerCertificate.OcspCertificate.Certificates) + { + string subjectName = "The OCSP Response was signed by " + item.SubjectName.Name; + Console.WriteLine(subjectName); + } + bool isEmbbed = signerCertificate.OcspCertificate.IsEmbedded; + DateTime validForm = signerCertificate.OcspCertificate.ValidFrom; + DateTime validTo = signerCertificate.OcspCertificate.ValidTo; } - bool isEmbbed = signerCertificate.OcspCertificate.IsEmbedded; - DateTime validForm = signerCertificate.OcspCertificate.ValidFrom; - DateTime validTo = signerCertificate.OcspCertificate.ValidTo; - } - if (signerCertificate.CrlCertificate != null) - { - foreach (X509Certificate2 item in signerCertificate.CrlCertificate.Certificates) + if (signerCertificate.CrlCertificate != null) { - string subjectName = "The CRL was signed by " + item.SubjectName.Name; - Console.WriteLine(subjectName); + foreach (X509Certificate2 item in signerCertificate.CrlCertificate.Certificates) + { + string subjectName = "The CRL was signed by " + item.SubjectName.Name; + Console.WriteLine(subjectName); + } + bool isEmbbed = signerCertificate.CrlCertificate.IsEmbedded; + DateTime validForm = signerCertificate.CrlCertificate.ValidFrom; + DateTime validTo = signerCertificate.CrlCertificate.ValidTo; } - bool isEmbbed = signerCertificate.CrlCertificate.IsEmbedded; - DateTime validForm = signerCertificate.CrlCertificate.ValidFrom; - DateTime validTo = signerCertificate.CrlCertificate.ValidTo; } -} - -//Close the document -loadedDocument.Close(true); +} \ No newline at end of file diff --git a/Digital Signature/Retrieve-signed-revision-information/.NET/Retrieve-signed-revision-information/Program.cs b/Digital Signature/Retrieve-signed-revision-information/.NET/Retrieve-signed-revision-information/Program.cs index cd0fe35a..bf94a393 100644 --- a/Digital Signature/Retrieve-signed-revision-information/.NET/Retrieve-signed-revision-information/Program.cs +++ b/Digital Signature/Retrieve-signed-revision-information/.NET/Retrieve-signed-revision-information/Program.cs @@ -1,26 +1,18 @@ -// See https://aka.ms/new-console-template for more information +using Syncfusion.Pdf.Parsing; -using Syncfusion.Pdf.Parsing; - -//Loads an existing document -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument document = new PdfLoadedDocument(docStream); - -//Gets an array of revisions. -PdfRevision[] revisions = document.Revisions; -foreach (PdfRevision rev in revisions) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Get revision start position. - long startPosition = rev.StartPosition; -} - -//Load the existing signature field. -PdfLoadedSignatureField loadedSignatureField = document.Form.Fields[0] as PdfLoadedSignatureField; -//Get the revision of the signature. -int revisionIndex = loadedSignatureField.Revision; - -//Close the document. -document.Close(true); - -Console.WriteLine("Revison index of the signature field is " + revisionIndex); -Console.ReadLine(); + //Gets an array of revisions. + PdfRevision[] revisions = loadedDocument.Revisions; + foreach (PdfRevision rev in revisions) + { + //Get revision start position. + long startPosition = rev.StartPosition; + } + //Load the existing signature field. + PdfLoadedSignatureField loadedSignatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Get the revision of the signature. + int revisionIndex = loadedSignatureField.Revision; + Console.WriteLine("Revison index of the signature field is " + revisionIndex); +} \ No newline at end of file diff --git a/Digital Signature/Sign-PDF-without-showing-digital-signature/.NET/Sign-PDF-without-showing-digital-signature/Program.cs b/Digital Signature/Sign-PDF-without-showing-digital-signature/.NET/Sign-PDF-without-showing-digital-signature/Program.cs index 768bab65..f03a0a92 100644 --- a/Digital Signature/Sign-PDF-without-showing-digital-signature/.NET/Sign-PDF-without-showing-digital-signature/Program.cs +++ b/Digital Signature/Sign-PDF-without-showing-digital-signature/.NET/Sign-PDF-without-showing-digital-signature/Program.cs @@ -1,33 +1,20 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Load digital ID with password. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate certificate = new PdfCertificate(certificateStream, "syncfusion"); - -//Create a signature with loaded digital ID. -PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); -signature.Settings.CryptographicStandard = CryptographicStandard.CADES; -signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; - -//This property enables the author or certifying signature. -signature.Certificated = true; - -//Allow the form fill and and comments. -signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.AllowComments; - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} - -//Close the document. -loadedDocument.Close(true); \ No newline at end of file + //Load digital ID with password. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate certificate = new PdfCertificate(certificateStream, "syncfusion"); + //Create a signature with loaded digital ID. + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; + //This property enables the author or certifying signature. + signature.Certificated = true; + //Allow the form fill and and comments. + signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.AllowComments; + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/Sign_PDF_Windows_Certificate/.NET/Sign_PDF_Windows_Certificate/Program.cs b/Digital Signature/Sign_PDF_Windows_Certificate/.NET/Sign_PDF_Windows_Certificate/Program.cs index c224087f..6e7072b5 100644 --- a/Digital Signature/Sign_PDF_Windows_Certificate/.NET/Sign_PDF_Windows_Certificate/Program.cs +++ b/Digital Signature/Sign_PDF_Windows_Certificate/.NET/Sign_PDF_Windows_Certificate/Program.cs @@ -1,5 +1,4 @@ -//Load existing PDF document. -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using Syncfusion.Pdf; using System.Security.Cryptography.X509Certificates; @@ -13,23 +12,16 @@ X509Certificate2 digitalID = fcollection[0]; //Load existing PDF document. -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); -//Load X509Certificate2. -PdfCertificate certificate = new PdfCertificate(digitalID); - -//Create a Revision 2 signature with loaded digital ID. -PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); -//Changing the digital signature standard and hashing algorithm. -signature.Settings.CryptographicStandard = CryptographicStandard.CADES; -signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512; - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} + //Load X509Certificate2. + PdfCertificate certificate = new PdfCertificate(digitalID); -//Close the document. -loadedDocument.Close(true); + //Create a Revision 2 signature with loaded digital ID. + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); + //Changing the digital signature standard and hashing algorithm. + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512; + //Save the PDF document. + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); +} diff --git a/Digital Signature/Sign_PDF_with_LTA/.NET/Sign_PDF_with_LTA/Program.cs b/Digital Signature/Sign_PDF_with_LTA/.NET/Sign_PDF_with_LTA/Program.cs index f5a3a66b..9ea06286 100644 --- a/Digital Signature/Sign_PDF_with_LTA/.NET/Sign_PDF_with_LTA/Program.cs +++ b/Digital Signature/Sign_PDF_with_LTA/.NET/Sign_PDF_with_LTA/Program.cs @@ -2,45 +2,32 @@ using Syncfusion.Pdf.Security; using Syncfusion.Pdf; -FileStream documentStream1 = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream1); -//Load digital ID with password. -FileStream documentStream2 = new FileStream(Path.GetFullPath(@"Data/DigitalSignatureTest.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate certificate = new PdfCertificate(documentStream2, "DigitalPass123"); - -//Create a signature with loaded digital ID. -PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); -signature.Settings.CryptographicStandard = CryptographicStandard.CADES; -signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; -signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl")); -//Enable LTV document. -signature.EnableLtv = true; - -//Save the PDF document. -MemoryStream stream = new MemoryStream(); -loadedDocument.Save(stream); -//Close the document. -loadedDocument.Close(true); - -//Load existing PDF document. -PdfLoadedDocument ltDocument = new PdfLoadedDocument(stream); -//Load the existing PDF page. -PdfLoadedPage lpage = ltDocument.Pages[0] as PdfLoadedPage; - -//Create PDF signature with empty certificate. -PdfSignature timeStamp = new PdfSignature(lpage, "timestamp"); -timeStamp.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl")); - -//Save and close the PDF document -MemoryStream stream1 = new MemoryStream(); -ltDocument.Save(stream1); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Load the PDF document +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - ltDocument.Save(outputFileStream); -} - -//Close the document. -ltDocument.Close(true); \ No newline at end of file + //Load digital ID with password. + FileStream documentStream2 = new FileStream(Path.GetFullPath(@"Data/DigitalSignatureTest.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate certificate = new PdfCertificate(documentStream2, "DigitalPass123"); + //Create a signature with loaded digital ID. + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], certificate, "DigitalSignature"); + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256; + signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl")); + //Enable LTV document. + signature.EnableLtv = true; + //Save the PDF document. + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + + //Load existing PDF document. + using (PdfLoadedDocument ltDocument = new PdfLoadedDocument(stream)) + { + //Load the existing PDF page. + PdfLoadedPage lpage = ltDocument.Pages[0] as PdfLoadedPage; + //Create PDF signature with empty certificate. + PdfSignature timeStamp = new PdfSignature(lpage, "timestamp"); + timeStamp.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl")); + //Save the PDF document. + ltDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } +} \ No newline at end of file diff --git a/Digital Signature/Signing-an-existing-PDF-document/.NET/Signing-an-existing-PDF-document/Program.cs b/Digital Signature/Signing-an-existing-PDF-document/.NET/Signing-an-existing-PDF-document/Program.cs index 2bcfc552..52ab0e6d 100644 --- a/Digital Signature/Signing-an-existing-PDF-document/.NET/Signing-an-existing-PDF-document/Program.cs +++ b/Digital Signature/Signing-an-existing-PDF-document/.NET/Signing-an-existing-PDF-document/Program.cs @@ -1,32 +1,19 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf; +using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; //Load the PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Gets the first page of the document. -PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; - -//Gets the first signature field of the PDF document. -PdfLoadedSignatureField field = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; - -//Creates a certificate. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -PdfCertificate certificate = new PdfCertificate(certificateStream, "syncfusion"); - -//Add digital signature to signature field. -field.Signature = new PdfSignature(loadedDocument, page, certificate, "Signature", field); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} - -//Close the document. -loadedDocument.Close(true); \ No newline at end of file + //Gets the first page of the document. + PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; + //Gets the first signature field of the PDF document. + PdfLoadedSignatureField field = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + //Creates a certificate. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate certificate = new PdfCertificate(certificateStream, "syncfusion"); + //Add digital signature to signature field. + field.Signature = new PdfSignature(loadedDocument, page, certificate, "Signature", field); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); +} \ No newline at end of file diff --git a/Digital Signature/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Program.cs b/Digital Signature/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Program.cs index 9f419464..8214caad 100644 --- a/Digital Signature/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Program.cs +++ b/Digital Signature/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Program.cs @@ -14,34 +14,24 @@ internal class Program { static void Main(string[] args) { - //Get the stream from the document - FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Barcode.pdf"), FileMode.Open, FileAccess.Read); - //Load the existing PDF document - PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); - - //Creates a digital signature. - PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); - //Sets the signature information. - signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); - signature.Settings.CryptographicStandard = CryptographicStandard.CADES; - signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; - - //Create an external signer. - IPdfExternalSigner externalSignature = new ExternalSigner("SHA1"); - - //Add public certificates. - List certificates = new List(); - certificates.Add(new X509Certificate2(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123"))); - signature.AddExternalSigner(externalSignature, certificates, null); - - //Create file stream. - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + //Load the PDF document. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Barcode.pdf"))) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); + //Creates a digital signature. + PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); + //Sets the signature information. + signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); + signature.Settings.CryptographicStandard = CryptographicStandard.CADES; + signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; + //Create an external signer. + IPdfExternalSigner externalSignature = new ExternalSigner("SHA1"); + //Add public certificates. + List certificates = new List(); + certificates.Add(new X509Certificate2(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123"))); + signature.AddExternalSigner(externalSignature, certificates, null); + //Save the PDF document + loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); } - //Close the document. - loadedDocument.Close(true); } //Create the external signer class and sign the document hash. class ExternalSigner : IPdfExternalSigner diff --git a/Digital Signature/Validate-all-signatures-in-digitally-signed-PDF/.NET/Validate-all-signatures-in-digitally-signed-PDF/Program.cs b/Digital Signature/Validate-all-signatures-in-digitally-signed-PDF/.NET/Validate-all-signatures-in-digitally-signed-PDF/Program.cs index 4facbdaa..1e56db43 100644 --- a/Digital Signature/Validate-all-signatures-in-digitally-signed-PDF/.NET/Validate-all-signatures-in-digitally-signed-PDF/Program.cs +++ b/Digital Signature/Validate-all-signatures-in-digitally-signed-PDF/.NET/Validate-all-signatures-in-digitally-signed-PDF/Program.cs @@ -1,36 +1,22 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates; -//Get the stream from the document. -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load an existing signed PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); - -//X509Certificate2Collection to check the signer's identity using root certificates. -X509Certificate2Collection collection = new X509Certificate2Collection(); - -//Creates a certificate instance from PFX file with private key. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -byte[] data = new byte[certificateStream.Length]; -certificateStream.Read(data, 0, data.Length); - -//Create new X509Certificate2 with the root certificate. -X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); - -//Add the certificate to the collection. -collection.Add(certificate); - -//Validate all signatures in loaded PDF document and get the list of validation result. -List results; -bool isValid = loadedDocument.Form.Fields.ValidateSignatures(collection, out results); - -Console.WriteLine("All signatures in the document are valid: " + isValid); - -//Close the document. -loadedDocument.Close(true); - - +//Load the PDF document. +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) +{ + //X509Certificate2Collection to check the signer's identity using root certificates. + X509Certificate2Collection collection = new X509Certificate2Collection(); + //Creates a certificate instance from PFX file with private key. + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + byte[] data = new byte[certificateStream.Length]; + certificateStream.Read(data, 0, data.Length); + //Create new X509Certificate2 with the root certificate. + X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); + //Add the certificate to the collection. + collection.Add(certificate); + //Validate all signatures in loaded PDF document and get the list of validation result. + List results; + bool isValid = loadedDocument.Form.Fields.ValidateSignatures(collection, out results); + Console.WriteLine("All signatures in the document are valid: " + isValid); +} \ No newline at end of file diff --git a/Digital Signature/Validate-the-digitally-signed-PDF-signature/.NET/Validate-the-digitally-signed-PDF-signature/Program.cs b/Digital Signature/Validate-the-digitally-signed-PDF-signature/.NET/Validate-the-digitally-signed-PDF-signature/Program.cs index 0fedca21..c49d8985 100644 --- a/Digital Signature/Validate-the-digitally-signed-PDF-signature/.NET/Validate-the-digitally-signed-PDF-signature/Program.cs +++ b/Digital Signature/Validate-the-digitally-signed-PDF-signature/.NET/Validate-the-digitally-signed-PDF-signature/Program.cs @@ -1,138 +1,120 @@ -// See https://aka.ms/new-console-template for more information - -using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates; -// Load the input PDF document stream from the specified file path -using (FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) +// Load the signed PDF document using the stream +using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"))) { - - // Load the signed PDF document using the stream - using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream)) + // Retrieve the first signature field from the PDF form + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; + // Create a certificate collection to hold trusted root certificates for validation + X509CertificateCollection collection = new X509CertificateCollection(); + // Load the root certificate from a PFX file (includes private key) + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + byte[] data = new byte[certificateStream.Length]; + certificateStream.Read(data, 0, data.Length); + // Create an X509Certificate2 instance using the loaded certificate data and password + X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); + // Add the certificate to the validation collection + collection.Add(certificate); + // Validate the signature using the provided certificate collection + PdfSignatureValidationResult result = signatureField.ValidateSignature(collection); + // Initialize flag to detect timestamp signatures + bool isTimeStampSignature = false; + // Check if the TimeStampInformation object is not null + if (result.TimeStampInformation != null) { - - // Retrieve the first signature field from the PDF form - PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; - - // Create a certificate collection to hold trusted root certificates for validation - X509CertificateCollection collection = new X509CertificateCollection(); - - // Load the root certificate from a PFX file (includes private key) - FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); - byte[] data = new byte[certificateStream.Length]; - certificateStream.Read(data, 0, data.Length); - - // Create an X509Certificate2 instance using the loaded certificate data and password - X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); - - // Add the certificate to the validation collection - collection.Add(certificate); - - // Validate the signature using the provided certificate collection - PdfSignatureValidationResult result = signatureField.ValidateSignature(collection); - - // Initialize flag to detect timestamp signatures - bool isTimeStampSignature = false; - - // Check if the TimeStampInformation object is not null - if (result.TimeStampInformation != null) + // Check if the signature is a document timestamp + if (result.TimeStampInformation.IsDocumentTimeStamp) { - // Check if the signature is a document timestamp - if (result.TimeStampInformation.IsDocumentTimeStamp) - { - isTimeStampSignature = true; - Console.WriteLine("Signature is a document timestamp signature."); - } - - // Retrieve signer certificates if available - PdfSignerCertificate[] certificates = result.TimeStampInformation.SignerCertificates; - if (certificates != null && certificates.Length > 0) - { - Console.WriteLine($"Retrieved {certificates.Length} signer certificate(s)."); - } - else - { - Console.WriteLine("No signer certificates found."); - } - - // Retrieve the main certificate - X509Certificate2 certificate2 = result.TimeStampInformation.Certificate; - if (certificate2 != null) - { - Console.WriteLine($"Certificate Subject: {certificate2.Subject}"); - } - else - { - Console.WriteLine("No certificate found."); - } - - // Retrieve timestamp date - DateTime dateTime = result.TimeStampInformation.Time; - Console.WriteLine($"Timestamp Date: {dateTime}"); - - // Retrieve timestamp policy ID - string policyID = result.TimeStampInformation.TimeStampPolicyId; - if (!string.IsNullOrEmpty(policyID)) - { - Console.WriteLine($"Timestamp Policy ID: {policyID}"); - } - else - { - Console.WriteLine("No Timestamp Policy ID found."); - } + isTimeStampSignature = true; + Console.WriteLine("Signature is a document timestamp signature."); + } - // Check if the timestamp is valid - bool valid = result.TimeStampInformation.IsValid; - Console.WriteLine($"Timestamp Validity: {(valid ? "Valid" : "Invalid")}"); + // Retrieve signer certificates if available + PdfSignerCertificate[] certificates = result.TimeStampInformation.SignerCertificates; + if (certificates != null && certificates.Length > 0) + { + Console.WriteLine($"Retrieved {certificates.Length} signer certificate(s)."); } else { - Console.WriteLine("TimeStampInformation is null. Cannot retrieve timestamp details."); + Console.WriteLine("No signer certificates found."); } - // Check if the signature is valid - SignatureStatus status = result.SignatureStatus; - - // Check if the document has been modified after signing - bool isModified = result.IsDocumentModified; - // Check if Long-Term Validation (LTV) is enabled in the signature - bool isLtvEnabled = result.LtvVerificationInfo.IsLtvEnabled; - - // Check if Certificate Revocation List (CRL) data is embedded in the PDF - bool isCrlEmbedded = result.LtvVerificationInfo.IsCrlEmbedded; - - // Check if Online Certificate Status Protocol (OCSP) data is embedded in the PDF - bool isOcspEmbedded = result.LtvVerificationInfo.IsOcspEmbedded; - - // Output the validation results to the console - Console.WriteLine("Document modified: " + isModified); - Console.WriteLine("LTV enabled: " + isLtvEnabled); - Console.WriteLine("CRL embedded: " + isCrlEmbedded); - Console.WriteLine("OCSP embedded: " + isOcspEmbedded); - - // Extract and display signature certificate details - string issuerName = signatureField.Signature.Certificate.IssuerName; - DateTime validFrom = signatureField.Signature.Certificate.ValidFrom; - DateTime validTo = signatureField.Signature.Certificate.ValidTo; - string signatureAlgorithm = result.SignatureAlgorithm; - DigestAlgorithm digestAlgorithm = result.DigestAlgorithm; - - Console.WriteLine("Issuer Name: " + issuerName); - Console.WriteLine("Valid From: " + validFrom); - Console.WriteLine("Valid To: " + validTo); - Console.WriteLine("Signature Algorithm: " + signatureAlgorithm); - Console.WriteLine("Digest Algorithm: " + digestAlgorithm); + // Retrieve the main certificate + X509Certificate2 certificate2 = result.TimeStampInformation.Certificate; + if (certificate2 != null) + { + Console.WriteLine($"Certificate Subject: {certificate2.Subject}"); + } + else + { + Console.WriteLine("No certificate found."); + } - // Extract and display revocation validation details - RevocationResult revocationDetails = result.RevocationResult; - RevocationStatus revocationStatus = revocationDetails.OcspRevocationStatus; - bool isRevokedCRL = revocationDetails.IsRevokedCRL; + // Retrieve timestamp date + DateTime dateTime = result.TimeStampInformation.Time; + Console.WriteLine($"Timestamp Date: {dateTime}"); - Console.WriteLine("Revocation Status: " + revocationStatus); - Console.WriteLine("Is Revoked CRL: " + isRevokedCRL); + // Retrieve timestamp policy ID + string policyID = result.TimeStampInformation.TimeStampPolicyId; + if (!string.IsNullOrEmpty(policyID)) + { + Console.WriteLine($"Timestamp Policy ID: {policyID}"); + } + else + { + Console.WriteLine("No Timestamp Policy ID found."); + } - // Close the loaded PDF document and release resources - loadedDocument.Close(true); + // Check if the timestamp is valid + bool valid = result.TimeStampInformation.IsValid; + Console.WriteLine($"Timestamp Validity: {(valid ? "Valid" : "Invalid")}"); + } + else + { + Console.WriteLine("TimeStampInformation is null. Cannot retrieve timestamp details."); } + // Check if the signature is valid + SignatureStatus status = result.SignatureStatus; + + // Check if the document has been modified after signing + bool isModified = result.IsDocumentModified; + + // Check if Long-Term Validation (LTV) is enabled in the signature + bool isLtvEnabled = result.LtvVerificationInfo.IsLtvEnabled; + + // Check if Certificate Revocation List (CRL) data is embedded in the PDF + bool isCrlEmbedded = result.LtvVerificationInfo.IsCrlEmbedded; + + // Check if Online Certificate Status Protocol (OCSP) data is embedded in the PDF + bool isOcspEmbedded = result.LtvVerificationInfo.IsOcspEmbedded; + + // Output the validation results to the console + Console.WriteLine("Document modified: " + isModified); + Console.WriteLine("LTV enabled: " + isLtvEnabled); + Console.WriteLine("CRL embedded: " + isCrlEmbedded); + Console.WriteLine("OCSP embedded: " + isOcspEmbedded); + + // Extract and display signature certificate details + string issuerName = signatureField.Signature.Certificate.IssuerName; + DateTime validFrom = signatureField.Signature.Certificate.ValidFrom; + DateTime validTo = signatureField.Signature.Certificate.ValidTo; + string signatureAlgorithm = result.SignatureAlgorithm; + DigestAlgorithm digestAlgorithm = result.DigestAlgorithm; + + Console.WriteLine("Issuer Name: " + issuerName); + Console.WriteLine("Valid From: " + validFrom); + Console.WriteLine("Valid To: " + validTo); + Console.WriteLine("Signature Algorithm: " + signatureAlgorithm); + Console.WriteLine("Digest Algorithm: " + digestAlgorithm); + + // Extract and display revocation validation details + RevocationResult revocationDetails = result.RevocationResult; + RevocationStatus revocationStatus = revocationDetails.OcspRevocationStatus; + bool isRevokedCRL = revocationDetails.IsRevokedCRL; + + Console.WriteLine("Revocation Status: " + revocationStatus); + Console.WriteLine("Is Revoked CRL: " + isRevokedCRL); } \ No newline at end of file