# Renumber pages of a PDF

I want to edit the metadata of a scanned PDF to assign custom page numbers to different pages. For example, what are now pages 1-3 I might want to call i, ii and iii, and what are pages 4-10, I want to call 1-7. I do not want to change the actual order of the pages.

Is there A) A way to do this at all using free tools; and B) A way to do this "in batch" (so, without having to renumber each page manually).

-
Would you be happy with a solution based on LaTeX? It would be possible to include the PDF in an otherwise empty document and create the PDF page numbers as you like. –  Martin Scharrer Apr 8 '11 at 23:34
I would indeed be happy with a LaTeX solution. Can you post some details below? –  MarkovCh1 Apr 9 '11 at 17:08

Here a solution based on LaTeX. It uses the pdfpages package to include the scanned PDF (here called scan.pdf). The PDF page labels you want can be set using the hyperref package with the pdfpagelabels option enabled. It uses the normal \thepage macro as a label which can be defined to lower case roman numbers. The page counter is then reset and changed back to normal numbers.

\documentclass[a4paper]{article}% or use 'letterpaper'
\usepackage{pdfpages}
\usepackage[pdfpagelabels]{hyperref}
\begin{document}
% Set lower case roman numbers (\Roman would be upper case):
\renewcommand{\thepage}{\roman{page}}
\includepdf[pages=1-3]{scan.pdf}
% Back to normal (arabic) numbers:
\renewcommand{\thepage}{\arabic{page}}
% Reset page counter to 1:
\setcounter{page}{1}
\includepdf[pages=4-]{scan.pdf}
\end{document}


Place the above code into a file (e.g. scan_mod.tex) and compile it with pdflatex:

# pdflatex scan_mod


This will produce scan_mod.pdf. However any special annotations incl. hyperlinks will disappear. This shouldn't be any problem with scanned PDFs.

If you need this more often you could write a script which accepts the number of roman numbered pages and the file name(s) as arguments and creates a tempfile with the above code where the name and numbers are variables, which is then compiled.

-
Thank you for such a thorough answer! This is a great solution. The only other solutions I was aware of included either .NET or something equally horrible, or wading through dialogs in Adobe Acrobat (which I can't afford anyway). This is even scriptable! –  MarkovCh1 Apr 10 '11 at 0:24
Nice solution! I was wondering about the same question of batch generation of bookmarks/outlines on the left panel with hyperlinks to the beginning of each section/chapter. Is it possible to use LaTex as well? Here is my question askubuntu.com/questions/27312/bookmark-pdf-and-djvu-files . Thanks! –  Tim Apr 15 '11 at 23:49
@Tim: You can create PDF bookmarks with LaTeX when combining PDFs. See my answer to How do I use LaTeX to create table of contents (chapter headings, subsections etc) for a set of pdf files which I am merging into a single large pdf? on TeX.SX. –  Martin Scharrer Apr 16 '11 at 8:37
This is a fantastic answer, I used it and it works perfectly. –  Andrea Lazzarotto Oct 27 '13 at 16:11
+1 for LaTeX​​​ –  Whaaaaaat Aug 15 '14 at 6:34

There's a tool called PDF Mod which is a free tool to rearrange the pages of a PDF.

It can be installed from the Ubuntu Software Centre in Ubuntu 10.10 and higher.

To install in Ubuntu 9.10 or 10.04 :

To install Add the ppa ppa:pdfmod-team/ppa to your software sources (Here's how to do that) and install pdfmod from the software center

Good Luck :D

-
Ah, but my question wasn't asking about how to rearrange the pages. It was to change the metadata for the pages: relabel the page numbers (insert roman numerals as the first few pages, maybe skip a few; PDFs support the former certainly). –  MarkovCh1 Mar 27 '11 at 5:31

Just found a pointer that it could be possible to use ghostscript for this, here: pdftk - Add and edit bookmarks to pdf - Unix and Linux - Stack Exchange #18600; it refers to links:

However, the above deal with bookmarks - not with logical pagination. It turns out from pdfmarkReference.pdf, the needed "command" is '/Label' (or '/PAGELABEL') - and it further refers to PDFReference.pdf chapter 8.3.1 "Page Labels". Unfortunately, that chapter doesn't necessarrily explain how pdfmarks could be used with page labels - but this post does:

The /PAGELABEL pdfmark does not have any /Page key, so one can set the label for the ‘current’ page only (and, as a consequence, only for one page at a time). Since you call it at the very beginning, it’s expected to set a label for the 1st page and only for it.

Multiple /PAGELABELs for the same page: the pdfmark reference says the last one takes effect, so the result of your 1st commandline is OK. Note the /Page key is ignored.

How to set page labels from PostScript? I can think of 2 methods:

(A) The 100% documented way:

Issue a /PAGELABEL as part of each page.

(B) The less documented way: ...

gswin32c -sDEVICE=pdfwrite -sOutputFile=50pages.pdf -dNOPAUSE

GS>[/_objdef {pl} /type /dict /OBJ pdfmark
GS>[{pl} <</Nums [0 <</P (Page ) /S /r /St 10>> 2 <<>>]>> /PUT pdfmark
GS>[{Catalog} <</PageLabels {pl}>> /PUT pdfmark
GS>50 { showpage } repeat
GS>quit


... and further in that thread:

As to making this work; since the original file is a PDF file, you can run each page from the file individually. So you can set the PAGELABEL pdfmark for page 1, run page 1 from the original file, set the PAGELABEL for page 2, run page 2 from the original file and so on.

Because the label is (as SaGS) said applied to the current page, this should correctly set the labels for each page in the output PDF file. (caveat: I haven't actually tried this)

EDIT: just to show this - if you have this saved as pdfmarks file:

[ /Label (-1) /PAGELABEL pdfmark
showpage
[ /Label (0) /PAGELABEL pdfmark
showpage
[ /Label (1) /PAGELABEL pdfmark
showpage


... and you call:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=outfile.pdf infile.pdf pdfmarks


... then you will get three empty pages appended at the end of infile.pdf, labeled -1, 0 and 1 :)

Well, maybe this helps sometime to get a simpler gs script for renumbering pages :)
Cheers!

EDIT2: Got it, I think - use same gs command as above - and below are the contents of the pdfmarks script, which will renumber the infile.pdf, so it starts with -1, 0, 1 ... It's basically a modified example from the PDF reference (see comments for more):

% Type name (Optional) The type of PDF object that this dictionary describes; if present, must be PageLabel for a page label dictionary.
% S name (Optional) The numbering style to be used for the numeric portion of each page label:
%       D Decimal arabic numerals
%       R Uppercase roman numerals
%       r Lowercase roman numerals
%       A Uppercase letters (A to Z for the first 26 pages, AA to ZZ for the next 26, and so on)
%       a Lowercase letters (a to z for the first 26 pages, aa to zz for the next 26, and so on)
% P text string (Optional) The label prefix for page labels in this range.
% St integer (Optional) The value of the numeric portion for the first page label in the range. Subsequent pages will be numbered sequentially from this value, which must be greater than or equal to 1. Default value: 1.

% renumber first 25 pages - push each by 10, and add prefix:
% [/_objdef {pl} /type /dict /OBJ pdfmark
% [{pl} <</Nums [0 <</P (Page ) /S /D /St 10>> 25 <<>>]>> /PUT pdfmark
% [{Catalog} <</PageLabels {pl}>> /PUT pdfmark

[/_objdef {pl} /type /dict /OBJ pdfmark
[{pl} <</Nums [ 0 << /P (-1) >>         % just label -1 (no style) for pg 0;
1 << /P (0) >>          % just label  0 (no style) for pg 1;
2 << /S /D /St 1 >>     % decimal style, start from 1, for pg2 and on.
]>> /PUT pdfmark
[{Catalog} <</PageLabels {pl}>> /PUT pdfmark

-
Great! Thanks, you don't joke around :) –  MarkovCh1 Oct 15 '11 at 15:58

You can do that with a text editor.

As the answer says, open a PDF file with a text editor, search /Catalog entry, and then append an entry named /PageLabels like this:

/PageLabels << /Nums [
0 << /P (cover) >> % labels 1st page with the string "cover"
1 << /S /r >> % numbers pages 2-6 in small roman numerals
6 << /S /D >> % numbers pages 7-x in decimal arabic numerals
]
>>


Note that the page indices (physical page numbers) begin with 0.

Of cource, you can do this automatically using scripting languages.

PDF Standards - Page Labels has detailed specification.

-

jPDF Tweak is an Open Source graphical utility that offers page numbering (the correct term is "page labeling") and many other beginner to advanced PDF editing features. It runs on Ubuntu and other operating systems.

The Documentation page provides step-by-step instructions.

-
Thanks, this what really helped me, preserving forms and all. jPDF Tweak is really powerful thing, though with not very convenient interface. –  TiGR Oct 19 '14 at 20:12
If the original question did not mention batch jobs, I would say this answer really deserves to be the accepted one. –  Brian Z Mar 19 at 10:52

Openoffice/Libreoffice can do the trick with the pdf-import extension and a pagination Macro.

Not a perfect solution, but it works for me (apart from using PDF Mod - which I would strongly suggest).

-
The pdf-import extension seems busted for OpenOffice.org 3.2. Importing (into Draw and Writer) gives an "I/O error." –  MarkovCh1 Apr 9 '11 at 16:54

Try pyPdf, a python library to manipulate PDF documents. Some, but not much, programming would be necessary.

You could also have a look at PDFtk, though I haven't checked if it supports changing the page number associated with individual pages. Both are available as packages in Ubuntu.

-
Hm, PDFtk doesn't seem to be able to do it. pyPdf has many methods for extracting metadata, but doesn't seem to be able to write them back into the document. –  MarkovCh1 Apr 9 '11 at 17:07

There is another app out there called PDFEdit - its hosted on source forge. Source Forge Project Page - However this doesn't help because it doesn't the functionality you require

-
I don't actually think PDF Edit can change the page numbers. I tried and haven't succeeded, in any case. –  MarkovCh1 Apr 9 '11 at 16:49
@Syzygy - indeed, just checked: pdfedit can show Catalog/PageLabels Dict if a document has it, but if it is selected, it says: "This dictionary does not have any directly editable properties"... Cheers! –  sdaau Oct 14 '11 at 0:36