C# PDF Page Library
How to count, show pages total numbers in PDF using C#.NET code
C# PDF library to calculate, determine, get PDF pages number and display, show it in PDF document in C#
In this tutorial, you will learn how to count PDF total pages number, and show current page number with total page count on PDF pages in C#
- Calculate, get PDF document total pages number without opening it
- Show, display page number and total page count on each PDF page
How to count, show PDF page number programmatically using C#
PDF C# library also provides
c# read pdf page,
c# pdf scale,
how to crop pdf page and save as image in c#,
how to remove pages from pdf in c#,
add header and footer in pdf using c#.
How to count the total number of PDF pages in C#
It is really easy to count, get the total pages number of PDF without open it using C#. Below are the steps and C# sample code to count the PDF document total pages number.
- Create a PDFDocument object with a PDF file loaded
- Call method GetPageCount to get the total page number of PDF document
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
int pageCount = doc.GetPageCount();
Console.WriteLine("Page Count: " + pageCount);
How to show page number in PDF using C#
You can add PDF page number and total pages count displayed in the page header or footer.
For Automatic Page Number
- <<1>> : Page number only.
- <<1/n>> : Page number and total page count, with separator '/'.
You can easily display current page number and pdf total pages count on PDF header and footer using C#.
Below are the steps and C# sample code to show page number and total pages count of PDF file.
- Create a PDFDocument object with a PDF file loaded
- Create a new PDFPageHeaderFooter object
- Add current page footer and total pages count on the page footer
- Save the PDF file with page header footer applied
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_hdrftr.pdf";
// open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);
{
// define a header/footer setting
PDFPageHeaderFooter hdrftr1 = new PDFPageHeaderFooter();
// set center header field
hdrftr1.CenterHeaderField.Set(@"Title: *****", new Font("Arial", 12F, FontStyle.Regular), Color.Black);
// set left footer field
hdrftr1.LeftFooterField.Set("Page <<1/n>>", new Font("Arial", 9F, FontStyle.Regular), Color.DarkGray);
// define page range: all odd pages
PageRangeOptions pageRange1 = new PageRangeOptions();
pageRange1.AllPages = true;
pageRange1.Subset = PageRangeSubset.Odd;
// apply header/footer settings to all odd pages
PDFPageFieldHandler.ApplyHeaderFooter(doc, hdrftr1, pageRange1);
}
{
// define a header/footer setting
PDFPageHeaderFooter hdrftr2 = new PDFPageHeaderFooter();
// set center header field
hdrftr2.CenterHeaderField.Set(@"Title: *****", new Font("Arial", 12F, FontStyle.Regular), Color.Black);
// set right footer field
hdrftr2.RightFooterField.Set("Page <<1/n>>", new Font("Arial", 9F, FontStyle.Regular), Color.DarkGray);
// define page range: all even pages
PageRangeOptions pageRange2 = new PageRangeOptions();
pageRange2.AllPages = true;
pageRange2.Subset = PageRangeSubset.Even;
// apply header/footer settings to all even pages
PDFPageFieldHandler.ApplyHeaderFooter(doc, hdrftr2, pageRange2);
}
doc.Save(outputFilePath);