Saturday, October 30, 2021

Create Visible Digital Signature in PDF in C#

 Digital Signature is widely used to protect the PDF files and it is so important that we always try our best to improve our Spire.PDF better and better. Besides the previous method of creating digital signature in PDF, Spire.PDF now supports to create visible digital signature in PDF with C# code.

Make sure Spire.PDF for .NET (version 2.9 or above) has been installed correctly. Add Spire.PDF.dll as reference in the downloaded Bin folder through the below path: "...\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".

01using Spire.Pdf;
02using Spire.Pdf.Graphics;
03using Spire.Pdf.Security;
04using System;
05using System.Drawing;
06 
07namespace DigitalSignature
08{
09    class Program
10    {
11        static void Main(string[] args)
12        {
13            //load a pdf document
14            PdfDocument doc = new PdfDocument();
15            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");
16 
17            //load the certificate
18            PdfCertificate cert = new PdfCertificate(@"C:\Users\Administrator\Desktop\gary.pfx""e-iceblue");
19 
20            //initialize a PdfSignature instance
21            PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "Signature1");
22 
23            //set the signature location
24            signature.Bounds = new RectangleF(new PointF(200, 200), new SizeF(200, 90));
25 
26            //set the image of signature
27            signature.SignImageSource = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\E-iceblueLogo.png");
28 
29            //set the content of signature
30            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
31            signature.NameLabel = "Signer:";
32            signature.Name = "Gary";
33            signature.ContactInfoLabel = "ContactInfo:";
34            signature.ContactInfo = signature.Certificate.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true);
35            signature.DistinguishedNameLabel = "DN: ";
36            signature.DistinguishedName = signature.Certificate.IssuerName.Name;
37            signature.LocationInfoLabel = "Location:";
38            signature.LocationInfo = "Chengdu";
39            signature.ReasonLabel = "Reason: ";
40            signature.Reason = "Le document est certifie";
41            signature.DateLabel = "Date:";
42            signature.Date = DateTime.Now;
43            signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
44            signature.Certificated = true;
45 
46            //set fonts
47            signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 10f);
48            signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 15);
49 
50            //set the sign image layout mode
51            signature.SignImageLayout = SignImageLayout.None;
52 
53            //save the file
54            doc.SaveToFile("signature.pdf");
55        }
56    }
57}

Create visible digital signature

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...