Search code examples
pdfpdf-extraction

Counting the pages in a PDF file


I know of several tools/libraries that can do this but I want to know if this is possible with just opening up the file as a text file and looking for a keyword.


Solution

  • have a look at this: http://www.freevbcode.com/ShowCode.asp?ID=8153
    Edit: not work, may be too old
    Found this:

    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;
            }
    

    Ps: tested! It works.see here source