PDF页面计数不正确
我只是想知道为什么下面链接中的vbs代码不能正确计算pdf页面?它似乎低估了每个pdf中实际存在的页面数的一半或更多 如果您无法访问上面的链接,请输入以下代码:PDF页面计数不正确,pdf,vbscript,count,Pdf,Vbscript,Count,我只是想知道为什么下面链接中的vbs代码不能正确计算pdf页面?它似乎低估了每个pdf中实际存在的页面数的一半或更多 如果您无法访问上面的链接,请输入以下代码: ' 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.FileSyst
' 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
谢谢试试这个Function getPdfPgCnt(ByVal sPath)
Dim strTStr
With CreateObject("Adodb.Stream")
.Open
.Charset = "x-ansi"
.LoadFromFile sPath
strTStr = .ReadText(-1)
End With
With (New RegExp)
.Pattern = "Type\s+/Page[^s]"
.IgnoreCase = True
.Global = True
getPdfPgCnt = .Execute(strTStr).Count
End With
If getPdfPgCnt = 0 Then getPdfPgCnt = 1
End Function
'Usage : getPdfPgCnt("C:\1.pdf")
更新#1~#2:
Option Explicit
Private Function getPdfPgCnt(ByVal sPath) 'Returns page count of file on passed path
Dim strTStr
With CreateObject("Adodb.Stream")
.Open
.Charset = "x-ansi"
.LoadFromFile sPath
strTStr = .ReadText(-1)
End With
With (New RegExp)
.Pattern = "Type\s*/Page[^s]"
.IgnoreCase = True
.Global = True
getPdfPgCnt = .Execute(strTStr).Count
End With
If getPdfPgCnt = 0 Then getPdfPgCnt = 1
End Function
'--------------------------------
Dim oFso, iFile
Set oFso = CreateObject("Scripting.FileSystemObject")
'enumerating pdf files in vbs's base directory
For Each iFile In oFso.getFolder(oFso.GetParentFolderName(WScript.ScriptFullName)).Files
If LCase(oFso.GetExtensionName(iFile)) = "pdf" Then WScript.Echo iFile & " has "& getPdfPgCnt(iFile)&" pages."
Next
Set oFso = Nothing
'--------------------------------
试试这个Function getPdfPgCnt(ByVal sPath)
Dim strTStr
With CreateObject("Adodb.Stream")
.Open
.Charset = "x-ansi"
.LoadFromFile sPath
strTStr = .ReadText(-1)
End With
With (New RegExp)
.Pattern = "Type\s+/Page[^s]"
.IgnoreCase = True
.Global = True
getPdfPgCnt = .Execute(strTStr).Count
End With
If getPdfPgCnt = 0 Then getPdfPgCnt = 1
End Function
'Usage : getPdfPgCnt("C:\1.pdf")
更新#1~#2:
Option Explicit
Private Function getPdfPgCnt(ByVal sPath) 'Returns page count of file on passed path
Dim strTStr
With CreateObject("Adodb.Stream")
.Open
.Charset = "x-ansi"
.LoadFromFile sPath
strTStr = .ReadText(-1)
End With
With (New RegExp)
.Pattern = "Type\s*/Page[^s]"
.IgnoreCase = True
.Global = True
getPdfPgCnt = .Execute(strTStr).Count
End With
If getPdfPgCnt = 0 Then getPdfPgCnt = 1
End Function
'--------------------------------
Dim oFso, iFile
Set oFso = CreateObject("Scripting.FileSystemObject")
'enumerating pdf files in vbs's base directory
For Each iFile In oFso.getFolder(oFso.GetParentFolderName(WScript.ScriptFullName)).Files
If LCase(oFso.GetExtensionName(iFile)) = "pdf" Then WScript.Echo iFile & " has "& getPdfPgCnt(iFile)&" pages."
Next
Set oFso = Nothing
'--------------------------------
提供(并接受)的解决方案仅适用于数量有限的PDF文档。由于PDF文档经常压缩包括页面元数据在内的大块数据,因此对“type\s*/page[^s]”的粗正则表达式搜索通常会遗漏页面
唯一真正可靠的解决方案是非常费力地分解PDF文档。恐怕我没有一个有效的VBS解决方案,但我已经编写了一个Delphi函数,演示了如何实现这一点(请参见)。提供(并接受)的解决方案只适用于有限数量的PDF文档。由于PDF文档经常压缩包括页面元数据在内的大块数据,因此对“type\s*/page[^s]”的粗正则表达式搜索通常会遗漏页面
唯一真正可靠的解决方案是非常费力地分解PDF文档。恐怕我没有一个有效的VBS解决方案,但我编写了一个Delphi函数来演示如何执行此操作(请参见)。我用包含的代码更新了问题。我用包含的代码更新了问题。此函数是否应该替换此函数countPage(sString)?不完全正确。我将countPage和ReadAllTextFile与Strem对象相结合,而不是FSO和Regex。我已经更新了更多细节的答案。谢谢更新。您的脚本比我最初发布的脚本计算页面的数量要多,但是有些PDF文件的页面数不足一页。你知道为什么会发生这种情况吗,thx?@Kul Tigin我对你的代码提出了一个新问题,你能帮我做些新的改进吗?这个函数应该替换这个函数countPage(sString)吗?不完全是。我将countPage和ReadAllTextFile与Strem对象相结合,而不是FSO和Regex。我已经更新了更多细节的答案。谢谢更新。您的脚本比我最初发布的脚本计算页面的数量要多,但是有些PDF文件的页面数不足一页。你知道为什么会发生这种情况吗,thx?@Kul Tigin我对你的代码提出了一个新问题,你能帮我做些新的改进吗?对,压缩是一个问题。还有其他的。PDF不是一种按文本处理的格式。对,压缩是一个问题。还有其他的。PDF不是一种可以按文本处理的格式。