使用PDFtk获取pdf文件的页数和文件名并写入excel

问题描述

我是新会员,我会尽我最大的努力做到具体清晰,尊重会员的时间。我是VBA初学者,需要您的帮助。

我正在使用 PDFtk 程序的免费版本,该程序通过图形界面显示有关 pdf 文件的信息(您可以查看所附图片)。

PDFtk

程序为每个pdf文件显示2种信息:

  1. 文件名
  2. 页数

但是,我正在寻找一个 VBA 宏来检索该信息(文件名、页数),然后将其写入 Excel 中打开的工作簿。

重要的是 VBA 将在我的计算机 c:\temp 中的特定位置搜索 pdf 文件

然后写成这样:

Excel filenames and number of pages

换句话说,需要一个 VBA 可以在不“真正”打开图形界面的情况下完成工作,并且使用 PDFtk 应用程序来获取正确的页数。

预先感谢您的帮助

解决方法

试试这个(来源:link

Sub Test()
    Dim I As Long
    Dim xRg As Range
    Dim xStr As String
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Dim xFileNum As Long
    Dim RegExp As Object
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.pdf",vbDirectory)
        Set xRg = Range("A1")
        Range("A:B").ClearContents
        Range("A1:B1").Font.Bold = True
        xRg = "File Name"
        xRg.Offset(0,1) = "Pages"
        I = 2
        xStr = ""
        Do While xFileName <> ""
            Cells(I,1) = xFileName
            Set RegExp = CreateObject("VBscript.RegExp")
            RegExp.Global = True
            RegExp.Pattern = "/Type\s*/Page[^s]"
            xFileNum = FreeFile
            Open (xFdItem & xFileName) For Binary As #xFileNum
                xStr = Space(LOF(xFileNum))
                Get #xFileNum,xStr
            Close #xFileNum
            Cells(I,2) = RegExp.Execute(xStr).Count
            I = I + 1
            xFileName = Dir
        Loop
        Columns("A:B").AutoFit
    End If
End Sub
,

感谢您的链接,感谢您的回复。

几周前我已经尝试过该链接。

我也尝试过这种方法 - https://www.mrexcel.com/board/threads/vba-page-count.347911/ Haluk 于 2008 年编写的 VBA 代码,虽然给出了更好的结果,但仍然会出错。

在阅读了很多关于这个主题的内容后,我意识到不使用第三方程序的 VBA 在计算 pdf 页面时会犯很多错误。

有时比实际数多,有时比实际少,有时数为零,计算页面非常棘手,没有程序它不可靠。

我阅读了一个解释,为什么在没有第三方程序的情况下计算 pdf 页面很棘手 https://www.reddit.com/r/visualbasic/comments/29n2xa/getting_pdf_page_count/

直到现在我发现只有一个 vba 总是提供正确数量的 pdf 页 - 一个使用 adobe acrobat Professional 的 vba - 这意味着使用不是免费的第三方程序。这对我来说不是一个选择。 http://www.vbaexpress.com/forum/archive/index.php/t-40734.html

我更喜欢免费的解决方案,因此我需要一个使用免费第三方程序的 vba,例如 PDFtk、pdf sam basic。 Pdf 属性扩展也能正确计算页数——它是一个轻量级的 COM 扩展,可以将 PDF 属性和列带回 Windows 资源管理器——但我不知道如何为它编写 vba。 https://coolsoft.altervista.org/en/pdfpropertyextension

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...