In this article I will explain with an example, how to add Footer to existing
PDF using
iTextSharp in ASP.Net with C# and VB.Net.
Download iTextSharp and XmlWorkerHelper Libraries
You can download the iTextSharp and XmlWorkerHelper libraries from the following links.
PDF File Location
The sample PDF file is located inside the PDF Folder (Directory) of ASP.Net project.
HTML Markup
The HTML Markup consists of following control:
Button – For exporting PDF.
The Button has been assigned with an OnClick event handler.
<asp:Button runat="server" Text="Export" OnClick="OnExport" />
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Adding Footer to existing PDF using C# and VB.Net
When the Export Button is clicked, first the BYTE Array of the sample PDF file (explained earlier) is determined.
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.
Then, the Response class properties are set.
1. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
2. ContentType – It informs the Browser about the file type. In this case it is PDF.
Finally, the PDF document is written to the Response which initiates the download operation.
C#
protected void OnExport(object sender, EventArgs e)
{
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/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);
}
}
bytesbytes = stream.ToArray();
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment;filename=Sample_Footer.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
VB.Net
Protected Sub OnExport(sender As Object, e As EventArgs)
Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/PDF/Sample.pdf"))
Using stream As MemoryStream = New MemoryStream()
' Reading pdf.
Dim reader As PdfReader = New PdfReader(bytes)
Using stamper As PdfStamper = New PdfStamper(reader, stream)
' Create PDF document from exisitng file.
Dim document As Document = New Document(PageSize.A4, 10.0F, 10.0F, 140.0F, 40.0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, stream)
' Loop through all pages.
For i As Integer = 1 To reader.NumberOfPages
Dim pdfContentByte As PdfContentByte = stamper.GetUnderContent(i)
' AddingPdfTable for Footer.
Dim tblFooter As PdfPTable = New PdfPTable(2)
' Setting table width.
tblFooter.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin
' Setting cell width.
tblFooter.SetWidths(New Single() {80.0F, 20.0F})
tblFooter.DefaultCell.Border = 0
' Adding table cell.
Dim cell As PdfPCell = 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)
Next
End Using
bytes = stream.ToArray()
End Using
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.AddHeader("content-disposition", "attachment;filename=Sample_Footer.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End Sub
Screenshots
PDF before Exporting
Form
PDF after Exporting
Downloads