Use Aspose.PDF for .NET to Sign the PDF files - Binding fails

Hello,
I just discovered Aspose products, and I’m trying to debug this code below (in a C#.NET Framewokr 4.7.2 solution) :

            Pdf pdf = new Pdf();
            Section section = pdf.Sections.Add();
            Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(section, html);
            text.IsHtmlTagSupported = true;
            text.IsHtml5Supported = true;
            section.Paragraphs.Add(text);

            Aspose.Pdf.Generator.HeaderFooter hHeader = new Aspose.Pdf.Generator.HeaderFooter(section);
            section.OddHeader = hHeader;
            section.EvenHeader = hHeader;
            Aspose.Pdf.Generator.Image tHeader = new Aspose.Pdf.Generator.Image(section);
            ImageInfo info = new ImageInfo();
            info.File = LogoApplicationUrl;
            info.FixWidth = 200;
            tHeader.ImageInfo = info;
            hHeader.Paragraphs.Add(tHeader);

            PKCS1 signature = new PKCS1(Server.MapPath(Url.Content("~/Signatures/MrsSmith.pfx")), "Key");
            signature.Reason = Const.SIGNATURE_REASON;
            signature.ShowProperties = false;
            signature.Date = [value];

            PdfFileSignature pdfSign = new PdfFileSignature();
            pdfSign.BindPdf(pdf);
            pdfSign.SignatureAppearance = Server.MapPath(Url.Content("~/Images/Logo.jpg"));
            pdfSign.Sign(1, Const.SIGNATURE_REASON, string.Empty, string.Empty, true, rect, signature);

            MemoryStream output = new MemoryStream();
            pdfSign.Save(output);
            output.Position = 0;

            return output;

There is a first bug in line:
pdfSign.BindPdf(pdf); --> ‘Server not found 404’

If I comment the Signature block at the end, another error occurs on the line:
pdfSign.Save(output); --> : NullReferenceException - Exception message: The facade is not initialized correctly. Please provide pdf document to the process.

I spent hours seeking for similar issues in the Internet, but I found nothing.
Except advice about the Aspose version (I work on old version 4.0.0, but I cannot upgrade it)

Could you please help?

Thank you in advance

Ok I guess I fixed the issue:
I replaced the binding with an explicit path:

pdfSign.BindPdf(@“C:\Temp\test.pdf”);

@tteboul

It is good to know that you were able to resolve your issue. However, it seems you are using an older version of the API as Aspose.Pdf.Generator Approach has been discontinued. Please try to use the latest version of the API which implements DOM approach which is much faster and improved to work with PDF files. For more information, please check the below section of the API documentation:

Hello Ali,
Thank you for your reply, unfortunately I won’t be able to upgrade the Aspose version for now in my solution…
Moreover, I realized that the first version of code I shared ( pdfSign.BindPdf(pdf); ) must not be changed. Maybe there is a missing file somewhere in my project…
Could you please help? What can be missing for the BindPdf method to run properly?
Thank you in advance :slight_smile:

@tteboul

It is hard to guess the issue which could be occurring at your end since you are using a quite older version of the API. You can try pdf.Save() method before calling the BindPdf() method.

Ok Ali the issue is definitely solved.
Some picture was not found here

pdfSign.SignatureAppearance = Server.MapPath(Url.Content("~/Images/Logo.jpg"));

but I obtained a Error 404 and no clue about the line concerned…
Thank you and best regards

@muton

It is good to know that your issue has been resolved. The API also throws exception at the time of Save() method when any of the used resource is not present. Please keep using our API and try to upgrade to the latest version. Feel free to contact us in case you need any assistance.