How can I get number of pages in a pdf file in AxAcroPdf?

Ali Ahmadi picture Ali Ahmadi · Jun 8, 2012 · Viewed 9k times · Source

I use AxAcroPdf to display PDF file with this code :

AcroPdfViewer.src = FilePath;
AcroPdfViewer.setPageMode("none");
AcroPdfViewer.setZoom(100);
AcroPdfViewer.setShowToolbar(true);

How can I get number of total pages of the PDF file in AxAcroPdf ?

Answer

Luis Enrique Acosta Rios picture Luis Enrique Acosta Rios · Nov 4, 2014

I think the best way to perform a pdf page count is the following:

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;

    }

Hope it helps ;)

Source: Counting PDF Pages