Rob van der Woude's Scripting Pages

Useful PDF Related Commands

PDF Page Count

1. Get the PDF's page count using ExifTool

  1. REM Uses Phil Harvey's ExifTool: https://exiftool.org/
  2. exiftool.exe -PageCount filename.pdf

 

2. Get the PDF's page count using GhostScript

  1. REM Uses GhostScript: https://www.ghostscript.com/
  2. REM Note the escaped backslashes in the -c command
  3. gswin32c.exe -q -dNODISPLAY -dNOSAFER --permit-file-read="d:\dir\filename.pdf" -c "(d:\\dir\\filename.pdf) (r) file runpdfbegin pdfpagecount = quit"

 

3. Get the PDF's page count using PDFtk

  1. REM Uses PDFtk - The PDF toolkit: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
  2. PDFtk.exe filename.pdf dump_data | FIND.EXE "NumberOfPages"

 

4. Get the PDF's page count using native Windows commands only

  1. REM Uses native Windows commands only; may occasionally miss 1 page.
  2. TYPE filename.pdf 2>NUL | FINDSTR.EXE /R /C:"/Type[	 ]*/Page" 2>NUL | FIND.EXE /V "/Pages" | FIND.EXE /C "/Page"

 

Merge PDFs (all pages)

1. Merge PDFs (all pages) using GhostScript

  1. REM Uses GhostScript: https://www.ghostscript.com/
  2. gswin32c.exe -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=outputfile.pdf -dBATCH inputfile1.pdf inputfile2.pdf inputfile3.pdf [ ... ]

 

2. Merge PDFs (all pages) using PDFtk

  1. REM Uses PDFtk - The PDF toolkit: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
  2. PDFtk.exe inputfile1.pdf inputfile2.pdf inputfile3.pdf [ ... ] output outputfile.pdf

 

Merge PDFs (selected pages)

1. Merge PDFs (selected pages) using GhostScript

  1. REM Uses GhostScript: https://www.ghostscript.com/
  2. REM Use doublequotes for PageList if it contains commas.
  3. REM If no PageList is specified for a file, the last known PageList will be used,
  4. REM hence 1- (page 1 and onwards) for the second file in this example.
  5. gswin32c.exe -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=outputfile.pdf -dBATCH -sPageList="2-5,7" inputfile1.pdf -sPageList=1- inputfile2.pdf -sPageList=12- inputfile3.pdf [ ... ]

 

2. Merge PDFs (selected pages) using PDFtk (ranges only)

  1. REM Uses PDFtk - The PDF toolkit: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
  2. REM Only ranges, no individual pages.
  3. REM If no range is specified for a file, the entire PDF is merged.
  4. PDFtk.exe A=inputfile1.pdf B=inputfile2.pdf C=inputfile3.pdf cat A2-7 C12-end output outputfile.pdf

 

3. Merge PDFs (selected pages) using PDFtk (any selection)

  1. REM Uses PDFtk - The PDF toolkit: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
  2. REM Workaround for the "only ranges" limitation: use multiple qualifiers
  3. REM for the same file (e.g. A and B are both referencing inputfile1.pdf).
  4. PDFtk.exe A=inputfile1.pdf B=inputfile1.pdf C=inputfile2.pdf D=inputfile3.pdf cat A2-5 B7 D12-end output outputfile.pdf

 

Print PDFs

1. Print PDF to default printer using Acrobat Reader (with Print dialog)

  1. REM Uses Acrobat Reader: https://get.adobe.com/reader/
  2. REM Will probably fail with Acrobat "Writer" installed.
  3. REM Open a Print dialog to print pdffile to the default printer.
  4. Acrobat.exe /P pdffile

 

2. Silently print PDF to any printer using Acrobat Reader

  1. REM Uses Acrobat Reader: https://get.adobe.com/reader/
  2. REM Will probably fail with Acrobat "Writer" installed.
  3. REM Silently print pdffile to any printer (printerdriver and printerport optional).
  4. Acrobat.exe /N /T pdffile printer [ printerdriver [ printerport ] ]

 

3. Silently print PDF to default printer using Foxit PDF Reader

  1. REM Uses Foxit PDF Reader: https://www.foxit.com/pdf-reader/
  2. REM Silently print pdffile to the default printer.
  3. FoxitPDFReader.exe /p pdffile

 

4. Silently print PDF to any printer using Foxit PDF Reader

  1. REM Uses Foxit PDF Reader: https://www.foxit.com/pdf-reader/
  2. REM Silently print pdffile to any printer.
  3. FoxitPDFReader.exe /t pdffile printer

 

5. Silently print PDFs to any printer using GhostScript

  1. REM Uses GhostScript: https://www.ghostscript.com/
  2. REM Silently print pdffile to any printer.
  3. gswin32c.exe -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile="%printer%PrinterName" pdffile

 

6. Silently print PDFs to default printer using LibreOffice/OpenOffice

  1. REM Uses OpenOffice Draw: https://www.openoffice.org/download/
  2. REM or LibreOffice Draw: https://www.libreoffice.org/download/download-libreoffice/
  3. REM Silently print pdffile to the default printer.
  4. sdraw.exe -p pdffile

 

7. Silently print PDFs to any printer using LibreOffice/OpenOffice

  1. REM Uses OpenOffice Draw: https://www.openoffice.org/download/
  2. REM or LibreOffice Draw: https://www.libreoffice.org/download/download-libreoffice/
  3. REM Silently print pdffile to any printer.
  4. sdraw.exe -pt printer pdffile

 

8. Print PDFs with whatever program is registered to handle PDFs

  1. REM Uses whatever program is registered for handling PDF files
  2. REM Not all programs have a registered Print verb, and even less programs have a registered PrintTo verb.
  3. REM Fails if no Print or PrintTo verb is registered for .pdf files (e.g. if Acrobat Writer is installed).
  4. @ECHO OFF
  5. REM Save this code as PrintPDF.bat, then use
  6. REM PrintPDF pdffile
  7. REM to print to the default printer, or
  8. REM PrintPDF pdffile printer
  9. REM to print to any printer.
  10. SET PrintVerb=print
  11. IF NOT "%~2"=="" SET PrintVerb=printto
  12. FOR /F "tokens=2 delims==" %%A IN ('ASSOC .pdf') DO (
  13. 	FOR /F "tokens=2*" %%B IN ('REG.EXE Query HKCR\%%A\shell\%PrintVerb%\command') DO (
  14. 		CALL %%C
  15. 	)
  16. )

 

 

Notes: 1: The GhostScript executable name depends on the OS used: on most OSs its name is just gs, on 32-bit Windows it is gswin32c, and on 64-bit Windows it is gswin64c.
2: Use the command WMIC.EXE Path Win32_Printer Get DeviceID | MORE.COM /E +1 to list all available printer names

 

Related Stuff:


page last modified: 2024-09-01; loaded in 0.0059 seconds