Search code examples
c#htmlasp.netselectpdf

SelectPdf display footer only on custom pages


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?


Solution

  • 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