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.

Thu Nov 21, 2019 10:28 am

Hi there, I'm having issues on adding a certification to a PDF.
Everything works rather well in .NET Framework, but not in .NET Core.

This is the program used in .NET Framework, and works perfectly:

Code: Select all
   using (PdfDocument document = new PdfDocument())
   {
    document.LoadFromFile(@"C:\input.pdf");
    using (X509Store store = new X509Store(StoreLocation.CurrentUser))
    {
     store.Open(flags: OpenFlags.ReadOnly);
     X509Certificate2Collection storecollection = X509Certificate2UI.SelectFromCollection(certificates: store.Certificates, title: null, message: null, selectionFlag: X509SelectionFlag.SingleSelection);
     using (PdfCertificate cert = new PdfCertificate(signData: storecollection[0].RawData))
     {
      PdfSignature signature = new PdfSignature(document, document.Pages[0], cert, "signature0")
      {
       DateLabel = "Date:",
       Date = DateTime.Now,
       DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges,
       Certificated = true
      };
      document.FileInfo.IncrementalUpdate = false;
      document.CompressionLevel = PdfCompressionLevel.Best;
      document.SaveToFile(@"C:\output.pdf");
      document.Close();
     }
    }
   }


The above code doesn't work in .NET Core:
  • Out of the bat, the .NET Core constructor is different, has an extra string argument called password.
  • That argument doesn't do anything. If one inserts null or even the certificate password (ouch!), it still will not sign the certificate.
  • On .NET Framework after I select the certificate there's a call from Pdfcertificate to insert the pin. That does not occur in .NET Core, as it crashes with a PdfException: lost private key.

Am I doing something wrong? Thanks in advance.

porcolino
 
Posts: 2
Joined: Fri Oct 04, 2019 9:31 pm

Fri Nov 22, 2019 7:43 am

Hi,

Thanks for your inquiry.
I made an initial test and did notice the issue you mentioned. I have logged it into our bug tracking system, once there is any progress, we will inform you. Sorry for the inconvenience caused.
By the way, I found if you load your certificate file directly from the disk, there won't be the exception, you could have a try. Below is the code for you.

Code: Select all
                    //Load your certificate directly from the disk.
                    using (PdfCertificate cert = new PdfCertificate(@"……\certificate.pfx", "password"))
                    {
                    //your code.
                    }


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Nov 22, 2019 12:25 pm

Hi Amber.Gu,

Thank you for your fast reply.
Unfortunately I can't use files, only the store + smartcard combination.

porcolino
 
Posts: 2
Joined: Fri Oct 04, 2019 9:31 pm

Mon Nov 25, 2019 1:21 am

Hi,

Thanks for your reply. Sorry for reply late for weekend.
Once there is any progress about your issue, we will inform you ASAP. Sorry for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Nov 26, 2019 3:25 pm

I have the same need and I just purchased a license of Spire.PDF, expecting to be able to sign my PDFs based on a certificate stored on a hardware token.
User avatar

pierre.arnaud
 
Posts: 13
Joined: Tue Nov 26, 2019 9:23 am

Wed Nov 27, 2019 6:01 am

Hi,

Thanks for your inquiry.
Please note we only need the certificate data while signing a Pdf file. You could get the data from X509Certificate2 first, then create PdfCertificate. Please refer to following code and have a try.

Code: Select all
PdfCertificate cert = new PdfCertificate(signData: storecollection[0].RawData ,"password");

If there is an exception(lost private key) while saving signed Pdf file, I am afraid this is a known issue while signing with the certificate from X509Certificate2 under .NET Core environment. This issue has been logged into our bug tracking system. Once there is any progress, we will inform you. Sorry for the inconvenience caused.
If the exception on your side is different or you encounter other issues, please share us with detailed information.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Dec 17, 2019 11:22 am

Hello, any solution for this bug?

ElVecino.Com
 
Posts: 1
Joined: Fri Oct 26, 2018 4:59 pm

Wed Dec 18, 2019 2:13 am

Hi,

Thanks for your inquiry.
The previous issue that there will be an exception(lost private key) when save the signed Pdf file which sign with the certificate from X509Certificate2 under .NET Core environment has been resolved in following hotfix: http://www.e-iceblue.com/downloads/TempVersion/spire.pdf_5.12.19.zip
Please download and test it. If you still have the issue, please provide detailed information

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF