Dear Sir.
Is it possible to sign a pdf with a certificate directly from the windows certificate store?
Thank you.
//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();
'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()