| Author: J Johnny 16 Oct 2009 | Member 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
|