- How to add page numbers to your PDF
- Add text field to PDF
- Append multiple PDF documents
- Bulleted list from XML and XSL
- Merge PDF
- Calculate the height of a paragraph in PDF
- Multipage TIFF to PDF
- Convert TXT to PDF
- Convert XHTML to PDF
- Create PDF in C# - Tall Components - Check out our PDF code samples
- Text formatting
- Generate PDF form from XML
- Generate PDF with local images from XML with Xamarin.iOS
- XhtmlParagraph and TrueType fonts
- Add footer with left and right aligned text on same line
- Read and write meta data from PDF
- Stitch PDF documents
- Use multiple licenses
- What is the resulting fontsize in PDF rich text used in SimpleXhtmlShape
How to add page numbers to your PDF
This article shows how to add page numbers to PDF using TallPDF built-in features.
We will build a simple structure based on TallPDF’s objects and using html content create a paged PDF document.
Code sample to add page numbers to PDF in C#:
private static void Main(string[] args)
{
// prepare our document
Document document = new Document();
// document may have one or more section, create one here
Section section = new Section();
document.Sections.Add(section);
// adding a fragment where page number will be generated
Fragment pagenumbers = new Fragment("#p/#P", 12);
pagenumbers.HasContextFields = true;
TextParagraph textParagraph = new TextParagraph();
textParagraph.Fragments.Add(pagenumbers);
// footer will be repeated in each page, a good place to put our page number
section.Footer = new Footer();
section.Footer.Paragraphs.Add(textParagraph);
using (FileStream inputFile = File.Open("input.html", FileMode.Open))
{
// add page content to the section here
XhtmlParagraph htmlParagraph = new XhtmlParagraph(inputFile);
section.Paragraphs.Add(htmlParagraph);
}
// save the result to PDF
using (Stream stream = new FileStream("out.pdf", FileMode.Create))
{
document.Write(stream);
}
}