My *nix world

How to rotate even odd scanned PDF pages

Sometimes you need to scan a book page by page resulting a scanned PDF document. Usually you start with the first (odd) page then you turn your book and scan the other (even) page. Or you may start in reverse order, it doesn't matter. You will continue the process until the desired book's pages are all scanned.

The result will be a PDF document where the odd pages are rotated well and the even pages are rotated upside down. Or it might be exactly inversely.

Depending on the PDF reader application you might use you might be able to rotate a page or not but reading a 400-pages document by rotating every other page is a pain in the ass. That is the moment when you decide to do something about it. The solution is to rotate the even pages in the same order like the odd pages.

How to rotate even odd scanned PDF pages in Linux

In Linux we have many tools available freely which helps us to do this job (that's the difference between Linux and Windows). We are going to use the pdftk tool. Depending on your Linux distro the pdftk may be already installed or not. If it's not then please search your Linux package manager for a package named "pdftk".

In Gentoo it is called "app-text/pdftk" so it can be installed like:

emerge pdftk

In Ubuntu/Debian it is called "pdftk" so it can be installed like:

apt-get install pdftk

There are hundreds Linux distro out there so please consult your Linux distro manual about how to install a package.

Anyway, once the pdftk is installed we just have to use it. If you want to learn more about its options then please check out its Linux manual.

To automate a bit the process a wrote a BASH script (pdf-rotate) that accepts some useful arguments (like input-output documents, start-end page, rotation angle for odd-even pages, etc) and simplifies the task making it as simple as shooting a fish in a barrel:

Usage: pdf-rotate input.pdf start_page end_page direction_odd direction_even output.pdf

The page rotation setting can cause pdftk to rotate pages and documents.
Each option sets the page rotation as follows (in degrees):
N: 0, E: 90, S: 180, W: 270, L: -90, R: +90, D: +180.
L, R, and D make relative adjustments to a page's rotation

The script code

#/bin/bash
# Script for rotating all even pages of a document
# Author  : Eugen Mihailescu
# Contact : eugenmihailescux at gmail dot com
# Website : mynixworld.info

APP=$(basename $0)
# enough arguments?
if [ $# -ne 6 ]; then
         echo
         echo 1>&2 "Usage: $APP input.pdf start_page end_page direction_odd direction_even output.pdf"
	 echo
	 echo 1>&2 "The page rotation setting can cause pdftk to rotate pages and documents."
	 echo 1>&2 "Each option sets the page rotation as follows (in degrees):"
	 echo 1>&2 "N: 0, E: 90, S: 180, W: 270, L: -90, R: +90, D: +180."
	 echo 1>&2 "L, R, and D make relative adjustments to a page's rotation"
         echo
         exit 127
fi

function get_rotation {
	local _result=$2
	local myresult
	case $1 in
	N)
		myresult="north"
		;;
	S)
		myresult="south"
		;;
	E)
		myresult="east"
		;;
	W)
		myresult="west"
		;;
	L)
		myresult="left"
		;;
	R)
		myresult="right"
		;;
	D)
		myresult="down"
		;;
	*)
		echo "Unknown rotation mode $1"
		exit
		;;
	esac

	eval $_result="'$myresult'"
}

get_rotation $4 odd_dir
get_rotation $5 even_dir

# generate a temporary directory
TEMPDIR=/tmp/pdf`date +%N`
mkdir $TEMPDIR

# rotate all even pages, keep the odd ones
ODD=0
EVEN=0
pdftk $1 cat $2-$3odd$odd_dir output $TEMPDIR/odd.pdf #2>/dev/null
if [ $? -eq 0 ];then
	ODD=1
fi

pdftk $1 cat $2-$3even$even_dir output $TEMPDIR/even.pdf #2>/dev/null
if [ $? -eq 0 ];then
	EVEN=1
fi

# split the files...
if [ $ODD -eq 1 ];then
	pdftk $TEMPDIR/odd.pdf burst output $TEMPDIR/pg%04d_A.pdf
fi
if [ $EVEN -eq 1 ];then
	pdftk $TEMPDIR/even.pdf burst output $TEMPDIR/pg%04d_B.pdf
fi

# ... and recombine them, "_A" and "_B" suffixes leading to the correct order
pdftk $TEMPDIR/pg*.pdf cat output $6

# cleanup
rm -r $TEMPDIR

if [ -f $6 ];then
	echo "SUCCESS: PDF file created at $6"
else
	echo "ERROR: No PDF file created. See previous error(s)"
fi

Usage example 1:

We want to rotate the input document "my-book.pdf" (which has 125 pages) such that the odd pages are rotated 180 degrees (because they are rotated upside down) and the even pages are rotated zero degrees (we don't want to touch them, they are OK as they are). By using my script mentioned above this task is accomplished as following:

pdf-rotate my-book.pdf 1 125 D N my-book-fixed.pdf

Usage example 2:

We want to accomplish the same task like in the Example 1 above with the mention that only the pages within tge page 25-80 rage must be fixed, the others do no require any rotation. So we are going to rotate the start_page=25 to end_page=80 like in the Example 1 resulting a new document called my-book-25-80.pdf. By doing so we missed the pages within the 1-24 and 81-125 ranges. So the task becomes a bit more complicated:

  1. extract the pages within the 1-24 range unchanged (no rotation required)
  2. extract the pages within the 25-80 range rotated as necessary
  3. extract the pages within the 81-125 range unchanged (no rotation required)
  4. concatenate these 3 PDF files into a single PDF document

These steps are accomplished with only 4 commands at your Linux terminal:

pdf-rotate my-book.pdf 1 24 N N file1.pdf # no rotation required
pdf-rotate my-book.pdf 25 80 D N file2.pdf # rotate only the odd pages
pdf-rotate my-book.pdf 81 125 N N file3.pdf # no rotation required
pdftk file1.pdf file2.pdf file3.pdf cat output my-book-fixed.pdf # combine all PDFs into our final PDF

How to rotate even odd scanned PDF pages in Windows

There is a PDFtk for Windows available freely. Just try to use the logic above with that program although I think there might be some limitations.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

The following two tabs change content below.
How to rotate even odd scanned PDF pages

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
How to rotate even odd scanned PDF pages

Latest posts by Eugen Mihailescu (see all)

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *