Skip to content

Commit

Permalink
911183: Update the github sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sri-hari-haran-g committed Sep 20, 2024
1 parent 40c2c58 commit 5a3affb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,41 @@
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 loadedDocument = new PdfLoadedDocument(documentStream);
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 signature = signatureField.Signature;
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.
signature.CreateLongTermValidity(new List<X509Certificate2> { x509 });
pdfSignature.CreateLongTermValidity(new List<X509Certificate2> { x509 });

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
Expand All @@ -34,3 +54,22 @@
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();
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.pdf">
<None Update="Data\PDF.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,39 @@
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;

//Get the stream from the document.
FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read);
//Creates a new PDF document.
PdfDocument document = new PdfDocument();

//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);
//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 signature = signatureField.Signature;
PdfSignature pdfSignature = signatureField.Signature;

//Enable LTV on Signature.
signature.EnableLtv = true;
pdfSignature.EnableLtv = true;


//Create file stream.
Expand Down

0 comments on commit 5a3affb

Please sign in to comment.