2

I'm using SelectPDF to convert an HTML template into a PDF. The design I'm following requires to display a footer only on the first page.

The documentation does not cover custom pages display.

Anyone tried this before?

Lorena Sfăt
  • 215
  • 2
  • 7
  • 18

1 Answers1

2

As you can see the documentation you can create a custom HTML document, it name is footer.html. This document will contains your custom footer.

This is the initialization.

string footerUrl = Server.MapPath("~/files/footer.html");

After that you have to init the converter object and set it.

HtmlToPdf converter = new HtmlToPdf();
converter.Options.DisplayFooter = true;
converter.Footer.DisplayOnFirstPage = true;

PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

UPDATE:

This piece of code create a new pdf from URL and add custom footer for the first page.

PdfDocument doc = converter.ConvertUrl(@"https://en.wikipedia.org/wiki/Chernobyl_disaster");
            PdfPage page = doc.Pages[0];

            PdfTemplate customFooter = doc.AddTemplate(
                page.PageSize.Width, 20);
            PdfHtmlElement customHtml = new PdfHtmlElement(
                "<div><b>This is the custom footer that will " +
                "appear only on page 1!</b></div>",
                string.Empty);
            customFooter.Add(customHtml);

            page.CustomFooter = customFooter;


            doc.Save("Test.pdf");
            doc.Close();

I hope it will help you Cheers

harmath
  • 183
  • 5
  • Thanks, but my issue is not 'adding the footer to the first page *too*', but to *only* display it on the first page. – Lorena Sfăt Aug 29 '19 at 12:39
  • Many thanks. This actually looks promising. The problem I have now is that `PdfHtmlElement` is not recognized as a class. Am I using the wrong nuget? I have Select.HtmlToPdf and Select.HtmlToPdf.NetCore - also not sure if I really need both. – Lorena Sfăt Aug 30 '19 at 06:32
  • Turns out that's only available in Select.Pdf(Full version) but not in Select.HtmlToPdf(Free verision) - correct me if I'm wrong. The problem with the first one is that it comes with a beautifully red watermark :( `Demo Version - Select.Pdf SDK` – Lorena Sfăt Aug 30 '19 at 07:50
  • I use version of Select.Pdf.19.1.0 (Free version), you can achive: PM> Install-Package Select.Pdf -Version 19.1.0. – harmath Aug 30 '19 at 19:52
  • 1
    Yes you are right you need just one, if you use ASP.Net you can use Select.Pdf. All in all I think you use wrong NuGet please try what I recommended for you. Unfortunatelly the free version always show the red watermark. – harmath Aug 30 '19 at 19:59
  • Thanks for all the replies. This was really helpful :) – Lorena Sfăt Sep 02 '19 at 09:19