When you want to know how many pages a PDF document has, you can get help from the PDF API Spire.PDF for .NET. Spire.PDF has powerful functions of working with pages. You can insert and remove blank pages from the PDF file, set PDF page properties and etc. This article will focus on show you how to get the number of pages of a PDF file.
Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
By using the Document class, you can use Count property of the Pages Collection of Document object to get the total number of pages in the document.
Here comes to the code snippet of get the number of pages of a PDF file:
using Spire.Pdf; using System; namespace GetNumberOfPages { class Program { static void Main(string[] args) { //create PDFDocument and load from file PdfDocument document = new PdfDocument(); string FileName = "sample.pdf"; document.LoadFromFile(FileName); //get page count int PageNumber = document.Pages.Count; Console.WriteLine("Page count: {0}", PageNumber); //remove one page of document document.Pages.RemoveAt(1); //get the number of pages PageNumber = document.Pages.Count; Console.WriteLine("Second page count:{0}", PageNumber); Console.ReadLine(); //close the document document.Close(); } } }
Effective Screenshot of the results when get the number of the pages.
The original PDF file: