Add footer with left and right aligned text on same line

Add a footer to a PDF document that has left aligned text with a page number and right aligned text on the same line.

Footer Left Right

Document pdf = new Document();
Section section = pdf.Sections.Add();

// this bottom margin of the section determines the space available for the footer
section.Margin.Bottom = 100; 
double width = section.PageSize.Width - section.Margin.Left - section.Margin.Right;

// filler text 
for (int i=0; i<100; i++)
   TextParagraph text = new TextParagraph();
   text.SpacingAfter = 12;
   text.Fragments.Add(new Fragment("Generate PDF documents from scratch. Use code, XML/XSL or a combination."));

Footer footer = new Footer();
section.Footer = footer;

// the trick to have both left and right aligned
// text on the same line is to use a table
Table table = new Table();

// the footer will be top-aligned so to move it down you need
// to add spacing before the first paragraph of the footer
table.SpacingBefore = 12;
Row row = table.Rows.Add();

// left text         
Cell leftCell = row.Cells.Add();
leftCell.PreferredWidth = width / 2;
TextParagraph leftText = new TextParagraph();
Fragment pageNumber = new Fragment("Page #p");
pageNumber.HasContextFields = true;

// right text         
Cell rightCell = row.Cells.Add();
rightCell.PreferredWidth = width / 2;
TextParagraph rightText = new TextParagraph();
rightText.HorizontalAlignment = HorizontalAlignment.Right;
Fragment title = new Fragment("TallPDF.NET");

string path = "out.pdf";
using (FileStream file = new FileStream(path, FileMode.Create))

Download TallPDF.NET 4.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.