Hello,
Hope everyone is doing fine.
I am converting URL (HTML Content) to pdf using HtmlToPdf converter (selectPdf) library and attaching that pdf in user's email as attachment. That pdf can be 7 or 8 pages, coz the content is dynamic
I want pagination at the end of each page and footer with static content at the end of each page, meaning each page end should show page number and some text (Ex. ABC Company)
How to achieve it in dot net with SelectPdf library?
or any alternate solution will be fine.
Thanks in advance

Lokesh VarmanRank #374 · 6.8K reputationPosted Feb 13, 2024, 10:19 PM EST
To achieve pagination and add a footer with page numbers and static content to each page of a PDF using the SelectPdf library in .NET, you can follow these steps:
Here's a sample code demonstrating how to achieve this:
using System; using SelectPdf; class Program { static void Main(string[] args) { // Create a new PDF document PdfDocument doc = new PdfDocument(); // Add a new page PdfPage page = doc.AddPage(); // Create a HTML to PDF converter HtmlToPdf converter = new HtmlToPdf(); // Set options (optional) converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.MarginBottom = 30; // Adjust as needed converter.Options.MarginTop = 30; // Adjust as needed // Add header and footer converter.Header.DisplayOnFirstPage = true; converter.Footer.DisplayOnFirstPage = true; // Add pagination PdfTextSection text = new PdfTextSection(0, 0, "Page {page_number} of {total_pages}"); text.TextAlign = PdfTextHorizontalAlign.Center; converter.Footer.Add(text); // Add static footer content PdfTextSection footerText = new PdfTextSection(0, 0, "ABC Company"); footerText.TextAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(footerText); // Convert HTML content to PDF PdfDocument pdf = converter.ConvertUrlToPdf("your_html_url"); // Append the PDF document to the existing one foreach (PdfPage pdfPage in pdf.Pages) { doc.AddPage(pdfPage); } // Save the PDF document doc.Save("output.pdf"); // Close the PDF document doc.Close(); // Close the HTML to PDF converter converter.Close(); } }Replace
"your_html_url"with the URL of the HTML content you want to convert to PDF. Adjust the margins, header, footer, and other options according to your requirements.This code will create a PDF document with pagination at the bottom of each page and a footer with the static content "ABC Company" at the bottom right corner of each page.
Note : AI Generated
Sushant TorankarRank #3,026 · 476 reputationPosted Feb 14, 2024, 9:24 AM EST
Thank you,
Sure I will try this
Abhishek YadavRank #217 · 11.3K reputationPosted Feb 16, 2024, 12:51 AM EST
To achieve pagination and footer with static content at the end of each page when converting HTML content to PDF using the SelectPdf library in .NET, you can follow these steps:
string url = "http://example.com/content.html"; PdfPage page = doc.AddPage(); HtmlToPdf converter = new HtmlToPdf(); converter.Options.DrawBackground = true; // Optional: set if you want to include backgrounds converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; PdfDocument contentDoc = converter.ConvertUrl(url); foreach (PdfPage contentPage in contentDoc.Pages) { doc.AddPage(contentPage); } contentDoc.Close();foreach (PdfPage pdfPage in doc.Pages) { int pageNumber = doc.Pages.IndexOf(pdfPage) + 1; string footerText = $"Page {pageNumber} - ABC Company"; HtmlToPdfElement footer = new HtmlToPdfElement(footerText); footer.FitHeight = true; footer.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; footer.Location = new PdfPoint(10, 10); pdfPage.AddElement(footer); }Leon DRank #851 · 2.6K reputationPosted Feb 23, 2024, 4:45 AM EST
You can use Spire.PDF to convert HTML to PDF and add header to pdf.
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Header-and-Footer/Add-Footer-to-Existing-PDF-in-C-VB.NET.html
Sushant TorankarRank #3,026 · 476 reputationPosted Feb 15, 2024, 3:03 AM EST
Hi Lokesh,
I tried below code but it's not working. You can see as in below image page is split to 2 diff page, but don't see any footer or paging