Wednesday, March 24, 2010

Count total number of pages in pdf file (C#)

It is actually a simple solution to get a total number of pages in pdf file. I found it from devasp and I think it's worth to share.

using System.IO;
using System.Text.RegularExpressions;

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;

1 comment:

Peter Taylor said...

thnx for sharing such a valuable information….I am using zetpdf to generate pdf in…I would also suggest you to use zetpdf…It makes the job easy..