Skip to footer content
PRODUCT COMPARISONS

IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Documents

Creating professional PDF documents with consistent headers and footers is essential for business reports, invoices, and documentation. If you're working with iTextSharp to add headers and footers to your PDF files, you know the process involves implementing page headers and managing complex positioning code. Let's explore how IronPDF simplifies this process while comparing both approaches.

Working with iTextSharp requires implementing the PdfPageEventHelper class and overriding the OnEndPage method to add headers and footers. This approach involves direct manipulation of the PdfContentByte object and precise coordinate calculations.

public class HeaderFooterEvent : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        PdfContentByte cb = writer.DirectContent;
        // Add header with text
        ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER,
            new Phrase("Company Report", new Font(Font.FontFamily.HELVETICA, 12)),
            document.PageSize.Width / 2, document.PageSize.Height - 30, 0);
        // Add footer with page numbers
        string footerText = $"Page {writer.PageNumber}";
        ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT,
            new Phrase(footerText, new Font(Font.FontFamily.HELVETICA, 10)),
            document.PageSize.Width - 40, 30, 0);
    }
}
// Usage
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.PageEvent = new HeaderFooterEvent();
public class HeaderFooterEvent : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        PdfContentByte cb = writer.DirectContent;
        // Add header with text
        ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER,
            new Phrase("Company Report", new Font(Font.FontFamily.HELVETICA, 12)),
            document.PageSize.Width / 2, document.PageSize.Height - 30, 0);
        // Add footer with page numbers
        string footerText = $"Page {writer.PageNumber}";
        ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT,
            new Phrase(footerText, new Font(Font.FontFamily.HELVETICA, 10)),
            document.PageSize.Width - 40, 30, 0);
    }
}
// Usage
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.PageEvent = new HeaderFooterEvent();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

PDF with Headers and Footers

IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Documents: Image 1 - Output PDF with headers and footers

This code demonstrates the manual positioning required with iTextSharp—you need to calculate exact coordinates, manage fonts separately, and handle the rendering through DirectContent.

IronPDF transforms the header and footer creation process with its intuitive API. Instead of implementing event handlers, you configure headers and footers through simple property settings on the ChromePdfRenderer:

var renderer = new ChromePdfRenderer();
// Add text header
renderer.RenderingOptions.TextHeader.CenterText = "Company Report";
renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add text footer with page numbers
renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
renderer.RenderingOptions.TextFooter.LeftText = "{date}";
renderer.RenderingOptions.TextFooter.FontSize = 10;
var PDF = renderer.RenderHtmlAsPdf(htmlContent);
var renderer = new ChromePdfRenderer();
// Add text header
renderer.RenderingOptions.TextHeader.CenterText = "Company Report";
renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add text footer with page numbers
renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
renderer.RenderingOptions.TextFooter.LeftText = "{date}";
renderer.RenderingOptions.TextFooter.FontSize = 10;
var PDF = renderer.RenderHtmlAsPdf(htmlContent);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDF Output with Headers and Footers

IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Documents: Image 2 - IronPDF PDF output with headers and footers

The difference is immediately apparent - IronPDF handles positioning, margins, and rendering automatically while providing built-in placeholders for dynamic content.

Feature iTextSharp IronPDF
Implementation Method PdfPageEventHelper class RenderingOptions properties
Code Complexity Complex coordinate calculations Simple property assignment
Page Numbers Manual tracking with writer.PageNumber Built-in {page} placeholder
HTML Support Limited, requires XMLWorker Native HTML header support
Margin Management Manual calculation Automatic adjustment
Dynamic Content Custom implementation Predefined placeholders
Learning Curve Steep Gentle

How to Add Headers and Footers with Page Numbers?

Page numbering is a common requirement for PDF documents. With iTextSharp, you must track the current page number and total number of pages manually:

