代码之家  ›  专栏  ›  技术社区  ›  Koosh

PDF文档的页数不一致

  •  1
  • Koosh  · 技术社区  · 6 年前

    我正在尝试获取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个不同文档的二进制模式行计数的图像: enter image description here

    我仔细检查了页数,并且/count显示了三个文档中每个文档的正确页数。

    1 回复  |  直到 6 年前
        1
  •  1
  •   jac    6 年前

    正则表达式有一些限制,但我更喜欢使用它们来搜索字符串,我认为这将是一个使用正则表达式的好地方。您可能想使用该模式,因为我只进行了一点测试,就比较快地完成了这项工作。

    在项目中添加对Microsoft VBScript正则表达式5.5的引用。然后您可以尝试下面的示例代码。

    Private Sub Command1_Click()
        Dim oRegEx As RegExp
        Dim fHndl As Integer
        Dim sContents As String
        Dim oMatches As MatchCollection
    
        On Error GoTo ErrCommand1_Click
    
        'Open and read in the file
        fHndl = FreeFile
        Open some pdf file For Binary Access Read As fHndl
        sContents = String(LOF(fHndl), vbNull)
        Get #fHndl, 1, sContents
        Close #fHndl    'We have the file contents so close it
        fHndl = 0
    
        'Instantiate and configure the RegEx
        Set oRegEx = New RegExp
        oRegEx.Global = True
        oRegEx.Pattern = "((?:/Count )(\d+))"
        Set oMatches = oRegEx.Execute(sContents)
    
        'Look for a match
        If oMatches.Count > 0 Then
           If oMatches(0).SubMatches.Count > 0 Then
               MsgBox CStr(oMatches(0).SubMatches(0)) & " Pages"
           End If
        End If
    
        Exit Sub
    
    ErrCommand1_Click:
        Debug.Print "Error: " & CStr(Err.Number) & ", " & Err.Description
        If Not oRegEx Is Nothing Then Set oRegEx = Nothing
        If Not oMatches Is Nothing Then Set oMatches = Nothing
    
    End Sub
    

    正则表达式模式的说明:
    () 创建组
    ?: 括号内使组无法捕获
    <</Linearized 是文本字符串
    .* 贪婪量词,匹配任意字符0次或更多次
    /N 文字字符串
    \d+ 贪婪的qualtifier,匹配数字1次或多次
    >> 文字字符串