During one of my .NET projects working with Adobe PDF files, I encountered the need to simply retrieve the page count of a specific file. I did not need to manipulate the PDF at all so buying a .NET component for this task sounded a little inconvenient.
After a few hours of researching for an easy solution, I found out that the old regular expressions might hold the answer.
Opening the PDF in Notepad, I noticed that for each page in the file there is a specific character sequence: "/Type /Page" (depending on the PDF version with or without the space between the two words). So, all we need to do is to count how many times this sequence repeats in the file.
Getting It Done !
First, we need to open the PDF file using a
FileStream and read the contents as a
string using a
FileStream fs = new FileStream(@"c:\a.pdf", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
Once we have the PDF text, all we need to do is to create the regular expression and count the matches.
Regex rx1 = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = rx1.Matches(pdfText);
MessageBox.Show("The PDF file has " + matches.Count.ToString() + " page(s).";
- 11th July, 2006: Initial post
Vicente Angotti has been working as a software developer for more than 12 years and he is currently an IT project manager for a Federal U.S. Government Agency.
Some of his recent projects include TCP/IP communications, Asynchronous Threading, Image Manipulation and VLDB Design using .NET.