In this article I will explain with an example, how to add Footer to existing PDF using iTextSharp in ASP.Net Core (.Net Core 7) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Download iTextSharp and XmlWorkerHelper Libraries

You can download the iTextSharp and XmlWorkerHelper libraries from the following links.
Note: You will need to add the reference of iTextSharp and XmlWorkerHelper libraries in your project.
 
 

PDF File Location

The sample PDF file is located inside the PDF Folder (Directory) of wwwroot Folder (Directory).
ASP.Net Core Razor Pages: Add Footer to existing PDF using iTextSharp
 
 

Namespaces

You will need to import the following namespaces.
using iTextSharp.text;
using iTextSharp.text.pdf; 
 
 

Index PageModel (Code-Behind)

Inside the PageModel, first the private property IWebHostEnvironment interface is created.
Then, the interface is injected into the Constructor (IndexModel) using Dependency Injection method and the injected object is assigned to private property (created earlier).
The PageModel consists of following Handler method.

Handler method for handling GET operation

This Handler method left empty as it is not required.
 

Handler method for handling POST operation

Inside this Handler method, first the BYTE Array of the sample PDF file (explained earlier) is determined.
Note: For more details about IWebHostEnvironment interface, please refer my article Using IWebHostEnvironment in ASP.Net Core. For more details on how to access Static Files in Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Then, an object of PdfReader class is created which accepts BYTE Array of the sample PDF file as a parameter.
A PdfStamper class object is also created which accepts objects of PdfReader and MemoryStream class as a parameter.
After that, PDF document is created using existing PDF file and a FOR loop is executed over all the pages.

Adding Footer

Inside the loop, a Table is created for Header using PdfPTable class object and the its cell value is defined using PdfPCell class.
After defining Width, Position and Border, the Table is written to the PDF document using WriteSelectedRows method.
The WriteSelectedRows method accepts the necessary properties and the it is placed at top using GetBottom method which accepts the BottomMargin as parameter.
Finally, the File function is called which accepts the BYTE Array of the PDF document and the name of the file, which initiates the file download operation.
public class IndexModel : PageModel
{
    private IWebHostEnvironment Environment{ get; }
    public IndexModel(IWebHostEnvironment _environment)
    {
        this.Environment = _environment;
    }
 
    public void OnGet()
    {
    }
 
    public FileResult OnPostExport()
    {
        byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(this.Environment.WebRootPath, "PDF\\Sample.pdf"));
 
        using (MemoryStream stream = new MemoryStream())
        {
            // Reading pdf.
            PdfReader  reader =  new PdfReader(bytes);
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {
                // Create PDF document from exisitng file.
                Document document = new Document(PageSize.A4, 10f, 10f, 140f, 40f);
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
 
                // Loop through all pages.
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfContentByte pdfContentByte stamper.GetUnderContent(i);
 
                    // AddingPdfTable for Footer.
                    PdfPTable tblFooter = new PdfPTable(2);
 
                    // Setting table width.
                    tblFooter.TotalWidth document.PageSize.Width - document.LeftMargin - document.RightMargin;
 
                    // Setting cell width.
                    tblFooter.SetWidths(new float[] { 80f, 20f });
                    tblFooter.DefaultCell.Border = 0;
 
                    // Adding table cell.
                    PdfPCell cell = new PdfPCell(new Paragraph(new Chunk("https://www.aspsnippets.com/", FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.BLUE))));
                    cell.HorizontalAlignment Element.ALIGN_CENTER;
                    cell.Border = 0;
                    tblFooter.AddCell(cell);
 
                    // Adding Page Number cell.
                    cell = new PdfPCell(new Paragraph(string.Format("Page Number: {0}", i)));
                    cell.HorizontalAlignment Element.ALIGN_RIGHT;
                    cell.Border = 0;
                    tblFooter.AddCell(cell);
 
                    // Adding table to pdf footer.
                    tblFooter.WriteSelectedRows(0, -1,document.LeftMargin, writer.PageSize.GetBottom(document.BottomMargin), pdfContentByte);
                }
            }
 
            return File(stream.ToArray(), "Sample_Footer.pdf");
        }
    }
} 
 
 

Razor Page (HTML)

HTML Markup

Inside the Razor Page the ASP.Net TagHelpers is inherited.
The HTML of Razor Page consists of a Form which consists of a Submit Button which when clicked, the Form is submitted.
Note: In the Razor PageModel, the Handler method name is OnPostExport but here it will be specified as Export when calling from the Razor HTML Page.
 
@page
@model iTextSharp_Add_Footer_Core_Razor.Pages.IndexModel
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        <input id="btnSubmit" type="submit" value="Export" asp-page-handler="Export" /> 
    </form>
</body>
</html> 
 
 

Screenshots

PDF before Exporting

ASP.Net Core Razor Pages: Add Footer to existing PDF using iTextSharp
 

Form

ASP.Net Core Razor Pages: Add Footer to existing PDF using iTextSharp
 

PDF after Exporting

ASP.Net Core Razor Pages: Add Footer to existing PDF using iTextSharp
 
 

Downloads