37

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).

2
  • 1
    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. Apr 8, 2011 at 23:34
  • I would indeed be happy with a LaTeX solution. Can you post some details below?
    – MarkovCh1
    Apr 9, 2011 at 17:08

9 Answers 9

27
+50

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.

6
16

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.

2
  • +1 This answer is much simpler and better than the accepted one, and the link to the spec is a great help.
    – jja
    Mar 31, 2016 at 11:24
  • 1
    The link mentioned above is dead, but this W3 page has a good description.
    – Kevin Rak
    Jan 21, 2021 at 19:10
11

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.

3
  • 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, 2014 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, 2015 at 10:52
  • jPDF Tweak doesn't require each page to be manually renumbered, it works pretty naturally with page sequences with minimal input. It also lets you load/edit 'Index' and 'Bookmarks', so you don't have to strip them as with the Latex solution above. An easy option for a quick hack-job.
    – jdpipe
    Jan 21, 2020 at 11:17
6

There is a little python script, that can do the job: https://github.com/lovasoa/pagelabels-py

In your case call:

./addpagelabels.py --delete file.pdf
./addpagelabels.py --startpage 1 --type 'roman lowercase' file.pdf
./addpagelabels.py --startpage 4 --type arabic file.pdf
4
  • 2
    This is a great solution! Oct 31, 2019 at 18:17
  • 2
    This works like a charm, huge time saver too!
    – Bastian
    Dec 10, 2019 at 9:55
  • 1
    This option is limited to PDF 1.3, according to the documentation.
    – jdpipe
    Jan 21, 2020 at 11:20
  • GNU Ghostscript can convert PDF 1.3 to PDF 1.7 so the output of addpagelabels can be fixed: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.7 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf Aug 6, 2022 at 11:25
4

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
1
  • Great! Thanks, you don't joke around :)
    – MarkovCh1
    Oct 15, 2011 at 15:58
3

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

Adapted from : http://www.webupd8.org/2011/03/edit-pdf-documents-in-linux-with-pdf.html

Good Luck :D

1
  • 5
    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, 2011 at 5:31
1

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).

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

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.

1
  • 1
    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, 2011 at 17:07
0

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

Text Editing in PDFEdit

2
  • 1
    I don't actually think PDF Edit can change the page numbers. I tried and haven't succeeded, in any case.
    – MarkovCh1
    Apr 9, 2011 at 16:49
  • 2
    @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, 2011 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .