I'm using IronPDF to generate PDF documents in .NET. I wish to have different headers on odd and even pages.
Based on the IronPDF API presented, The headers and footers have to be the same on every page. E.g.
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// Add a header to every page easily
Renderer.RenderingOptions.FirstPageNumber = 1; // use 2 if a cover page will be appended
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "{url}";
Renderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
Renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.FontFamily = "Arial";
Renderer.RenderingOptions.TextFooter.FontSize = 10;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
Source: https://ironpdf.com/tutorials/html-to-pdf/
Is there a way of altering this behavior so that I can have custom Headers and Footers on even/odd pages or on specific pages?
CodePudding user response:
When rendering a PDF from HTML I agree that the Headers/Footers options are limited.
There is a Solution though: Add the headers and Footers AFTER the pdf has been rendered.
IronPDF rendering creates an
IronPDF.PdfDocument
class instance .This
IronPDF.PdfDocument
class instance has methods to add Headers and Footers only to specific page numbers.
This approach supports adding both Text and HTML based headers to PDFs.
MSDN Style Docs:
- IronPDF.PdfDocument.AddTextHeaders
- IronPDF.PdfDocument.AddTextFooters
- IronPDF.PdfDocument.AddHtmlHeaders
- IronPDF.PdfDocument.AddHtmlFooters
Code Example:
// PM> Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
using System.Linq;
//First create a PDF or Load an existing PDF using IronPdf.PdfDocument.Load()
var Renderer = new IronPdf.ChromePdfRenderer();
var multi_page_html = @"
<p> This is 1st Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 4th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 5th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 6th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 7th Page</p>";
using IronPdf.PdfDocument pdfdoc = Renderer.RenderHtmlAsPdf(multi_page_html);
// Create a Header
var header = new HtmlHeaderFooter();
header.HtmlFragment = "THIS IS HEADER {page} of {total-pages}";
// Create a Page Range 0 .. 7
var allPageIndexes = Enumerable.Range(0, pdfdoc.PageCount);
// Example 1
// Apply header to even page index only. (page number will be odd number because index start at 0 but page number start at 1)
var evenPageIndexes = allPageIndexes.Where(i => i % 2 == 0);
pdfdoc.AddHtmlHeaders(header, 1, evenPageIndexes);
pdfdoc.SaveAs("EvenPages.pdf");
// Example 2
//Apply header to odd page index only. (page number will be even number because index start at 0 but page number start at 1)
var oddPageIndexes = allPageIndexes.Where(i => i % 2 != 0);
pdfdoc.AddHtmlHeaders(header, 1, oddPageIndexes);
pdfdoc.SaveAs("OddPages.pdf");
// Example 3
// Apply header to last page only.
var lastPageIndex = new List<int>() { pdfdoc.PageCount - 1 };
pdfdoc.AddHtmlHeaders(header, 1, lastPageIndex);
pdfdoc.SaveAs("LastPageOnly.pdf");
// Example 4
// Apply header to first page only.
var firstPageIndex = new List<int>() { 0 };
pdfdoc.AddHtmlHeaders(header, 1, firstPageIndex);
pdfdoc.SaveAs("FirstPageOnly.pdf");
// Example 5
// Skip the first page.
var skipFirstPageIndexes = allPageIndexes.Skip(1);
var fileName = "SkipFirstPage.pdf";
pdfdoc.AddHtmlHeaders(header, 1, skipFirstPageIndexes);
pdfdoc.SaveAs("SkipFirstPage.pdf");
// Example 6
// Skip first page and make second pages label as 1.
var skipFirstPageIndexes = allPageIndexes.Skip(1);
pdfdoc.AddHtmlHeaders(header, 0, skipFirstPageIndexes);
pdfdoc.SaveAs("SkipFirstPageAndDontCountIt.pdf");
Source: https://ironpdf.com/examples/adding-headers-and-footers-advanced/ Related Question (but not identical): IronPdf - How to make headers and footers not cover page content when converting HTML to PDF?
CodePudding user response:
// PM> Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
using System.Linq;
//First create a PDF or Load an existing PDF using IronPdf.PdfDocument.Load()
var Renderer = new IronPdf.ChromePdfRenderer();
var multi_page_html = @"
<p> This is 1st Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 4th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 5th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 6th Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 7th Page</p>";
using IronPdf.PdfDocument pdfdoc = Renderer.RenderHtmlAsPdf(multi_page_html);
// Create a Header
var header = new HtmlHeaderFooter();
header.HtmlFragment = "THIS IS HEADER {page} of {total-pages}";
// Create a Page Range 0 .. 7
var allPageIndexes = Enumerable.Range(0, pdfdoc.PageCount);
// Example 1
// Apply header to even page index only. (page number will be odd number because index start at 0 but page number start at 1)
var evenPageIndexes = allPageIndexes.Where(i => i % 2 == 0);
pdfdoc.AddHtmlHeaders(header, 1, evenPageIndexes);
pdfdoc.SaveAs("EvenPages.pdf");
// Example 2
//Apply header to odd page index only. (page number will be even number because index start at 0 but page number start at 1)
var oddPageIndexes = allPageIndexes.Where(i => i % 2 != 0);
pdfdoc.AddHtmlHeaders(header, 1, oddPageIndexes);
pdfdoc.SaveAs("OddPages.pdf");