dotnetspider.com


 


TutorialsForumResourcesReviewsJobsInterviewVideosCommunitiesProjectsTraining

Subscribe to Subscribers


Online MembersSankar
Sunitha
cloud
Rajalingam
SivaSaiKrishna
Ashokkumar
er.g.ilango
Anu George
Krishna Kant
parameswaran
chanti
More...




Resources » Code Snippets » C# Syntax


Count pages in a PDF file


Posted Date:     Category: C# Syntax    Rating: 1 out of 5
Author: Member Level: Gold    Points: 10 (Rs 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


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.


    Next Resource: strip alpha numeric characters
    Previous Resource: Calculating Age using C#
    Return to Resources
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Pagecount for PDF  .  

    My Profile

    Active Members
    TodayLast 7 Daysmore...


    Awards & Gifts


    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds



    About Us    Trademark Disclaimer    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.