Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jul 23, 2024 9:58 am

Hello.

Is it possible to perform a digital signature using a certificate stored in the Windows certificate store using its spire.pdf library?


Thank you.

JleonIcoes
 
Posts: 2
Joined: Fri Jul 12, 2024 10:36 am

Tue Jul 23, 2024 2:34 pm

I can't speak on a Windows Store, but if your application and environment is in Azure, we are successfully using a Azure Key Vault with the certificate stored in the Key Vault and accessing and downloading the Certificate using the Azure API and providing it to Spire PDF as the certificate with no problems.

motogeek
 
Posts: 15
Joined: Tue Mar 16, 2021 4:18 am

Thu Jul 25, 2024 12:59 am

Hello,

Thanks for your inquriy.
Yes, our Spire.PDF supports signing PDF with certificates from Windows Certificate Store. Please refer to the code below for testing. If you have any questions, please feel free to write back.

Code: Select all
//Load PDF document from disk
 String input = "PDFTemplate_HF.pdf";
 PdfDocument doc = new PdfDocument();
 doc.LoadFromFile(input);

 //Sign with certificate selection in the windows certificate store
 System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser);
 store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly);

 //Manually chose the certificate in the store
 System.Security.Cryptography.X509Certificates.X509Certificate2Collection sel = System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);

 //Create a certificate using the certificate data from the store
 PdfCertificate cert = new PdfCertificate(sel[0].RawData);

 PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "signature0");
 signature.Bounds = new RectangleF(new PointF(250, 660), new SizeF(250, 90));

 //Load sign image source.
 signature.SignImageSource = PdfImage.FromFile("E-iceblueLogo.png");

 //Set the dispay mode of graphics, if not set any, the default one will be applied
 signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
 signature.NameLabel = "Signer:";

 signature.Name = sel[0].GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName,true);

 signature.ContactInfoLabel = "ContactInfo:";
 signature.ContactInfo = signature.Certificate.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true);

 signature.DateLabel = "Date:";
 signature.Date = DateTime.Now;

 signature.LocationInfoLabel = "Location:";
 signature.LocationInfo = "Chengdu";

 signature.ReasonLabel = "Reason: ";
 signature.Reason = "The certificate of this document";

 signature.DistinguishedNameLabel = "DN: ";
 signature.DistinguishedName = signature.Certificate.IssuerName.Name;

 signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
 signature.Certificated = true;

 //Set fonts, if not set, default ones will be applied
 signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 10f);
 signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 15);

 //Set the sign image layout mode
 signature.SignImageLayout = SignImageLayout.None;

 //Save the PDF file
 String output = "PdfFileSignature_out.pdf";
 doc.SaveToFile(output);
 doc.Close();


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 454
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF