C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Count pages in a PDF file


Posted Date: 16 Oct 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 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();
}
}




Responses

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



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Pagecount for PDF  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: strip alpha numeric characters
Previous Resource: Calculating Age using C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use