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 28

  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
  5. Joerg wrote:

    Thanks for examples, got my results much faster than messin’ around with the pdftk man page.

    Greets

    Joerg

    Posted 10 Nov 2010 at 8:39 pm
  6. chewearn wrote:

    hi Joerg
    Thanks for reading, nice to know it is helpful. :-)

    Posted 10 Nov 2010 at 8:52 pm
  7. Tony Redhead wrote:

    Hi Chewearn,

    Thanks for the rearrange page order command line. Saved me a lot of time trying to figure it out.

    Tony

    Posted 15 Nov 2010 at 3:16 pm
  8. chewearn wrote:

    hi Tony
    Thanks for reading :)

    Posted 16 Nov 2010 at 9:29 am
  9. Mohana Karthick wrote:

    hey nice post , but can you help me out with how to replace pages . I have a set of e-books and I need to replace few pages in them with a common file . Its more like updating . Hope you understand what I mean.

    Posted 19 Dec 2011 at 1:33 pm
  10. chewearn wrote:

    hi Mohana
    What you want to do, in principle, is same as what I described in no. 5 “Insert a blank page”, except to use your common file in place of the blank page.

    Posted 19 Dec 2011 at 10:28 pm
  11. gelli wrote:

    Hi all,

    If I want blank page for often every page. Simply say, including all even pages as blank pages. Please, provide command line in terminal.

    Thanks in advance !!

    Posted 16 May 2012 at 10:49 pm
  12. Georgios Anthitsis wrote:

    If you want to create a blank page which has the same page size as your document’s page size, you can do the following:
    1. Find out your document’s page size by using pdfinfo. The size will be given in pts.
    2. Use a (online) conversion tool to convert pts to inches.
    3. Open libreoffice and change the page size (format -> page…) using the values obtained in step 2.
    4. Export to pdf.

    Posted 28 May 2012 at 2:19 am
  13. lbmouse wrote:

    Came across this while trying to find something to allow me to manulplate odd or even pages. dump_data came in handy:

    for /f “skip=8 tokens=2 delims= ” %%a IN (‘d:\pdftk\pdftk.exe %1 dump_data’) do (set pagecount=%%a)
    set /a modulo = “%pagecount% %% 2″
    if %modulo% == 0 (
    REM Even
    ) else (
    REM Odd
    )

    Posted 12 Jun 2012 at 3:01 am
  14. chewearn wrote:

    hi lbmouse
    Thanks for providing the solution :)

    Posted 12 Jun 2012 at 7:28 pm
  15. K-Veikko wrote:

    Thanks.

    – 5. Insert a blank page

    When manually creating a blank page the page-size may not be the same with existing pages.

    Is there a way to clear the contents of an existing page?

    Posted 16 Sep 2012 at 3:52 pm
  16. chewearn wrote:

    hi K-Veikko
    I am not aware any command that clears contents of an existing page.

    However, it is easy create to a blank page with the correct page size. In the Office Write program, simply change the size of the blank page before printing to pdf.

    Posted 16 Sep 2012 at 5:00 pm
  17. Doug wrote:

    Hello, I need to arrange multiple pdf’s by section names of the filename.

    This script works:
    pdftk *_%DATE:/=%_A*.pdf *_%DATE:/=%_B*.pdf *_%DATE:/=%_C*.pdf *_%DATE:/=%_*.pdf output MM_%DATE:/=%.pdf

    But when there is no *_%DATA:/=%_C*.pdf section of files, it errors and does not produce an output file.

    In other words the range of files will be unknown and could change daily…

    Any ideas?

    Posted 20 Oct 2012 at 5:50 am
  18. Doug wrote:

    EDIT: sorry there was an error in my previous example

    Hello, I need to arrange multiple pdf’s by section names of the filename.

    This script works:
    pdftk *_%DATE:/=%_A*.pdf *_%DATE:/=%_B*.pdf *_%DATE:/=%_C*.pdf output MM_%DATE:/=%.pdf

    But when there is no *_%DATA:/=%_C*.pdf section of files, it errors and does not produce an output file.

    In other words the range of files will be unknown and could change daily…

    Any ideas?

    Posted 20 Oct 2012 at 5:52 am
  19. chewearn wrote:

    hi Doug
    Thanks for visiting my blog. Sorry, looks like your question is specifically about Windows command line, which I am unfamiliar with (except the most basic stuff).

    For Windows scripting, I mostly uses AutoHotkey. In my opinion, AutoHotkey is more versatile, easier to use, and it’s easier to find help/examples/howtos.

    Posted 20 Oct 2012 at 5:45 pm
  20. Jim Jones wrote:

    How do you remove just the last page without having to count every page?

    Does pdftk accept operators, such as “end-1″?

    Posted 22 Oct 2012 at 1:06 am
  21. chewearn wrote:

    hi Jim
    I didn’t find an operator for what you wanted to do. The operator “end-1″ would actually create a pdf with reverse page order.

    However, you can display the number of pages in a pdf by the following command:

    pdftk input.pdf dump_data


    The above command will show a few lines of statistic about “input.pdf”, include one with “NumberOfPages”. Presumably, you can then use standard bash command (e.g. grep) to parse the number of pages to a variable.

    Posted 22 Oct 2012 at 9:18 pm
  22. michl wrote:

    Hi there,
    nice page. Thanks.
    I have 2 files containing scans: one with the front sides of a questionaire (3 front pages each) and one with the backsides (also 3 pages each). The output should be all 6 pages of one filled out questionaire (so 1 person) followed by all the others (always 6 pages, maybe 100 questionaires, so 600 pages)…. Can you tell how to do that?
    Thanks & greetings
    Michl

    Posted 26 Nov 2012 at 5:20 am
  23. chewearn wrote:

    hi michl
    Basic command you can use:

    pdftk A=front.pdf B=back.pdf cat A1 B1 A2 B2 A3 B3 output questionaire.pdf

    To mass processing 100 questionaires, you would have to do it programmatically. That would be beyond the topic of this page.

    Posted 26 Nov 2012 at 10:00 pm
  24. Jagdish wrote:

    I have 2 pdfs. 1.pdf & 2.pdf. Both are having page numbers separately. I want to merge these 2 files and rearrange the page number from 1,2,3,4,….for all the pages in the merged file. Can it be possible ? If yes , How ?

    Kindly guide.

    Posted 01 Mar 2013 at 3:01 pm
  25. chewearn wrote:

    hi Jagdish
    Yes, it’s possible. It can be done.

    Posted 02 Mar 2013 at 10:28 am
  26. Jagdish wrote:

    hi,
    i have 2 pdfs i.e 1.pdf & 2.pdf whose footer may or may not contain page no.s. I want to merget these 2 pdfs. The output pdf should have page no.s. Is is possible ? if yes, how ?
    e.g. 1.pdf is having 5 pages and 2.pdf is having 2 pages.
    then the output pdf will be of 7 pages but its footer should be page 1/7 , 2/7 …etc.

    Posted 02 Mar 2013 at 3:32 pm
  27. chewearn wrote:

    hi Jagdish
    In this specific scenario you describe, pdftk cannot create page number on footer. It’s not able to manipulate the footer text.

    Posted 02 Mar 2013 at 4:15 pm
  28. Jonah Langlieb wrote:

    I just want to point out that there are programs that are just PDFTK with a GUI wrapper. Personally, I prefer the command line(and, it can do a lot more then just rotate and merge http://en.wikipedia.org/wiki/Pdftk has a good overview) but for some of those “technically challenged” it could be useful. If you’re using linux I PDF chain is a good one(look to the wikipedia page for GUI for other OS’s)

    Posted 16 Apr 2014 at 12:01 am

Trackbacks & Pingbacks 1

  1. From Manipulando Arquivos PDF Pelo Terminal Usando O pdftk | Daemonio Labs on 14 Apr 2012 at 1:57 pm

    [...] [2] REARRANGE PDF PAGES WITH PDFTK, by chewearn (Acessado em: Abril/2012) http://blog.chewearn.com/2008/12/18/rearrange-pdf-pages-with-pdftk/ [...]

Post a Comment

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