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.

Fri Apr 14, 2023 12:19 pm

Hello

I'm trying to sign a PDF with a belgian eID smart card.
I encountered an issue where the PIN has to be passed 2 times. I found this post which proposes a solution:
signing-with-smart-card-t6556.html

It works in that I only have to enter my PIN once, but I'm not seeing the signature when I open the file in Adobe Acrobat Reader.
I've attached my unsigned and signed file. I did notice that when I try to close the file again, it asks me to save my changes, while I've not made any. So maybe something changes when the file is opened by Adobe?

This is the code I'm running
Code: Select all
      public static void Sign(X509Certificate2 cert)
      {
         //Create a PdfDocument object
         PdfDocument doc = new PdfDocument();

         //Load a sample PDF file
         doc.LoadFromFile("test.pdf");

         //create CustomPKCS7SignatureFormatter
         CustomPKCS7SignatureFormatter customPKCS7SignatureFormatter = new CustomPKCS7SignatureFormatter(cert);
         PdfSignature signature = new PdfSignature(doc, doc.Pages[0], customPKCS7SignatureFormatter, "signature0");
         //signature.Bounds = new RectangleF(new PointF(90, 550), new SizeF(270, 90));
         //signature.GraphicsMode = GraphicMode.SignDetail;
         //signature.NameLabel = "Signer:";
         //signature.Name = "Test";
         //signature.Reason = "The certificate of this document";
         //signature.DistinguishedNameLabel = "DN: ";
         signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
         //signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 10f);
         //signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 15);
         //signature.SignImageLayout = SignImageLayout.None;

         //Save to another PDF file
         doc.SaveToFile("out_spire.pdf");

         doc.Close();
      }


Any help as to what may be causing this issue?

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Mon Apr 17, 2023 10:40 am

Hi,

Thanks for your message.
I did an initial test to verify your output PDF with this tutorial(Get and Verify Digital Signature in PDF in C#), it does not have any signature field. Anyway, I will do more tests and update you about the progress accordingly.

Best Regards,
Herman
E-iceblue support team
User avatar

Herman.Yan
 
Posts: 115
Joined: Wed Mar 08, 2023 2:00 am

Wed Apr 19, 2023 6:23 am

Hi,

I used our latest Spire.PDF Pack (Hot Fix) Version: 9.4.0 to test your code and PDF file, it could be signed successfully. I have uploaded my generated PDF file for your reference. As for your situation, please first make sure that you get the correct certificate information. Meanwhile, if you were using an old version, I suggest you upgrade to the latest one to try again. Look forward to your further testing result.

Best Regards,
Herman
E-iceblue support team
User avatar

Herman.Yan
 
Posts: 115
Joined: Wed Mar 08, 2023 2:00 am

Tue Apr 25, 2023 8:49 am

I was working on FreeSpire.
I changed to Spire.PDF version 9.4.0, but now I'm getting a PDF where adobe acrobat just freezes upon opening.

What do you mean by correct certificate information? I'm not sure what to put for the signature length parameter.

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Wed Apr 26, 2023 7:25 am

Hello,

Thanks for your feedback.
Our Dev team has analyzed your output PDF( "out_spire.pdf") and found that the document has added some signature data, but many other data are missing, the signature is not effective. Thus we advice that you need to check the correct certificate information. Beside, our previous tests have used the attached "CustomPKCS7SignatureFormatterWithAPI.cs" to invoke the CustomPKCS7SignatureFormatter class. You can use this file to test again.
Meanwhile, you mentioned our Free version, do you mean that the signing is successful when using the Free version? If so, could you please also share your output of Free version? Or you mean that you used our Free version did other tests. Look forward to your further information.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Wed Apr 26, 2023 8:01 am

Hi

I'm not sure what certificate data could be missing.
I've tried again with the class you provided me, and now I'm getting an error message when attempting to open the file with adobe acrobat.

I've also supplied a correctly signed document with the same certificate, but where I have to enter my PIN code twice to sign.
Can you help identify what is missing between the two.
This is the code I'm running:

Code: Select all
      public static void SignTwice(X509Certificate2 cert)
      {
         //Create a PdfDocument object
         PdfDocument doc = new PdfDocument();

         //Load a sample PDF file
         doc.LoadFromFile("test.pdf");

         var certificate = new PdfCertificate(cert);
         PdfSignature signature = new PdfSignature(doc, doc.Pages[0], certificate, "signature0");
         signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;

         //Save to another PDF file
         doc.SaveToFile("out_spire_correct.pdf");

         doc.Close();
      }

      public static void Sign(X509Certificate2 cert)
      {
         //Create a PdfDocument object
         PdfDocument doc = new PdfDocument();

         //Load a sample PDF file
         doc.LoadFromFile("test.pdf");

         //create CustomPKCS7SignatureFormatter
         IPdfSignatureFormatter customPKCS7SignatureFormatter = new CustomPKCS7SignatureFormatterWithAPI(cert);
         PdfSignature signature = new PdfSignature(doc, doc.Pages[0], customPKCS7SignatureFormatter, "signature0");
         signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;

         //Save to another PDF file
         doc.SaveToFile("out_spire_error.pdf");

         doc.Close();
      }


The only real difference to me is the use of the CustomFormatter, so I'm guessing that needs to be extended somehow, but I'm not sure how or what's missing.

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Thu Apr 27, 2023 3:48 am

Hello,

Thanks for your feedback.
After more tests, I found that your issue did exist when invoking "CustomPKCS7SignatureFormatter" when importing CustomPKCS7SignatureFormatter.cs that was attached at the other post (signing-with-smart-card-t6556.html). We had made some adjustments in that later. Now please use "CustomPKCS7SignatureFormatterWithAPI" by importing my yesterday "CustomPKCS7SignatureFormatterWithAPI.cs" file, like the following code. Look forward to your further testing result.
Code: Select all
...
            BugDebug.CustomPKCS7SignatureFormatterWithAPI customPKCS7SignatureFormatter = new BugDebug.CustomPKCS7SignatureFormatterWithAPI(cert);
......

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Thu Apr 27, 2023 6:54 am

Hello

I tried the new class but I'm still getting the same error message in adobe.
I've attached my test results.

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Thu Apr 27, 2023 10:08 am

Hello,

Thanks for your quick feedback.
How did you obtain your certificate information? You can refer to my obtained method to test again.
Code: Select all
....
            X509Store store = new X509Store(StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "F9B500AD95CDA70CD5157C3A1DF8C35049C3BEF0", false);
            X509Certificate2 cert = new X509Certificate2(certs[0]);

            BugDebug.CustomPKCS7SignatureFormatterWithAPI customPKCS7SignatureFormatter = new BugDebug.CustomPKCS7SignatureFormatterWithAPI(cert);

....

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Thu Apr 27, 2023 12:48 pm

Hi

I'm letting the user select a certificate via UI

Code: Select all
      private static X509Certificate2 GetCertificate()
      {
         X509Store store = new X509Store(StoreLocation.CurrentUser);
         store.Open(OpenFlags.ReadOnly);

         var sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);

         return sel[0];
      }

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Fri Apr 28, 2023 9:42 am

