We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Cannot sign PDF with LTV enabled

Hi!

I am trying to sign a pdf using LTV but I get a NullReferenceException.

I followed the guide on the controls' documentation.
I successfully created a PDF without LTV. The following code works fine if is set signature.EnableLtv = false .

   
Private Function CreatePdf(myCert As X509Certificate2 ) As String
        Try
            dim cert as PdfCertificate = new PdfCertificate(myCert)
            Dim doc As New PdfDocument()
            Dim page As PdfPageBase = doc.Pages.Add()
            Dim graphics As PdfGraphics = page.Graphics
            Dim signature As New PdfSignature(doc, page, cert, "Signature")
            signature.TimeStampServer = New TimeStampServer(New Uri("http://timestamp.ermis.gov.gr/TSS/HttpTspServer"))
            signature.Bounds = New RectangleF(New PointF(100, 200), new SizeF(100,100))
            signature.ContactInfo = "CONTACT"
            signature.LocationInfo = "LOCATION"
            signature.Reason = "REASON"
            signature.Bounds = New RectangleF(New PointF(100, 100), New SizeF(50, 50))
            Dim _font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)
            graphics.DrawString("Hello world!", _Font, PdfBrushes.Black, New PointF(0, 0))
            signature.EnableValidationAppearance = True

            signature.EnableLtv = true

            doc.Save("outputNoLtv.pdf")
            doc.Close(True)
            Process.Start("outputNoLtv.pdf")
            Return "OK"
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
            Return ex.Message.ToString()
        End Try
    End Function


When I set  "signature.EnableLtv=True" I get the following error:

{"Object reference not set to an instance of an object."}
   at Syncfusion.Pdf.Security.PdfSignature.set_EnableLtv(Boolean value)
   at Tests.Form1.CreatePdf(X509Certificate2 myCert) in ... Form1.vb:line 105

The lin 105 is the exact line where I assign the value to EnableLtv: signature.EnableLtv=True

Do you have any suggestions?

5 Replies

KC Karthikeyan Chandrasekar Syncfusion Team November 5, 2018 05:12 PM UTC

Hi Stavros, 
LTV for signature will only works when load the certificate (.pfx) from a file or stream using PdfCertificate instance and it does not support if the certificate is loaded from X509Certificate2 API like you have used in your previous code snippet.  

So kindly use the code snippet mentioned below to enable LTV in the signature of PDF document:  
'Load the certificate from a path or as a stream  
Dim cert As PdfCertificate = New PdfCertificate("C://temp//pdf.pfx", "syncfusion")  
Dim doc As PdfDocument = New PdfDocument  
Dim page As PdfPageBase = doc.Pages.Add  
Dim graphics As PdfGraphics = page.Graphics  
Dim signature As PdfSignature = New PdfSignature(doc, page, cert, "Signature")  
signature.TimeStampServer = New TimeStampServer(New Uri("http://timestamp.ermis.gov.gr/TSS/HttpTspServer"))  
signature.Bounds = New RectangleF(New PointF(100, 200), New SizeF(100, 100))  
signature.ContactInfo = "CONTACT"  
signature.LocationInfo = "LOCATION"  
signature.Reason = "REASON"  
signature.Bounds = New RectangleF(New PointF(100, 100), New SizeF(50, 50))  
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)  
graphics.DrawString("Hello world!", font, PdfBrushes.Black, New PointF(0, 0))  
signature.EnableValidationAppearance = True  
signature.EnableLtv = True  
doc.Save("output.pdf")  
doc.Close(True)  
Process.Start("output.pdf")  

Regards, 
karthikeyan 



ST Stavros November 8, 2018 12:53 PM UTC

I use a certificate stored on a HSM with non-exportable private key.

Thanks for the info, anyway. I'll try to find a workaround.



KC Karthikeyan Chandrasekar Syncfusion Team November 9, 2018 09:54 AM UTC

Hi Stavros,
Thanks for your update, we will fix the mentioned null reference exception while enabling LTV in the upcoming volume 4 release.

Regards,
Karthikeyan



TT Thorsten Twellmann August 7, 2019 06:22 PM UTC

Does Syncfusion plan to support LTV in combination with certificates on HSM, e.g. USB tokens, in the near future?

Thanks Thorsten


SK Surya Kumar Syncfusion Team August 8, 2019 02:11 PM UTC

Hi Thorsten, 

We can create LTV enabled signature PDF by loading certificate as X509Certificate2 instance and using it to sign the PDF. 
As a workaround you can get the certificate from USB as X509Certificate2  and use this to sign the PDF using below code snippet: 
// Create a PdfCertificate instance with X509Certificate2 created from certificates stored on USB tokens 
PdfCertificate cert = new PdfCertificate(x509Certificate2); 
 
PdfDocument doc = new PdfDocument(); 
PdfPageBase page = doc.Pages.Add(); 
PdfGraphics graphics = page.Graphics; 
PdfSignature signature = new PdfSignature(doc, page, cert, "Signature"); 
signature.TimeStampServer = new TimeStampServer(new Uri("Time stamp server link")); 
signature.Bounds = new RectangleF(new PointF(100, 200), new SizeF(100, 100)); 
signature.ContactInfo = "CONTACT"; 
signature.LocationInfo = "LOCATION"; 
signature.Reason = "REASON"; 
signature.Bounds = new RectangleF(new PointF(100, 100), new SizeF(50, 50)); 
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14); 
graphics.DrawString("Hello world!", font, PdfBrushes.Black, new PointF(0, 0)); 
signature.EnableValidationAppearance = true; 
signature.EnableLtv = true; 
doc.Save("output.pdf"); 
doc.Close(true); 

Kindly try the same and let us know if you need any further information. 

Regards, 
Surya Kumar 


Loader.
Live Chat Icon For mobile
Up arrow icon