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 Dec 10, 2019 9:15 am

On our local windows machine the code has worked fine for years. We use latest Spire.Pdf 5.12.3.

We've moved the code to Microsoft Azure and we are getting this error:

Code: Select all
WindowsCryptographicException: The system cannot find the file specified
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider, CngKeyOpenOptions openOptions)

PdfException: export private key failed.
spr⠲.ᜀ(X509Certificate2 A_0, byte[] A_1)


WindowsCryptographicException: The system cannot find the file specified
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider)
Internal.Cryptography.Pal.CertificatePal.GetPrivateKey<T>(Func<CspParameters, T> createCsp, Func<CngKey, T> createCng)
Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey()
System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
spr⠲.ᜀ(X509Certificate2 A_0, byte[] A_1)


PdfException: export private key failed.
spr⠲.ᜀ(X509Certificate2 A_0, byte[] A_1)
Spire.Pdf.Security.PdfCertificate.ᜀ(byte[][] A_0)
spr⡆.ᜀ(byte[][] A_0)
spr⡆.ᜀ()
spr⡆.ᜂ(spr᜸ A_0)
spr⡆.ᜀ(object A_0, sprᝠ A_1)
spr᝞.ᜀ(sprᝠ A_0)
spr᝞.ᜀ(spr᜸ A_0, bool A_1)
spr᝞.ᜅ(spr᜸ A_0)
sprៜ.ᜀ(spr᜶ A_0, sprᝥ A_1, spr៥ A_2)
sprៜ.ᜀ(spr᜶ A_0, spr៥ A_1)
sprៜ.ᜄ(spr៥ A_0)
sprៜ.ᜅ(spr៥ A_0)
spr᡽.ᜁ(spr៥ A_0)
spr᡽.ᜁ(Stream A_0)
Spire.Pdf.PdfDocument.SaveToStream(Stream stream)

bob7144787
 
Posts: 3
Joined: Tue Dec 10, 2019 9:05 am

Tue Dec 10, 2019 9:54 am

Hi,

Thanks for your inquiry.
According to your information, it seemed you were using signature function. Please provide following detailed information for further investigation.
1. The input PDF file
2. The certificate file(if any) along with the password
3. The complete code you were using
4. Do you use .NET Core framework?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Dec 10, 2019 9:54 pm

1) I can't provide the pdf as it has confidential information
2) I can't provide the certificate file either
3) The code is below
4) Yes, .net Core

Code: Select all
        private MemoryStream SignAndSaveInternal(Stream document, CertifiedPayrollSigner signer)
        {
            byte[] certBytes = _certificateRepository.Get(signer.CertificateName);
            PdfCertificate cert = new PdfCertificate(certBytes, signer.CertificatePassword);


            PdfDocument doc = new PdfDocument();
            doc.LoadFromStream(document); //load the PDF into a document object   
            doc.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //the signature will be on the last page of the document, so:
            int pageCount = doc.Pages.Count; //find the page count and:
            PdfPageBase page = doc.Pages[pageCount - 1]; //select the last page from the document object's Pages array

            PdfSignature signature = new PdfSignature(doc, page, cert, signer.Name);
            signature.ConfigGraphicType = ConfiguerGraphicType.Text;
            signature.Bounds = new System.Drawing.RectangleF(new System.Drawing.PointF(564, 444), new System.Drawing.SizeF(270, 35));
            signature.DigitalSigner = signer.Name;

            PdfPen pen = new PdfPen(Color.Black, 0.2f);
            PdfSolidBrush brush = new PdfSolidBrush(System.Drawing.Color.Black);
            PdfStringFormat format = new PdfStringFormat();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 6f);

            //add the timestamp
            string date = "Date: " + DateTime.Now;
            RectangleF rectA = new RectangleF(664, 470, 100, 35);
            page.Canvas.DrawString(date, font, pen, brush, rectA, format);

            //add the Digitally signed by stamp
            string signedBy = "Digitally signed by " + signer.Name;
            RectangleF rectB = new RectangleF(566, 470, 100, 35);
            page.Canvas.DrawString(signedBy, font, pen, brush, rectB, format);

            //save the PDF with any changes made
            MemoryStream finalPdf = new MemoryStream();
            doc.SaveToStream(finalPdf);
            doc.Close();
            return finalPdf;
        }


bob7144787
 
Posts: 3
Joined: Tue Dec 10, 2019 9:05 am

Tue Dec 10, 2019 10:08 pm

This line change fixed the problem in Azure
source: stackoverflow.com/a/27146917/2565663

Code: Select all
            PdfCertificate cert = new PdfCertificate(certBytes, signer.CertificatePassword,
                X509KeyStorageFlags.MachineKeySet |
                X509KeyStorageFlags.PersistKeySet |
                X509KeyStorageFlags.Exportable);

bob7144787
 
Posts: 3
Joined: Tue Dec 10, 2019 9:05 am

Wed Dec 11, 2019 1:33 am

Hi,

Thanks for sharing the solution.
Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF