Friday, February 12, 2010

How to count the no.of pages in a PDF file using C#.Net?

string PgCount = string.Empty;
string filename = @"D:\Kannabiran\Test.pdf";
System.IO.FileInfo fextension = new FileInfo(filename);
string extension = fextension.Extension;
if (extension == ".pdf" || extension == ".PDF")
{
FileStream fs = new FileStream(filename, 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);
PgCount = match.Count.ToString();
}

No comments:

 
Feedback Form