Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Count total number of page in PDF file in C# .NET 2.0?

Author: Zunnair
Download Source Code :

In this simple article you will learn that how you can count total number of pages in PDF File.

Simply just create New Project of windows application in C#.


Import some APIs


using System.IO;

using System.Text.RegularExpressions;


Drag one label, two buttons, one fileOpenDialog object and one textbox on the form.


Now write following code on form Load event


private void Form1_Load(object sender, EventArgs e)


            this.Text = "DevAsp PDF Page Counter";

            button1.Text = "Browse";

            button2.Text = "Count Pages";



Now write code on buttons events


private void button2_Click(object sender, EventArgs e)





private void button1_Click(object sender, EventArgs e)


            if (openFileDialog1.ShowDialog() == DialogResult.OK)


                textBox1.Text = openFileDialog1.FileName;




Now this is the function which count pages of PDF

public static int GetNoOfPagesPDF(string FileName)


            int result = 0;

            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            StreamReader r = new StreamReader(fs);

            string pdfText = r.ReadToEnd();


            System.Text.RegularExpressions.Regex regx = new Regex(@"/Type\s*/Page[^s]");

            System.Text.RegularExpressions.MatchCollection matches = regx.Matches(pdfText);

            result = matches.Count;

            return result;




In this funciton we are opening PDF file. and then using aDoc. Regex(@"/Type\s*/Page[^s]"); function we count total pages of PDF file.


This is the simple page counting source.


Article Comments
Its really simply superb

Thanks alot

Posted on 12/14/2009 1:24:54 PM by Sreekant

gr8t example

Posted on 1/20/2010 1:47:39 AM by sangram

In many cases it won't work. Many pdf docs don't have this "new page" tag and simply have a /Count tag at the end.

Posted on 3/8/2010 9:25:17 AM by Marcello

In some pdfs it does not give correct results. why ?

Posted on 4/23/2010 6:37:08 AM by Shafqt Ali

how to get more than one pdf file count

Posted on 5/10/2010 1:24:21 AM by naveen

Add Article Comment:
Name :
Email Address :
Comments :
<< Start/Stop Window service installed on your machine using System.ServiceProcess in C# .NET 2.0.

Disclaimer - Privacy
© 2002-2017