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.

Mon Dec 17, 2018 1:18 pm

I am getting null reference exception while saving the pdf loaded file. Here is the code:
Code: Select all
String input = "DigitalSignature.pdf";
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(input);
            //Load the certificate
            String pfxPath = @"C:\Program Files (x86)\e-iceblue\Spire.Pdf\Demos\Data\gary.pfx";
            PdfCertificate cert = new PdfCertificate(pfxPath, "e-iceblue");

            PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "signature0");
            signature.Bounds = new RectangleF(new PointF(90, 550), new SizeF(270, 90));

            //Load sign image source.
            //signature.SignImageSource = PdfImage.FromFile(@"..\..\..\..\..\..\Data\E-iceblueLogo.png");

            //Set the dispay mode of graphics, if not set any, the default one will be applied
            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
            signature.NameLabel = "Signer:";

            signature.Name = "Gary";

            signature.ContactInfoLabel = "ContactInfo:";
            signature.ContactInfo = signature.Certificate.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true);

            signature.DateLabel = "Date:";
            signature.Date = DateTime.Now;

            signature.LocationInfoLabel = "Location:";
            signature.LocationInfo = "Chengdu";

            signature.ReasonLabel = "Reason: ";
            signature.Reason = "The certificate of this document";

            signature.DistinguishedNameLabel = "DN: ";
            signature.DistinguishedName = signature.Certificate.IssuerName.Name;

            signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
            signature.Certificated = true;

            //Set fonts. if not set, default ones will be applied.
            signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 10f);
            signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 15);

            //Set the sign image layout mode
            signature.SignImageLayout = SignImageLayout.None;

            //Save pdf file.
            doc.SaveToFile("DigitalSignature.pdf");

            doc.Close();


Here is stack trace:
Code: Select all
  at Spire.Pdf.Security.PdfSignature.ᜀ(PdfCanvas A_0, String A_1, Single A_2, Single A_3)
   at Spire.Pdf.Security.PdfSignature.ᜀ(PdfTemplate A_0)
   at Spire.Pdf.Security.PdfSignature.ᜀ(PdfSignatureField A_0)
   at Spire.Pdf.Security.PdfSignature.ᜀ(Object A_0, EventArgs A_1)
   at spr⺖.ᜀ(EventArgs A_0)
   at spr⺖.ᜋ()
   at Spire.Pdf.Annotations.PdfAnnotation.ᜀ(Object A_0, sprẊ A_1)
   at sprẋ.Invoke(Object A_0, sprẊ A_1)
   at sprẈ.ᜀ(sprẊ A_0)
   at sprẈ.ᜀ(sprḰ A_0, Boolean A_1)
   at sprẈ.ᜅ(sprḰ A_0)
   at sprἊ.ᜀ(sprḮ A_0, sprẏ A_1, sprἓ A_2)
   at sprἊ.ᜀ(sprḮ A_0, sprἓ A_1)
   at sprἊ.ᜄ(sprἓ A_0)
   at sprἊ.ᜅ(sprἓ A_0)
   at spr⹎.ᜁ(sprἓ A_0)
   at spr⹎.ᜂ(Stream A_0)
   at Spire.Pdf.PdfDocumentBase.Save(String filename)
   at Spire.Pdf.PdfDocumentBase.Save(String fileName, FileFormat fileformat)
   at Spire.Pdf.PdfDocument.SaveToFile(String filename, FileFormat fileFormat)
   at ConsoleApp2.Program.Main(String[] args) in C:\Users\someUser\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 63

What I am doing wrong here?

muhammad.ahmad@graymath.com
 
Posts: 1
Joined: Mon Dec 17, 2018 9:34 am

Tue Dec 18, 2018 3:25 am

Hi,

Thank you for your inquiry.
The issue is caused by the "GraphicsMode". If the mode is "SignImageAndSignDetail", an image is required by SignImageSource, yet I have noticed that you commented out the image source. If you do not need the image, change the graphics mode to "SignDetail" like below.
Code: Select all
signature.GraphicsMode = GraphicMode.SignDetail;


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Dec 19, 2018 8:39 am

Hi,

Greetings from e-iceblue!
Has your issue been resolved?
Your feedback would be greatly appreciated!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF