Count pages in a PDF file

This code shows how to count pages in a pdf file..

//Function for finding the number of pages in a given PDF file

string PgCount = string.Empty;
System.IO.FileInfo fextension = new FileInfo(vfileName);
string extension = fextension.Extension;
bool flag = UploadFile(vfileName);
if (extension == ".pdf" || extension == ".PDF")
FileStream fs = new FileStream(vfileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string pdf = sr.ReadToEnd();
Regex rx = new Regex(@"/Type\s/Page[^s]");
MatchCollection match = rx.Matches(pdf);
if (flag == true)
PgCount = match.Count.ToString();


Author: J Johnny16 Oct 2009 Member Level: Bronze   Points : 2

Dharmaraj's code logic was what I needed for my program. I'm using VB .Net instead C#. Below is the function I created in VB based on the above code.

Imports System.IO
Imports System.Text.RegularExpressions

Private Function pageCountPDF(ByRef pdfFile As FileInfo) As Integer
' Function for finding the number of pages in a given PDF file
' based on code found at

pageCountPDF = 0

If pdfFile.Exists Then
Dim fs As FileStream = New FileStream(pdfFile.FullName, FileMode.Open, FileAccess.Read)
Dim sr As StreamReader = New StreamReader(fs)
Dim pdfMagicNumber() As Char = "0000".ToArray

sr.Read(pdfMagicNumber, 0, 4) ' put the first for characters of
' the file into the pdfMagicNumber array

If pdfMagicNumber = "%PDF".ToArray Then 'The first four characters
' of a PDF file should start with %PDF
Dim pdfContents As String = sr.ReadToEnd()
Dim rx As Regex = New Regex("/Type\s/Page[^s]")
Dim match As MatchCollection = rx.Matches(pdfContents)
pageCountPDF = match.Count
Throw New Exception("File does not appear to be a PDF file (magic number not found).")
End If
Throw New Exception("File does not exist.")
End If
End Function

Guest Author: Fabry04 May 2012

This code have this problem: if I delete a page with Acrobat write the number of page not change

Guest Author: mikegar24 May 2012

When I try to compile this code I get the error "'ToArray' is not a member of 'string'". Running VS 2010 and IIS 7. Anyone found a way around this?

Guest Author: PraMaratha15 Jul 2012

to: mikegar
You were getting error: ToArray' is not a member of 'string
because it should be ("0000").tochararray instead of "0000".toarray in VS2010
Try it for the other error also and post reply.

Guest Author: nobody07 Sep 2012

I have better results with
Dim rx As Regex = New Regex("/Type/Page")

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: