Get number of pages in a pdf using a cmd batch file

前端 未结 7 709
深忆病人
深忆病人 2021-02-01 01:37

I can see there are a lot of questions for getting the number of pages in a a pdf with C, PHP and others but am wondering with a batch file or cmd is there a simple way of getti

7条回答
  •  孤街浪徒
    2021-02-01 02:03

    Without any external tools (save the script bellow as .bat) :

    @if (@X)==(@Y) @end /* JScript comment
    @echo off
    
    cscript //E:JScript //nologo "%~f0"  %*
    
    exit /b 0
    @if (@X)==(@Y) @end JScript comment */
    
       var args=WScript.Arguments;
       var filename=args.Item(0);
       var fSize=0;
       var inTag=false;
       var tempString="";
       var pages="";
    
       function getChars(fPath) {
    
            var ado = WScript.CreateObject("ADODB.Stream");
            ado.Type = 2;  // adTypeText = 2
            ado.CharSet = "iso-8859-1";
            ado.Open();
            ado.LoadFromFile(fPath);                     
            var fs = new ActiveXObject("Scripting.FileSystemObject");
            fSize = (fs.getFile(fPath)).size;
    
            var fBytes = ado.ReadText(fSize);
            var fChars=fBytes.split('');
            ado.Close();
            return fChars;
       }
    
    
       function checkTag(tempString) {
    
        if (tempString.length == 0 ) {
            return;
        }
    
        if (tempString.toLowerCase().indexOf("/count") == -1) {
            return;
        }
    
        if (tempString.toLowerCase().indexOf("/type") == -1) {
            return;
        }
    
        if (tempString.toLowerCase().indexOf("/pages") == -1) {
            return;
        }
    
        if (tempString.toLowerCase().indexOf("/parent") > -1) {
            return;
        }
    
    
        var elements=tempString.split("/");
        for (i = 0;i < elements.length;i++) {
    
            if (elements[i].toLowerCase().indexOf("count") > -1) {
                pages=elements[i].split(" ")[1];
    
            }
        }
       }
    
       function getPages(fPath) {
            var fChars = getChars(fPath);
    
            for (i=0;i" &&
                     fChars[i+1] == ">" ) {
    
                    inTag = false;
                    checkTag(tempString);
                    if (pages != "" ) {
                        return;
                    }
    
                    tempString="";
    
                }
    
                if (inTag) {
                    if (fChars[i] != '\n' && fChars[i] != '\r') {
                       tempString += fChars[i];
                    }
                }
    
            }
    
       }
    
       getPages(filename);
       if (pages == "") {
         WScript.Echo("1");
       } else {
        WScript.Echo(pages);
       }
    

    It takes the path to the .pdf file and simply prints the number of the pages.Not pretty fast as it reads the pdf symbol by symbol , but could be optimized.

提交回复
热议问题