By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,867 Members | 1,981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,867 IT Pros & Developers. It's quick & easy.

Can we write code to count pages in a file irrespective of its type?

P: 2
Hello,
I have to count the number of pages in the document that I am uploading. This document can be a word file, xls, txt or pdf. I have my code working for pdf but not for other types. Here is the code that works for pdf- it uses regex

Expand|Select|Wrap|Line Numbers
  1. System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  2.                 System.IO.StreamReader r = new System.IO.StreamReader(fs);
  3.                 string text = r.ReadToEnd();
  4.  
  5.                 System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
  6.                 System.Text.RegularExpressions.MatchCollection matches = regx.Matches(text);
  7.                 noOfPages = matches.Count;


please help me with this. I am totally stuck.
Aug 27 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.