title: Inconsistent page count of a PDF document
body: <p>I'm trying to get the number of pages in the PDF document. Some of my PDFs are created in Word (saved as PDF), some of them are Xeroxed into the directory (not sure if this matters).</p>
  
  <p>After hours of research I've come to find out that this is easier said than done. <strong>The page count rarely comes back giving me the correct number of pages</strong>, even though most PDF's do in fact have <code>/Count</code> inside the Binary Code.</p>
  
  <p>For example I've used the following code; it is supposed to open the document in Binary Mode, look for <code>/Count</code> or <code>/N</code> and get the number next to it which is supposed to give me the page count.</p>
  
  <pre><code>Public Sub pagecount(sfilename As String)
      On Error GoTo a
      Dim nFileNum As Integer
      Dim s As String
      Dim c As Integer
      Dim pos, pos1 As Integer
      pos = 0
      pos1 = 0
      c = 0
      ' Get an available file number from the system
      nFileNum = FreeFile
      'OPEN the PDF file in Binary mode
      Open sfilename For Binary Lock Read Write As #nFileNum
      ' Get the data from the file
      Do Until EOF(nFileNum)
      Input #1, s
      c = c + 1
      If c &lt;= 10 Then
          pos = InStr(s, "/N")
      End If
      pos1 = InStr(s, "/count")
         If pos &gt; 0 Or pos1 &gt; 0 Then
              Close #nFileNum
              s = Trim(Mid(s, pos, 10))
              s = Replace(s, "/N", "")
              s = Replace(s, "/count", "")
              s = Replace(s, " ", "")
              s = Replace(s, "/", "")
              For i = 65 To 125
                      s = Replace(s, Chr(i), "")
              Next
              pages = Val(Trim(s))
              If pages &lt; 0 Then
                  pages = 1
              End If
              Close #nFileNum
              Exit Sub
          End If
          'imp only 1000 lines searches
          If c &gt;= 1000 Then
               GoTo a
          End If
       Loop
         Close #nFileNum
         Exit Sub
     a:
         Close #nFileNum
         pages = 1
         Exit Sub
  End Sub
  </code></pre>
  
  <p>However, most of the time, it defaults to pages = 1 (under a:). 
  I've also updated this to 10000 to be sure that it hits the <code>/Count</code> line, yet it still does not give me the correct count.</p>
  
  <pre><code>If c &gt;= 10000 Then
           GoTo a
  End If
  </code></pre>
  
  <p>I also came across this <a href="https://www.reddit.com/r/visualbasic/comments/29n2xa/getting_pdf_page_count/" rel="nofollow noreferrer">reddit</a></p>
  
  <p>Is there another way to do this, something I can utilize in my app? </p>
  
  <p>Any help is greatly appreciated.</p>
  
  <hr>
  
  <p>Background:</p>
  
  <p>This is for a legacy vb6 app where I'm attempting to let the user manipulate the PDF files. I added a ListBox that displays all PDF documents in a particular directory. When user double clicks on any one of the files, i display it in a WebBrowser component inside my application.</p>
  
  <p>EDIT: Image containing the BinaryMode line Count for 3 different documents:
  <a href="https://i.stack.imgur.com/mLQV5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mLQV5.png" alt="enter image description here"></a></p>
  
  <p>I double checked the page count, and /Count displays the correct page count for each of the three documents.</p>