Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);

Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -32,6 +25,6 @@
}
}
}
//Close the document
loadedDocument.Close(true);
//Save the PDF document
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
}
Original file line number Diff line number Diff line change
@@ -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<X509Certificate2> { 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<X509Certificate2> { 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Original file line number Diff line number Diff line change
@@ -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);
// 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");
}
}
Loading
Loading