PDF 页数不正确
我只是想知道为什么下面链接中的 vbs 代码没有正确计算 pdf 页数?似乎每个 pdf 中实际存在的页数少了一半或更多。
http://docs.ongetc.com /index.php?q=content/pdf-pages-counting-using-vb-script
如果您无法访问上面的链接,请使用以下代码:
' By Chanh Ong
'File: pdfpagecount.vbs
' Purpose: count pages in pdf file in folder
Const OPEN_FILE_FOR_READING = 1
Set gFso = WScript.CreateObject("Scripting.FileSystemObject")
Set gShell = WScript.CreateObject ("WSCript.shell")
Set gNetwork = Wscript.CreateObject("WScript.Network")
directory="."
set base=gFso.getFolder(directory)
call listPDFFile(base)
Function ReadAllTextFile(filespec)
Const ForReading = 1, ForWriting = 2
Dim f
Set f = gFso.OpenTextFile(filespec, ForReading)
ReadAllTextFile = f.ReadAll
End Function
function countPage(sString)
Dim regEx, Match, Matches, counter, sPattern
sPattern = "/Type\s*/Page[^s]" ' capture PDF page count
counter = 0
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern "^rem".
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
set Matches = regEx.Execute(sString) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
counter = counter + 1
Next
if counter = 0 then
counter = 1
end if
countPage = counter
End Function
sub listPDFFile(grp)
Set pf = gFso.CreateTextFile("pagecount.txt", True)
for each file in grp.files
if (".pdf" = lcase(right(file,4))) then
larray = ReadAllTextFile(file)
pages = countPage(larray)
wscript.echo "The " & file.name & " PDF file has " & pages & " pages"
pf.WriteLine(file.name&","&pages)
end if
next
pf.Close
end sub
谢谢
I was just wondering why the vbs code in the link below is not counting pdf pages correctly? It seems to under count by half or more the number of pages that actually exist in each pdf.
http://docs.ongetc.com/index.php?q=content/pdf-pages-counting-using-vb-script
Here is the code if you can not access the link above:
' By Chanh Ong
'File: pdfpagecount.vbs
' Purpose: count pages in pdf file in folder
Const OPEN_FILE_FOR_READING = 1
Set gFso = WScript.CreateObject("Scripting.FileSystemObject")
Set gShell = WScript.CreateObject ("WSCript.shell")
Set gNetwork = Wscript.CreateObject("WScript.Network")
directory="."
set base=gFso.getFolder(directory)
call listPDFFile(base)
Function ReadAllTextFile(filespec)
Const ForReading = 1, ForWriting = 2
Dim f
Set f = gFso.OpenTextFile(filespec, ForReading)
ReadAllTextFile = f.ReadAll
End Function
function countPage(sString)
Dim regEx, Match, Matches, counter, sPattern
sPattern = "/Type\s*/Page[^s]" ' capture PDF page count
counter = 0
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern "^rem".
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
set Matches = regEx.Execute(sString) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
counter = counter + 1
Next
if counter = 0 then
counter = 1
end if
countPage = counter
End Function
sub listPDFFile(grp)
Set pf = gFso.CreateTextFile("pagecount.txt", True)
for each file in grp.files
if (".pdf" = lcase(right(file,4))) then
larray = ReadAllTextFile(file)
pages = countPage(larray)
wscript.echo "The " & file.name & " PDF file has " & pages & " pages"
pf.WriteLine(file.name&","&pages)
end if
next
pf.Close
end sub
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提供(并接受)的解决方案仅适用于有限数量的 PDF 文档。由于 PDF 文档经常压缩包括页面元数据在内的大块数据,因此对“type\s*/page[^s]”的粗略正则表达式搜索通常会丢失页面。
唯一真正可靠的解决方案是非常费力分解PDF文档。恐怕我没有有效的 VBS 解决方案,但我编写了一个 Delphi 函数来演示如何执行此操作(请参阅 http://www.angusj.com/delphitips/pdfpagecount.php)。
The solution offered (and accepted) will only work for a limited number of PDF documents. Since PDF documents frequently compress large chunks of data including page metadata, crude regular expression searches for "type\s*/page[^s]" will often miss pages.
The only really reliable solution is to very laboriously decompose the PDF document. I'm afraid I don't have a working VBS solution but I have written a Delphi function which demonstrates how to do this (see http://www.angusj.com/delphitips/pdfpagecount.php).
试试这个
更新#1~#2:
Try this
Update #1~#2: