Hello Everyone,
We have a windows application in (the dot net 2.0 framework). Using this application our client took out invoice print. Due to the new gov. rules and regulations, they want to take out digitally signed invoices in pdf format so they can upload them on the gov. portal.
Every client will have their own one or multiple "DSC token/ e-pass token 2003/smart card" given by emudhra. We have to use this USB token to digitally sign the pdf.
I want to ask that has anyone worked and digitally sign the PDF document using a USB token I need your help in this task.
What I did :
I can add a digital signature in pdf using a pfx file but as discussed with emudhra USB token and a pfx file are two different things.
Sir, I need your help to accomplish this task.
Loading

Leon DRank #851 · 2.6K reputationPosted Jan 27, 2023, 10:46 PM EST
The Spire.PDF library supports signing PDFs using USB token.
https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Code sample:
//Load PDF document PdfDocument doc = new PdfDocument(); doc.LoadFromFile("test.pdf"); PdfPageBase page = doc.Pages[0]; //Get the certificate X509Store store = new X509Store(StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySerialNumber, "Your USB Token Cert", false); X509Certificate2 cert1 = certs[0]; PdfCertificate cert = new PdfCertificate(cert1); Spire.Pdf.Security.PdfSignature signature = new Spire.Pdf.Security.PdfSignature(doc, page, cert, "signature"); signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges; signature.GraphicsMode = Spire.Pdf.Security.GraphicMode.SignDetail; signature.Certificated = true; doc.CompressionLevel = PdfCompressionLevel.Best; //Set the dispay mode of graphics signature.GraphicsMode = Spire.Pdf.Security.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 font signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 10f); signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 15); //Save the Pdf document string output = "output.pdf"; doc.SaveToFile(output);If you get problems, you can post them on the library's forum: https://www.e-iceblue.com/forum/spire-pdf-f7.html?sid=71da8c49b8ad86c39c8c81ca3df3c5b8
Uday DodiyaRank #239 · 10.3K reputationPosted Jan 28, 2023, 2:29 AM EST
To digitally sign a PDF using iTextSharp and a USB token, you will need to first install the iTextSharp library for C#
Example :
using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; using System.Security.Cryptography.X509Certificates; class Demo { static public void Main(String[] args) { X509Certificate2 certificate = FindUsbCertificateByName("Name") string pdfPath = "C:\Users\Admin\Desktop\MainPDF.pdf"; string SignpdfPath = "C:\Users\Admin\Desktop\MainPDF_Signed.pdf"; SignPdf(pdfPath,SignpdfPath,certificate) } public X509Certificate2 FindUsbCertificateByName(string name) { X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, name, true); store.Close(); if (certificates.Count > 0) { return certificates[0]; } else { return null; } } public void SignPdf(string pdfPath, string signedPdfPath,X509Certificate2 certificate) { // Open the PDF document PdfReader pdfReader = new PdfReader(pdfPath); PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, new FileStream(signedPdfPath, FileMode.Create), '\0'); // Create a signature field PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance; signatureAppearance.SetVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); // Create the signature appearance signatureAppearance.Reason = "Approval"; signatureAppearance.Location = "Location"; signatureAppearance.SignatureGraphic = Image.GetInstance("signature.png"); // Create the signature IExternalSignature externalSignature = new X509Certificate2Signature(certificate, "SHA-256"); MakeSignature.SignDetached(signatureAppearance, externalSignature, new Org.BouncyCastle.X509.X509Certificate[] { certificate }, null, null, null, 0, CryptoStandard.CMS); // Close the PDF document pdfStamper.Close(); } }