Get the number of pages in a PDF document

后端 未结 12 1824
你的背包
你的背包 2020-12-07 09:00

This question is for referencing and comparing. The solution is the accepted answer below.

Many hours have I searched for a fast and easy, but mostly a

12条回答
  •  借酒劲吻你
    2020-12-07 09:24

    Here is a Windows command script using gsscript that reports the PDF file page number

    @echo off
    echo.
    rem
    rem this file: getlastpagenumber.cmd
    rem version 0.1 from commander 2015-11-03
    rem need Ghostscript e.g. download and install from http://www.ghostscript.com/download/
    rem Install path "C:\prg\ghostscript" for using the script without changes \\ and have less problems with UAC
    rem
    
    :vars
      set __gs__="C:\prg\ghostscript\bin\gswin64c.exe"
      set __lastpagenumber__=1
      set __pdffile__="%~1"
      set __pdffilename__="%~n1"
      set __datetime__=%date%%time%
      set __datetime__=%__datetime__:.=%
      set __datetime__=%__datetime__::=%
      set __datetime__=%__datetime__:,=%
      set __datetime__=%__datetime__:/=% 
      set __datetime__=%__datetime__: =% 
      set __tmpfile__="%tmp%\%~n0_%__datetime__%.tmp"
    
    :check
      if %__pdffile__%=="" goto error1
      if not exist %__pdffile__% goto error2
      if not exist %__gs__% goto error3
    
    :main
      %__gs__% -dBATCH -dFirstPage=9999999 -dQUIET -dNODISPLAY -dNOPAUSE  -sstdout=%__tmpfile__%  %__pdffile__%
      FOR /F " tokens=2,3* usebackq delims=:" %%A IN (`findstr /i "number" test.txt`) DO set __lastpagenumber__=%%A 
      set __lastpagenumber__=%__lastpagenumber__: =%
      if exist %__tmpfile__% del %__tmpfile__%
    
    :output
      echo The PDF-File: %__pdffilename__% contains %__lastpagenumber__% pages
      goto end
    
    :error1
      echo no pdf file selected
      echo usage: %~n0 PDFFILE
      goto end
    
    :error2
      echo no pdf file found
      echo usage: %~n0 PDFFILE
      goto end
    
    :error3
      echo.can not find the ghostscript bin file
      echo.   %__gs__%
      echo.please download it from:
      echo.   http://www.ghostscript.com/download/
      echo.and install to "C:\prg\ghostscript"
      goto end
    
    :end
      exit /b
    

提交回复
热议问题