Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Mar 09, 2022 3:17 pm

How can I sign a PDF using with certificate from hardware token (Usb Key)?

silvano.laboranti
 
Posts: 4
Joined: Thu May 24, 2018 7:44 am

Thu Mar 10, 2022 1:52 am

Hello,

Thank you for your inquiry.
Please use the following code to achieve your needs.
Code: Select all
//Load PDF document from disk
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("input.pdf");
PdfPageBase page = doc.Pages[0];
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "My USB Token Cert", false);
X509Certificate2 cert1 = certs[0];
PdfCertificate cert = new PdfCertificate(cert1);
PdfSignature signature = new PdfSignature(doc, page, cert, "signature");

signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;

signature.GraphicsMode = GraphicMode.SignDetail;
signature.Certificated = true;

doc.CompressionLevel = PdfCompressionLevel.Best;

//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.ContactInfoLabel = "ContactInfo:";
signature.ContactInfo = signature.Certificate.GetNameInfo(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;

//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 document
string output = "result.pdf";
doc.SaveToFile(output);
If there is any question, just feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 09, 2022 10:39 pm

When using this code with a password protected USB key it generates an operating system gui prompt for the password. This obviously won't work for programmatic signing. Is there any way to provide the password programmatically?

arcticberry
 
Posts: 1
Joined: Thu Nov 03, 2022 3:28 pm

Mon Dec 12, 2022 4:11 am

arcticberry wrote:When using this code with a password protected USB key it generates an operating system gui prompt for the password. This obviously won't work for programmatic signing. Is there any way to provide the password programmatically?


Hello,

Thanks for your inquiry.
I have reported this scenario to our development team, and the issue number with SPIREPDF-5668. Once there are any updates, I’ll inform you in time.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Sun Feb 25, 2024 10:11 am

If there any solution to solve the problem above ?

kivgotha
 
Posts: 3
Joined: Tue Feb 13, 2024 2:14 pm

Mon Feb 26, 2024 2:26 am

Hi,

Thanks for your following up.
I’m sorry to tell you that our product can’t control whether or not generates an operating system gui prompt for the password, due to it’s controlled by operation system.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Feb 28, 2024 11:02 am

I tried - see below (1., 2. attempt) - but it does not work fine for me!

The password could not submit.

Unfurtunality - the password-prompt was showed !

Code: Select all
X509Store store = new(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySerialNumber, certSerialNr, false); //USB-Token Cert
               
// First attempt
X509Certificate2 cert1 = certs[0];
PdfCertificate cert = new(cert1);

// Second attempt - also failed ! -->
//X509Certificate2 X509Certificate2Object = new(certs[0].GetRawCertData(), "password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
//PdfCertificate cert = new PdfCertificate(signData: certs[0].Export(X509ContentType.Pfx), "password");
//<--


// Zuweisung des Zertifikats zum Signieren
PdfSignature signature = new PdfSignature(pdfDoc, page, cert, "Company");

//...

kivgotha
 
Posts: 3
Joined: Tue Feb 13, 2024 2:14 pm

Thu Feb 29, 2024 9:42 am

Hi,

Thanks for your feedback.
Please note that the password output box is controlled by your usb certificate software, and this is unavoidable.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc