How to split and merge pdf documents on Windows without Adobe products

Posted on Sat 16 April 2016 in technical • 3 min read

I often get invoices and other documents that I need to perform further processing on in my day job. Without a pro version of Adobe's product on Windows, it's quite difficult to split the pages of a pdf into individual documents. The below is my solution...

Split PDF

  1. Download and install Ghostscript.
  2. Change the name of the Ghostscript executable in the bin folder from gwwin64_version_.exe to gs.exe. (It saves time when using the tool)
  3. It's easier to use if you add the Ghostscript executable to your user or system path (From super user StackExchange)
  4. Open up a CMD window and navigate to the directory where the PDF is saved
  5. Type in:
$ gs -sDEVICE=pdfwrite -dSAFER -o outputfile.%d.pdf inputfile.pdf

WHERE:
gs = the Ghostscript executable you added to your path in step 3. If you get an error similar to: ''gs' is not recognized as an internal or external command", it wasn't added to your path correctly.
-sDEVICE=pdfwrite = indicates that the output device from the script will be a pdf document.
-dSAFER = allows some safe checking on the document output and restricts renaming, piping and deleting.
-o outputfile.%d.pdf = indicates that the output file is going to be named: outputfile.(page number).pdf. So if the input file contains two pages, this command will result in two output files called: outputfile.1.pdf and outputfile.2.pdf. (See help for more on switches)

  • If you don't want the output to be saved in the same place as the input, add the full path to the output switch to move the output to that folder. eg: -o C:\Users\Username\Downloads\outputfile.%d.pdf.
  • You need to have write access to the output folder, otherwise you'll generate an error.

inputfile.pdf = the name of the input file.

Add as a command from the windows "send to" menu

  • Create a Shortcut to the gs.exe created above called "Split PDF"
  • Edit the shortcut Target as follows:
$ "C:\Path\To\Ghostscript\Executable\gs.exe" -sDEVICE=pdfwrite -dSAFER -o C:\Users\Username\Downloads\output.%d.pdf

(I put my output files in my Downloads folder. You can put them anywhere you'd like, provided you have write access to the folder.)

  • Move the "Split PDF" shortcut to the SendTo folder. (C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo)
  • There should now be a "Send To -> Split PDF" option when you right-click on a pdf.

Split PDF and extract specific page(s)

  • All steps the same as above, add the following options to the command on step 5: -dFirstPage=2 -dLastPage=2 (Where they represent the first and last pages to extract. So the example extracts page 2)
  • The full command is then:
$ gs -sDEVICE=pdfwrite -dSAFER -dFirstPage=2 -dLastPage=2 -o outputfile.%d.pdf inputfile.pdf 

Merge PDF

  • Follow steps 1-3 above, so that Ghostscript is installed
  • From a CMD window in the same location as the PDF:
$ gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dSAFER -sOUTPUTFILE=C:\Users\Username\Downloads\merge.pdf input1.pdf input2.pdf ....
  • As with the split above, you can also add this as a command to the "send to" menu:
    • Create a Shortcut to the gs.exe created above called "Merge PDF"
    • Edit the shortcut Target as follows:
$ "C:\Program Files\gs\gs9.10\bin\gs.exe" -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dSAFER -sOUTPUTFILE=C:\Users\username\Downloads\merge.pdf
  • NOTE: Using the menu command means that you'll need to select the pdfs to be merged in reverse order in order for them to be merged in order
    • Example: To Merge 3 pdf's: 1.pdf,2.pdf,3.pdf, Select them in order as 3.pdf, 2.pdf, 1.pdf and then use the Send To-> Merge PDF option.
  • Move the "Merge PDF" shortcut to the SendTo folder. (C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo)
  • There should now be a "Send To -> Merge PDF" option when you right-click on a pdf.

Resources

Ghostscript documentation: http://ghostscript.com/doc/current/Readme.htm
Download Ghostscript: http://www.ghostscript.com/download/
Some examples and help: http://stackoverflow.com/questions/tagged/ghostscript