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.

Fri Jul 19, 2024 12:18 pm

Dear Sir.

Is it possible to sign a pdf with a certificate directly from the windows certificate store?
Thank you.

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

Mon Jul 22, 2024 8:08 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: 732
Joined: Mon Dec 27, 2021 2:23 am

Fri Dec 20, 2024 11:40 am

Thanks for your response.

Would you have a Visual Basic version of this same code?

Thank you.

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

Mon Dec 23, 2024 6:44 am

Hello,
Thanks for your reply.
The Visual Basic version of the code is as follows:
Code: Select all
'Load PDF document from disk
Dim input As String = "../../../../../../Data/PDFTemplate_HF.pdf"
Dim doc As New PdfDocument()
doc.LoadFromFile(input)

'Sign with certificate selection in the windows certificate store
Dim store As 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
Dim sel As System.Security.Cryptography.X509Certificates.X509Certificate2Collection = System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(store.Certificates, Nothing, Nothing, System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection)

'Create a certificate using the certificate data from the store
Dim cert As New PdfCertificate(sel(0).RawData)

Dim signature As 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("../../../../../../Data/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 = Date.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 Or 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
Dim output As String = "SignWithSmartCardUsingPdfFileSignature_out.pdf"
doc.SaveToFile(output)
doc.Close()


Sincerely,
William
E-iceblue support team
User avatar

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

Fri Sep 19, 2025 8:56 am

Estimado Sr.

Estoy teniendo problemas con este código de su ejemplo.


' Create a new signature field on the first page of the document
Dim signature As New PdfSignature(doc, doc.Pages(0), cert, "signature0")


Obtengo el error:

BC30182 Se esperaba un tipo.

Supongo que es una incompatibilidad con el framework de Visual studio, pero no he podido resolverlo.

Estoy usando FreeSpire PDF 10.2.0 y framework 8.0 Visual Studio 2022

¿Qué puede causar este error? ¿Sería mejor usar otra versión de framework?

Gracias, hasta pronto.

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

Fri Sep 19, 2025 9:14 am

Hello,

Thanks for your reply.
To help us investigate your issue more accurately, please share more details with us, such as detailed exception information. Some screenshots will also be helpful. Thanks in advance. In addition, we recommend that you install FreeSpire.PDF through Nuget search. Did you install it this way?

Sincerely,
William
E-iceblue support team
User avatar

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

Fri Sep 26, 2025 9:03 am

Gracias por la respuesta.

En Visual Studio Visual Basic 2022 qué Framework sería más recomendable para interactuar con spire.pdf?

Gracias, hasta luego.

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

Fri Sep 26, 2025 10:02 am

Hello,

Thanks for your reply.
Kinly note that our FreeSpire.PDF is compatible with the Net8.0 framework. I have attached a simple project for your reference. Please download and test it. If you encounter any issue during testing, please feel free to write back.

Sincerely,
William
E-iceblue support team
User avatar

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

Tue Sep 30, 2025 11:03 am

Estimado Sr.

Muchas gracias. el ejemplo ha sido muy interesante. He detectado que la causa del no funcionamiento es una incompatiblidad de mi proyecto con la librería SkiaSharp, todo funciona perfectamente con la 1.68.0 pero no con la versión actual de dicha librería 3.119.1

En el mismo proyecto tengo que usar Spire.OCR que requiere una versión más avanzada de la librería SkiaSharp no sendo compatible con la 1.68.0 ¿Hay alguna versión de la librería SkiaSharp que me permita usar ambos módulos de Spire sin problemas?

Gracias.

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

Wed Oct 01, 2025 5:03 am

Hello,

Thank you for your inquiry.

To use Spire.PDF together with Spire.OCR in the same project, you need to install Spire.PDF for .NET Standard dll.
Please install it via NuGet by searching for "Spire.PDFfor.NETStandard".
https://www.nuget.org/packages/Spire.PDFfor.NETStandard/11.9.17.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3007
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF