Skip to content

Commit

Permalink
908771_ug: volume 3 UG documentations revamped samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sri-hari-haran-g committed Sep 11, 2024
1 parent edb1ceb commit 574e461
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 73 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);

//Get the page of the existing PDF document.
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;

//Create a new PDF signature without PdfCertificate instance.
PdfSignature signature = new PdfSignature(loadedDocument, loadedPage, null, "Signature1");

//Hook up the ComputeHash event.
signature.ComputeHash += Signature_ComputeHash;
//Gets the first signature field of the PDF document
PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
PdfSignature signature = signatureField.Signature;

//Create X509Certificate2 from your certificate to create a long-term validity.
X509Certificate2 x509 = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion");
Expand All @@ -37,23 +32,3 @@

//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();
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
</ItemGroup>

<ItemGroup>
<None Update="Data\PDF.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\syncfusion_logo.png">
<None Update="Data\Input.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
// See https://aka.ms/new-console-template for more information

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;


//Sets an image for signature field.
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/syncfusion_logo.png"), FileMode.Open, FileAccess.Read);

//Sets an image for signature field.
PdfBitmap image = new PdfBitmap(imageStream);

//Adds time stamp by using the server URI and credentials.
signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/ "));
//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);
//Gets the first signature field of the PDF document
PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
PdfSignature signature = signatureField.Signature;

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

//Sets signature info.
signature.Bounds = new RectangleF(new PointF(0, 0), image.PhysicalDimension);
signature.ContactInfo = "[email protected]";
signature.LocationInfo = "Honolulu, Hawaii";
signature.Reason = "I am author of this document.";

//Draws the signature image.
signature.Appearance.Normal.Graphics.DrawImage(image, 0, 0);

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
document.Save(outputFileStream);
loadedDocument.Save(outputFileStream);
}

//Close the document.
document.Close(true);
loadedDocument.Close(true);

0 comments on commit 574e461

Please sign in to comment.