PdfDocument not save using digital signature

I have a word file that after changing I need to convert to PDF and digitally sign

            var entity = db.Records.FirstOrDefault(x => x.Id);

            var blob = AzureBlob.DownloadStream(entity.TemplateId);
            var DocWord = new WordDocument(blob);
            // Edit DocWord using Replace("<>", entity.Name, true,true); and others properties

            DocToPDFConverter converter = new DocToPDFConverter();

            PdfDocument pdfDocument = converter.ConvertToPDF(DocWord);
            pdfDocument.Compression = PdfCompressionLevel.Best;
            //Add graphic
            PdfGraphics graphics = pdfDocument.Pages[0].Graphics;

            //Load certificate
            var digitalCertificate = db.DigitalCertificates.Where(x => x.Id == entity.DigitalCertificateId).FirstOrDefault(); 
            //load X509Certificate2 using byte[] 
            var x509 = new X509Certificate2(digitalCertificate.Cert, EncryptionHelper.Decrypt(digitalCertificate.Password), , X509KeyStorageFlags.UserKeySet);
            PdfCertificate pdfCert = new PdfCertificate(x509);
            PdfSignature signature = new PdfSignature(pdfDocument, pdfDocument.Pages[0], pdfCert, "Sign");
            //Create image
            PdfBitmap signatureImage = new PdfBitmap(HttpContext.Current.Server.MapPath(@"~/Content/images/pdfStamp.png"));
            
            signature.Certificated = true;
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;
            signature.Bounds = new RectangleF(new PointF(0, 0), signatureImage.PhysicalDimension);

           
            graphics.SetTransparency(0.10F);
            graphics.DrawImage(signatureImage, 15, 740);

            PdfFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10), true);
            graphics.SetTransparency(0.40F);
            graphics.DrawString("Sign in by", font, PdfBrushes.Black, new PointF(85, 760));
            graphics.DrawString(x509.FriendlyName, font, PdfBrushes.Black, new PointF(85, 770));
            graphics.DrawString($"{DateTime.Now.ToString("dd/MM/yyyy HH:mm")}", font, PdfBrushes.Black, new PointF(85, 780));

            //Convert save
            var result = new MemoryStream();
            pdfDocument.Save(result);
            result.Seek(0, SeekOrigin.Begin);


Localhost its ok

In server (Azure Web App S1) this Exception:

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.       at System.Security.Cryptography.Pkcs.PkcsUtils.CreateSignerEncodeInfo(CmsSigner signer, Boolean silent, SafeCryptProvHandle& hProv)     at System.Security.Cryptography.Pkcs.SignedCms.Sign(CmsSigner signer, Boolean silent)     at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer, Boolean silent)     at Syncfusion.Pdf.Security.PdfSignatureDictionary.SignCertificate(Byte[] message, X509Certificate2 cert, Boolean detached)     at Syncfusion.Pdf.Security.PdfSignatureDictionary.GetStoreCertificate()     at Syncfusion.Pdf.Security.PdfSignatureDictionary.DocumentSaved(Object sender, DocumentSavedEventArgs e)     at Syncfusion.Pdf.PdfDocumentBase.OnDocumentSaved(DocumentSavedEventArgs args)     at Syncfusion.Pdf.PdfDocument.Save(Stream stream)     at HealthModule.Data.Domain.Records.RecordHelper.Sign(AppDbContext db, Guid id, Guid digitalCertificateId, String password, Guid tenantId)

Synfusion Version:

 
   package id="Syncfusion.Compression.Base" version="18.1.0.43" targetFramework="net472"
  package id="Syncfusion.DocIO.ClientProfile" version="18.1.0.43" targetFramework="net472"
  package id="Syncfusion.DocToPdfConverter.ClientProfile" version="18.1.0.43" targetFramework="net472"
  package id="Syncfusion.Licensing" version="18.1.0.43" targetFramework="net472"
  package id="Syncfusion.OfficeChart.Base" version="18.1.0.43" targetFramework="net472"
  package id="Syncfusion.Pdf.ClientProfile" version="18.1.0.43" targetFramework="net472"
 
 .net framework 4.7.2

2 Replies

SL Sowmiya Loganathan Syncfusion Team April 8, 2020 01:04 PM UTC

Hi Marcelo,  
 
Thank you for contacting Syncfusion support.  
 
Currently we are trying to reproduce the reported issue. We will update the further details on 9th April,2020.  
 
Regards, 
Sowmiya Loganathan 



SL Sowmiya Loganathan Syncfusion Team April 9, 2020 12:26 PM UTC

Hi Marcelo,   
  
Thank you for your patience.   
  
We were able to see the reported issue “While Sign the PDF document using X509Certificate in Azure”. However, we can overcome this error by directly load the certificate in “PdfCertificate” overload and please find the below code snippet for more details,  
  
//Creates a certificate instance from PFX file with private key  
Stream certificateStream = new FileStream(path, FileMode.Open);  
PdfCertificate pdfCertificate = new PdfCertificate(certificateStream, "syncfusion");  
  
PdfSignature signature = new PdfSignature(document, document.Pages[0], pdfCertificate,"Sign");  
                
Please try the above solution in your end and let us know the result.   
  
Regards,  
Sowmiya Loganathan  
  


Loader.
Up arrow icon