Take the 2-minute tour ×

I'm looking for a solution that will allow me to do the following to PDF documents:

  1. Insert PDF pages into existing PDF documents
  2. Delete PDF pages from existing PDF documents
  3. Change the orientation of certain PDF pages

Any solutions I should be looking at? My output needs to be PDF.

share|improve this question
Are you looking for a standalone program (such as GhostScript) or a library to integrate with your own program? If latter, what programming language? –  Lauri Lehtinen Jun 20 '10 at 23:38
I need to integrate this in my web application. Using PHP. Trying to figure out how to do this in GhostScript if at all possible. –  StackOverflowNewbie Jun 21 '10 at 0:09
Part of my solution: 1. Given an input PDF document, split the document into individual pages: gs -dFirstPage=1 -dLastPage=1 -sDEVICE=pdfwrite -sOutputFile=page-1.pdf input.pdf (the above creates a new PDF document containing only the first page of input.pdf) 2. Given multiple PDF documents, combine documents into a singular PDF document. gs -sDEVICE=pdfwrite -sOutputFile=combined.pdf -f page-1.pdf page-2.pdf (the above combines page-1.pdf and page-2.pdf into combined.pdf) So now, I just need to know how to change the orientation of each page. –  StackOverflowNewbie Jun 21 '10 at 0:52
add comment

3 Answers

up vote 1 down vote accepted

Since you found out already...
* ...how to separate multipage PDF documents into singlepage files, and
* ...how to re-combine multiple PDF documents into a single one
using Ghostscript, here is how to rotate pages. However, this does not work reliably, because it depends on Ghostscript's ability to discover the main flow of text orientation on each page:
* -dAutoRotatePages=/None -- retains orientation of each page;
* -dAutoRotatePages=/All -- rotates all pages (or none) depending on a kind of "majority decision";
* -dAutoRotatePages=/PageByPage -- auto-rotates pages individually.

Add one of these to the Ghostscript commandline you're using.

If there is no text on a page (or if there is an automatic page rotation set to /None), then Ghostscript uses the setpagedevice settings. You can pass such setpagedevice parameters on the Ghostscript commandline using the -c switch like this:
* -c "<</Orientation 3>> setpagedevice" -- sets landscape orientation;
* -c "<</Orientation 0>> setpagedevice" -- sets portrait orientation;
* -c "<</Orientation 2>> setpagedevice" -- sets upside down orientation;
* -c "<</Orientation 1>> setpagedevice" -- sets seascape orientation.

Probably you need to set the orientation for each page when extracting the pages. I don't think it would work when merging them back to the unified document (I have never tested this).

However, I'd also recommend to you that you have a look at pdftk (which is also available for Windows). It is a commandline tool that can extract pages from PDFs, merge multiple PDF files, rotate pages, and much more. Easier to use than Ghostscript for your stated purposes, and much faster as well. See here for details: http://www.accesspdf.com/pdftk/ .

share|improve this answer
add comment

If you're looking for a standalone tool, pdftk is capable of all those things

share|improve this answer
Not a standalone tool; need a library. –  StackOverflowNewbie Jun 21 '10 at 0:10
You should update your question with this clarification then. (BTW, ghostscript has a .dll as well which serves to control all its functions.) –  Kurt Pfeifle Aug 9 '10 at 10:02
add comment

Now that StackOverflowNewbie has clarified he doesn't want a standalone commandline tool, but a library: iText is the basis of the pdftk commandline utility he rejected as a suitable solution for his requirements. And its a library.

share|improve this answer
add comment

Your Answer


