1.

Solve : Using Visual Basic?

Answer»

How I can create a program using visual Basic. My Idea is to count the number of pages from a "PDF" file .which code i can use to create this type of program?This sub procedure LETS you get the page count for a given PDF file. The page count usually appears somewhere in the last 50 lines of the file. Simply open the file in binary mode and parse through the content searching for "/count". To test this, simply add a command button and label control to your Winform. Be SURE to update the path to your PDF file in the command button control's event handler.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Code: [Select]' open the PDF in binary mode & count the pages
' search for "/N xx"
' or "/Count xx"
Dim pages As Long

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 <= 10 Then
pos = InStr(s, "/N")
End If
pos1 = InStr(s, "/count")
If pos > 0 Or pos1 > 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 < 0 Then
pages = 1
End If
Close #nFileNum
Exit Sub
End If
'imp only 1000 lines searches
If c >= 1000 Then
GoTo a
End If
Loop
Close #nFileNum
Exit Sub
a:
Close #nFileNum
pages = 1
Exit Sub
End Sub

Private Sub Command1_Click()
pagecount ("c:\path\to\your.pdf")
Label1.Caption = pages
End Sub




Discussion

No Comment Found