// iTextSharp approach
public override void OnEndPage(PdfWriter writer, Document document)
{
    PdfPTable footerTable = new PdfPTable(3);
    footerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
    PdfPCell leftCell = new PdfPCell(new Phrase(DateTime.Now.ToString("dd/MM/yyyy")));
    PdfPCell centerCell = new PdfPCell(new Phrase("Confidential"));
    PdfPCell rightCell = new PdfPCell(new Phrase($"Page {writer.PageNumber}"));
    footerTable.AddCell(leftCell);
    footerTable.AddCell(centerCell);
    footerTable.AddCell(rightCell);
    footerTable.WriteSelectedRows(0, -1, document.LeftMargin,
        document.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);
}
// iTextSharp approach
public override void OnEndPage(PdfWriter writer, Document document)
{
    PdfPTable footerTable = new PdfPTable(3);
    footerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
    PdfPCell leftCell = new PdfPCell(new Phrase(DateTime.Now.ToString("dd/MM/yyyy")));
    PdfPCell centerCell = new PdfPCell(new Phrase("Confidential"));
    PdfPCell rightCell = new PdfPCell(new Phrase($"Page {writer.PageNumber}"));
    footerTable.AddCell(leftCell);
    footerTable.AddCell(centerCell);
    footerTable.AddCell(rightCell);
    footerTable.WriteSelectedRows(0, -1, document.LeftMargin,
        document.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDF streamlines this with built-in placeholders:

// IronPDF approach
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
renderer.RenderingOptions.TextFooter.CenterText = "Confidential";
renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
renderer.RenderingOptions.MarginBottom = 25; // Ensure proper spacing
// IronPDF approach
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
renderer.RenderingOptions.TextFooter.CenterText = "Confidential";
renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
renderer.RenderingOptions.MarginBottom = 25; // Ensure proper spacing
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Can You Create HTML Headers with Dynamic Content?

For complex layouts with company logos, styled text, or tables, HTML headers provide superior flexibility. IronPDF excels here with native HTML support:

// IronPDF HTML header with company logo
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = @"<div style='text-align: center;'>
                      <img src='logo.png' height='30'>
                      <h3>Annual Report 2024</h3>
                    </div>",
    MaxHeight = 50,
    BaseUrl = "https://mycompany.com/assets/"
};
// HTML footer with styled page numbers
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>Page {page} of {total-pages}</i></center>",
    DrawDividerLine = true
};
// IronPDF HTML header with company logo
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = @"<div style='text-align: center;'>
                      <img src='logo.png' height='30'>
                      <h3>Annual Report 2024</h3>
                    </div>",
    MaxHeight = 50,
    BaseUrl = "https://mycompany.com/assets/"
};
// HTML footer with styled page numbers
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>Page {page} of {total-pages}</i></center>",
    DrawDividerLine = true
};
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Header with Logo Image

IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Documents: Image 3 - Logo image in document headers

With iTextSharp, achieving HTML headers requires additional libraries like XMLWorker and complex parsing code.

Which Approach Offers Better Performance and Flexibility?

While both libraries handle headers and footers effectively, IronPDF's approach reduces development time significantly. The library manages predefined margins automatically, adjusting layout to prevent overlapping content.

IronPDF's licensing model provides commercial-friendly terms compared to iTextSharp's AGPL license, which requires open-sourcing your application unless you purchase a commercial license.

For developers already familiar with iTextSharp's page event system, the learning curve exists, but IronPDF's documentation and examples make the transition smooth. The ability to use CSS for styling and HTML for layout opens possibilities that would require extensive custom code in iTextSharp.

Conclusion

Adding headers and footers to new document instances doesn't have to involve complex event handling and coordinate calculations. While iTextSharp's approach offers granular control through page events and direct content manipulation, IronPDF provides a more intuitive solution with its property-based configuration and native HTML support.

The choice becomes clear when considering development speed, maintainability, and the ease of creating professional-looking documents. IronPDF's approach to headers and footers exemplifies modern PDF generation, powerful yet accessible.

Ready to simplify your PDF header and footer creation? Start your free trial and experience the difference. For teams looking to streamline their document generation workflow, explore our licensing options to find the perfect fit for your project.

Frequently Asked Questions

What are the main differences between IronPDF and iTextSharp for adding headers and footers?

IronPDF simplifies the process of adding headers and footers to PDFs by allowing HTML headers, which can be more intuitive and flexible compared to iTextSharp's positioning code.

How does IronPDF simplify adding page headers?

IronPDF allows you to use HTML for creating page headers, enabling a more straightforward and familiar coding experience compared to the complex positioning code required by iTextSharp.

Is it possible to use HTML to customize headers in IronPDF?

Yes, IronPDF supports HTML-based headers and footers, making it easier to customize and style them as needed.

What are the benefits of using IronPDF for business reports?

IronPDF streamlines the creation of consistent headers and footers in business reports, reducing the complexity and time required for implementation, especially when compared to iTextSharp.

Can IronPDF handle page numbering in headers and footers?

Yes, IronPDF can manage page numbering within headers and footers seamlessly, allowing for professional document presentation.

How does IronPDF compare to iTextSharp in terms of ease of use?

IronPDF is generally considered easier to use due to its support for HTML elements in headers and footers, contrasting with iTextSharp's more complex coding requirements.

Does IronPDF support dynamic content in headers?

Yes, IronPDF can incorporate dynamic content such as dates or page numbers into headers and footers using HTML.

What makes IronPDF more suitable for documentation projects?

IronPDF's ability to integrate HTML for headers and footers makes it ideal for documentation projects where consistent styling and ease of updating are important.

Can I use IronPDF for invoices with custom headers?

Absolutely, IronPDF is well-suited for creating invoices with custom headers, thanks to its HTML support and ease of implementation.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More