Resources » Code Snippets » C# Syntax

Count pages in a PDF file


Posted Date: 16-Oct-2008  Last Updated:   Category: C# Syntax    
Author: Member Level: Gold    Points: 10



This code shows how to count pages in a pdf file..


//Function for finding the number of pages in a given PDF file

string PgCount = string.Empty;
System.IO.FileInfo fextension = new FileInfo(vfileName);
string extension = fextension.Extension;
bool flag = UploadFile(vfileName);
if (extension == ".pdf" || extension == ".PDF")
{
FileStream fs = new FileStream(vfileName, 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);
if (flag == true)
{
PgCount = match.Count.ToString();
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Count pages in a PDF file"
Author: J Johnny    16 Oct 2009Member Level: Bronze   Points : 2

Dharmaraj's code logic was what I needed for my program. I'm using VB .Net instead C#. Below is the function I created in VB based on the above code.



Imports System.IO
Imports System.Text.RegularExpressions

Private Function pageCountPDF(ByRef pdfFile As FileInfo) As Integer
' Function for finding the number of pages in a given PDF file
' based on code found at http://www.dotnetspider.com/resources/21866-Count-pages-PDF-file.aspx

pageCountPDF = 0

If pdfFile.Exists Then
Dim fs As FileStream = New FileStream(pdfFile.FullName, FileMode.Open, FileAccess.Read)
Dim sr As StreamReader = New StreamReader(fs)
Dim pdfMagicNumber() As Char = "0000".ToArray

sr.Read(pdfMagicNumber, 0, 4) ' put the first for characters of
' the file into the pdfMagicNumber array

If pdfMagicNumber = "%PDF".ToArray Then 'The first four characters
' of a PDF file should start with %PDF
Dim pdfContents As String = sr.ReadToEnd()
Dim rx As Regex = New Regex("/Type\s/Page[^s]")
Dim match As MatchCollection = rx.Matches(pdfContents)
pageCountPDF = match.Count
Else
Throw New Exception("File does not appear to be a PDF file (magic number not found).")
End If
Else
Throw New Exception("File does not exist.")
End If
End Function



Guest Author: Fabry     04 May 2012
This code have this problem: if I delete a page with Acrobat write the number of page not change


Guest Author: mikegar     24 May 2012
When I try to compile this code I get the error "'ToArray' is not a member of 'string'". Running VS 2010 and IIS 7. Anyone found a way around this?


Guest Author: PraMaratha     15 Jul 2012
to: mikegar
You were getting error: ToArray' is not a member of 'string
because it should be ("0000").tochararray instead of "0000".toarray in VS2010
Try it for the other error also and post reply.



Guest Author: nobody     07 Sep 2012
I have better results with
Dim rx As Regex = New Regex("/Type/Page")



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India