Hello,

Thanks for your feedback.
Even if I use your method, there is no problem at my side. We don't have the smart card, I used the USB. Actually, their logic is the same. Here I uploaded my testing program, please use your smart card to test again. Meanwhile, in order to help us have an accurate investigate to find the root caused reason, you can debug test and record your entire testing process as a video. Thanks for your cooperation.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Fri May 05, 2023 10:20 am

Hi

Sorry for the delay and thanks for the code. The pdf is opening without errors (out.pdf) but I'm not seeing a signature in adobe.
I've included a video of my testing and the pdf result.

I see you put 999 as the SignatureLength parameter in CustomPKCS7SignatureFormatterWithAPI (line 117). Is there a reason for this 999?

If there's anything more you want to see, please ask.

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Sat May 06, 2023 5:41 am

Hello,

Thanks for your feedback.
Sorry I didn't find your included video, could you help to upload it again? If your attachment exceeds the size limitation, you could upload it to the DropBox or OneDrive and then share the download link with us. Thanks in advance.
Besides, about the "Parameters.Add("SignatureLength", 999);" of the CustomPKCS7SignatureFormatterWithAPI, how did you set it on your side?

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Sun May 07, 2023 8:22 pm

Apologies, looks like the attachment was too large and I didn't notice.
Here's a dropbox link: https://www.dropbox.com/s/0o7kqy1zpvylx ... 3.zip?dl=0

I also used SignatureLength 999 in the test results I sent you.

JeroenRoefs
 
Posts: 15
Joined: Thu Mar 30, 2023 1:52 pm

Mon May 08, 2023 9:59 am

Hello,

Thanks for your video.
I have reported this phenomenon to our Dev team, they will analyze the internal data of your output PDF and try to find a solution to locate the root caused reason. Once there is any progress, I will let you know asap.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron