我正在尝试获取PDF文档中的页数。我的一些PDF是在Word中创建的(另存为PDF),其中一些是复印到目录中的(不确定这是否重要)。
经过数小时的研究,我发现这说起来容易做起来难。
页面计数很少返回,给我正确的页数
,尽管大多数PDF实际上
/Count
二进制代码内部。
例如,我使用了以下代码;它应该以二进制模式打开文档,查找
/计数
或
/N
然后得到它旁边的数字,这应该是我的页数。
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
但是,大多数情况下,它默认为pages=1(在a下:)。
我还将此更新为10000,以确保它符合
/计数
行,但它仍然没有给我正确的计数。
If c >= 10000 Then
GoTo a
End If
我也遇到过这个
reddit
有没有其他方法可以做到这一点,我可以在我的应用程序中使用?
非常感谢您的帮助。
背景:
这是一个旧的vb6应用程序,我试图让用户操作PDF文件。我添加了一个列表框,显示特定目录中的所有PDF文档。当用户双击任何一个文件时,我会将其显示在应用程序中的WebBrowser组件中。
编辑:包含3个不同文档的二进制模式行计数的图像:
我仔细检查了页数,并且/count显示了三个文档中每个文档的正确页数。