Rearrange PDF pages with Pdftk

Pdftk is a command line tool to manipulate PDF. Some of the features include rearrange, remove, add/merge and rotate pages. Don’t be put off by the lack of GUI. Pdftk is simple to use with easy to understand syntax. It is also one of the few tool where the man page has useful usage examples (something more developers could learn from when writing the man pages).

Installation

Pdftk is available in the Ubuntu’s universe repository.

sudo apt-get install pdftk


While this post is specifically written with Ubuntu Linux platform in mind, Pdftk is also available in the Windows platform. The method to obtain and install Pdftk in the Windows platform is different, but the usage instruction is the same.

General

For the usage examples below, I will use the following command line arguments:

  • src.pdf, src1.pdf, src2.pdf
    the source PDFs in which you wanted to make the changes;
  • res.pdf
    the result PDF from the changes, i.e. what you want to achieve.

Please note some PDFs are encrypted, which set the permission on what operations can be performed. To manipulated these files beyond the set permission, you need to supply the files’ passwords. These situations will not be specifically described in this post.

If you need the instruction on how to supply the required password, please refer to Pdftk man page.

man pdftk


1. Merge pages from a number of PDFs

To merge two entire PDFs into one:

pdftk src1.pdf src2.pdf output res.pdf

The merged pages will be in the order as it appeared in the command. Specifically for the command above, pages from src1.pdf will be in front of pages from src2.pdf.

To merge first 5 pages of a PDF, into a second PDF:

pdftk A=src1.pdf B=src2.pdf cat A1-5 B1-end output res.pdf

The cat option is used to designate which pages, in what order, are arranged to the result. Specifically for the command above, page 1 to 5 of src1.pdf will be in front, followed by all pages from src2.pdf.

2. Remove page

To remove page 3 from the PDF:

pdftk src.pdf cat 1-2 4-end output res.pdf


3. Rearrange page order

Let’s say I wanted to move page 3 to be the first page of the PDF:

pdftk src.pdf cat 3 1-2 4-end output res.pdf

Observed how the page order is rearranged by the cat options.

4. Rotate pages

Sometimes, you might received a PDF with pages that are sideways (maybe created by a colleague who are technically challenged :shock: ). To rotate all pages 90 degrees clockwise:

pdftk src.pdf cat 1-endE output res.pdf


Or, if the pages are upside down, then rotate them 180 degrees:

pdftk src.pdf cat 1-endS output res.pdf


You can also rotate individual page. Say, you have a PDF of pages in portrait orientation. But page 5 is landscape, because there is a figure that appeared sideway in the page. This type of PDF almost always made the PDF application window to be wider overall, and mess with the “fit to width” setting.

pdftk src.pdf cat 1-4 5E 6-end output res.pdf


5. Insert a blank page

A book (as in real life made from paper book) always has odd pages on the right, and even pages on the left. We are used to seeing this. However, this convention is sometimes violated in a PDF electronic book, where the document’s odd pages are the file’s even pages.

If you printed the PDF in booklet form (two page per one side of paper), the odd pages would be printed on the left, and the even pages on the right. The resulting printed book would “feel” weird and somewhat “uncomfortable” to read. To solve this, I usually insert a blank page into the appropriate location in the PDF.

First, you need to create a PDF with one blank page. This can be easily done by opening the OpenOffice Writer. By default, you get a blank page for you to type in. Simply create a PDF out of this blank page. Click the “Export Directly as PDF” icon on the toolbar and save the file as blank.pdf.

Now, this command inserts the blank page into page 2.

pdftk A=src.pdf B=blank.pdf cat A1 B1 A2-end output res.pdf


Closing

Changing PDF is one of those tasks that we seldom have to do. But when we need to do it, there is usually a lack of tools. Pdftk came in handy this situation. The lack of a GUI should not be a hinderance, since it’s quite easy to use.

Comments 4

  1. Ramtaturis wrote:

    Good quick-start guide for pdftk, thank you.

    I only noticed an error in the last example.
    The correct syntax to insert a blank page 2 into a pdf is

    pdftk A=src.pdf B=blank.pdf cat A1 B1 A2-end output res.pdf

    Posted 30 Nov 2009 at 9:10 pm
  2. chewearn wrote:

    hi Ramtaturis
    Fixed.
    Thanks for pointing out the error.

    Posted 30 Nov 2009 at 9:25 pm
  3. swebster wrote:

    Thanks, very helpful!! I was worried about all the flipping, deleting and combining I had to do, but this made the job quick!

    Posted 15 Jan 2010 at 11:11 pm
  4. chewearn wrote:

    hi swebster
    Happy to be of assistance. :)

    Posted 15 Jan 2010 at 11:30 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *