Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change page orientation of PDF? (Ghostscript or PostScript solution needed)

Tags:

Given a PDF document, how do I change individual page orientation?

I'm using latest version of Ghostscript.

like image 963
StackOverflowNewbie Avatar asked Jun 22 '10 02:06

StackOverflowNewbie


People also ask

How do you change the orientation of all pages in a PDF?

Choose “Tools” > “Organize Pages.” Or, select “Organize Pages” from the right pane. Choose pages to rotate: Rotate all or a selection of pages in your document by holding the shift key and clicking on the pages to rotate. Or, select a range in the secondary toolbar “Enter Page Range.”

How do I rotate page in PD?

Open your PDF document. Select the Rotate icon in the toolbar. Right-click in the PDF and select Rotate Pages... from the right-click menu.

Can Ghostscript edit PDF?

This paper intends to introduce some of the ways the Ghostscript tools can be used to manipulate a PDF file, including creation of bookmarks, annotations, and setting document properties.

What is Ghostscript used for?

Ghostscript is an interpreter for the PostScript® language and PDF files. It is available under either the GNU GPL Affero license or licensed for commercial use from Artifex Software, Inc. It has been under active development for over 30 years and has been ported to several different systems during this time.


1 Answers

Why do you require usage of Ghostscript? Would it be acceptable to use another Free, Open Source Software tool running on the commandline, such as pdftk?

Anyway, here is how to rotate pages with Ghostscript. However, this may not work for your intentions, because you cannot force a certain orientation for an individual page only. It relies on an internal Ghostscript algorithm that tries to rotate pages automatically, depending on the flow of text inside the PDFs:
* -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).

In any case, I'd recommend to look at pdftk too (which is also available for Windows). It is a commandline tool that can rotate pages from PDFs, and much more. Easier to use than Ghostscript for your stated purpose, and much faster as well. Especially, it can rotate individual pages inside a PDF document, leaving the other pages untouched. Example:

pdftk A=in.pdf           \   cat A1-3 A4west A5-end \   output out.pdf 

This command will output pages 1, 2 and 3 as well as pages 5, 6, ... last un-rotated, but will rotate page 4 by 90 degrees (so the page header faces to the "west"). (However, be aware that this command can lead to unexpected results, depending on the original orientation of your input pages: You should check the orientation of all pages of your input PDF by running pdfinfo -l 1000 input.pdf and then check for the value of the rot output: if you see values different from 0, like 90, 180 and 270, these pages are already pre-rotated...)

See here for more details: http://www.accesspdf.com/pdftk/ .

like image 175
Kurt Pfeifle Avatar answered Oct 01 '22 13:10

Kurt Pfeifle