HowTo Add Page Numbers to a PDF File

Share your own howto's etc. Not for support questions!
Post Reply
User avatar
Posts: 989
Joined: 2007-06-10 03:03
Location: /dev/tty7 (Don't deny it. You're here too!)

HowTo Add Page Numbers to a PDF File

#1 Post by EMD »

If you've ever assembled a PDF file from a number of smaller PDF files, you may have thought that it's very difficult or impossible to use GNU/Linux tools to add page numbers to the file.

Fortunately, it can be done. The simplest, but most time-intensive solution is to use PDF Editor to manually add page numbers one at a time. While that brute-force solution works, it's very difficult to place the numbers in a consistent location on each page. I've also read that mbtPdfAsm can do the trick, but it isn't in Debian's repositories and the link to the author's page is broken.

I recently used the following solution on a 196-page document. It worked. It did the trick. The only downside is that the PDF file tripled in size (from 4.4 MB to 15.4 MB) because the fonts which were once embedded in a handful of locations became embedded in every single page.

Perhaps a reader of this HowTo could suggest a method of reducing the file size?


To use the solution below, you'll need to have pdftk, mawk and OpenOffice installed.

First, assemble your PDF file from the component files:

$ pdftk cover.pdf chap01.pdf chap02.pdf chap03.pdf cat output newbook.pdf

Then open up OpenOffice Writer and create a "page number file." In other words, create a document that only contains page numbers.

To do that you would go to the "Insert" drop-down menu, add a footer and in that footer add page numbers by using the "Insert" drop-down menu. Then add page breaks to the document to bring the page count up to the number of pages you have in your PDF file. The simplest way to do that is to create two blank lines, insert a page break between them ("Insert" >> "Manual Break..."), then select all (Ctrl+A), copy (Ctrl+C) and paste (Ctrl+V), paste, paste, paste ... until you reach the number of pages in your PDF file.

Once you've done that export the "page number file" to PDF (e.g. numbers.pdf) and place the file in the same directory as your assembled PDF file (i.e. newbook.pdf).

Finally, the bash script below will place the page numbers on each page of your assembled PDF file. You will have to edit the script to suit your needs. For example, I didn't want a page number on the first page. I also decided not to let the script delete the temporary file in which the assembly occurs.

Just copy and paste the code below into a text file, save it (e.g. and place it in the same directory as your assembled PDF file and the page number file. Then make it executable and run it:

$ chmod a+x
$ ./

Good Luck!

Code: Select all


# we'll hide the work in a temporary directory
mkdir tmp_num
cp numbers.pdf tmp_num/.
cp newbook.pdf tmp_num/.
cd tmp_num/

# burst newbook into its component pages and extract total pages
pdftk newbook.pdf burst output book_%04d.pdf 
cat doc_data.txt | grep NumberOfPages > nu_pages.txt
nu_pages=`mawk '{print $2}' nu_pages.txt`

# burst the page number file into its component pages
pdftk numbers.pdf burst output nums_%04d.pdf 

# no page number on the first page
cp book_0001.pdf fin_0001.pdf

# start converting pages from page 2
x=2 # initialize x

# place the page numbers on each page of newbook
while [ "$x" -le "$nu_pages" ]; do

     if [ "$x" -lt 10 ]; then
	 pdftk book_000"$x".pdf background nums_000"$x".pdf output fin_000"$x".pdf
         echo "Finished page $x of $nu_pages."

     elif [ "$x" -lt 100 ]; then
	 pdftk book_00"$x".pdf background nums_00"$x".pdf output fin_00"$x".pdf
         echo "Finished page $x of $nu_pages."

     elif [ "$x" -lt 1000 ]; then
	 pdftk book_0"$x".pdf background nums_0"$x".pdf output fin_0"$x".pdf
         echo "Finished page $x of $nu_pages."


# create the new PDF file and move it to the original directory
pdftk  fin_*.pdf cat output finbook.pdf
mv finbook.pdf ../.

# clean up the mess and exit
# cd ..
# rm tmp_num/*
# rmdir tmp_num

echo " "
echo "All done! Have fun!"
echo " "

Posts: 1
Joined: 2008-11-06 22:53

Re: HowTo Add Page Numbers to a PDF File -- Size Reduction

#2 Post by lemadezorn »

Dear EMD, dear forum members,

Thank you very much for this beautiful article, EMD. I have found it very useful to add headers and footers to a "book" composed of several PDF files. I prefer using LaTeX to prepare the numbers.pdf file.

If you do not mind losing the PDF information like hyperlinks, I have found an ugly way to reduce the file size in some cases with free programmes, but it is enough for me. While I create the file newbook.pdf, I convert it into a PostScript file with
pdftops newbook.pdf
and I convert the resulting PostScript file into a new pdf file
pdf2ps newbook2.pdf
In one example in which the file size has increased about 7 times, I have come back to more or less the original size. However I lose hyperlinks and other information. But this is useful if you just want to print the resulting file.

Another interesting complement to this method is jpdfbookmarks if you want to add navigation bookmarks to the resulting PDF file.

Best wishes,

User avatar
Posts: 1
Joined: 2009-08-04 11:16

Re: HowTo Add Page Numbers to a PDF File

#3 Post by Dingo »

EMD wrote:Perhaps a reader of this HowTo could suggest a method of reducing the file size?
Woof Woof (I bark like a shark, even being a dog jumping like a frog)


for Black and white only PDF, You can convert its colorspace (I suppose to be RGB), to Grayscale, with this ghostscript command line

Code: Select all

gs -sOutputFile=output.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 input.pdf < /dev/null
shifting from RGB to Grayscale representation, you will have, without any quality lossy, a big filesize reduction

Posts: 1
Joined: 2011-04-09 16:07

Re: HowTo Add Page Numbers to a PDF File

#4 Post by shalaev »

To me using Office (even if it is OpenOffice) is a bad habbit :) . So here is my solution of the problem:
Imagine that you have a 164-page file.pdf of A4-format without page numbers, which you miss a lot. So at first you create a 6-line file

Code: Select all

and then translate it with the command

Code: Select all

pdflatex pageNumbers
which produces a 164-page file pageNumbers.pdf having nothing but the page numbers.

Next, you split the files file.pdf and pageNumbers.pdf in single pages:

Code: Select all

pdftk file.pdf burst output file_%03d.pdf
pdftk pageNumbers.pdf burst output number_%03d.pdf
Then you unite every page from file.pdf with the same page from pageNumbers.pdf:

Code: Select all

time for i in $(seq -f %03g 1 164) ; do \
pdftk file_$i.pdf background number_$i.pdf output new-$i.pdf ; done
← took 2m29.5s in my case.

Next you merge all one-page files into one multipage one:

Code: Select all

pdftk new-???.pdf output new.pdf
Finally, it is a good idea ro shrink the file new.pdf:

Code: Select all

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE \
-dQUIET -dBATCH -sOutputFile=new-s.pdf new.pdf
That's all. Now you've got the file new-s.pdf which is the same as the initial file.pdf except that it has page numbers.

Posts: 27
Joined: 2011-07-02 05:39

Re: HowTo Add Page Numbers to a PDF File

#5 Post by geazzy »

wow amazing how to :)

Posts: 1
Joined: 2011-07-26 06:27

php script implementation

#6 Post by Kurkoski »

Shalaev's description is great, so I wrote a PHP implementation of it (less the gs part at the end). The script has almost no error checking, use at your own risk.

To use this you'll need:
  • php
  • pdfinfo
  • pdftk
  • pdflatex
Copy the code into a file called pdfAddPageNumbers.php, do chmod +x on that file, and run as ./pdfAddpageNumbers.php input.pdf output.pdf

Code: Select all

        (count($argv) == 3) || DIE("Usage: $argv[0] input.pdf output.pdf\n");
        $filename = $argv[1];
        $target = $argv[2];
        $tmpDir = '/tmp';
        $tmpName = 'addPdfPages';

        exec("pdfinfo $filename",$outList);

        foreach ($outList as $l) {
                if (preg_match('/^Pages/',$l)) {
                        $a = preg_split('/[ ]+/ ',$l);
        echo "Input file $filename has $a[1] pages.\n";
        $numPages = $a[1];

        $escPages = '{' . $numPages . '}';

        if ($fp = fopen($tmpDir . '/' . $tmpName . '.tex','w')) {
                echo("Generating PDF with page numbers\n");
                exec("pdflatex -output-directory=$tmpDir $tmpDir/$tmpName");

                echo("Bursting input file\n");
                exec("pdftk $tmpDir/source.pdf burst output $tmpDir/file_%03d.pdf");
                echo("Bursting page number file\n");
                exec("pdftk $tmpDir/$tmpName.pdf burst output $tmpDir/numb_%03d.pdf");
                echo("Backgrounding ");
                for($ii=1;$ii<=$numPages;$ii++) {
                        $jj = sprintf("%03d",$ii);
                        exec("pdftk $tmpDir/file_$jj.pdf background $tmpDir/numb_$jj.pdf output $tmpDir/new_$jj.pdf");
                exec("pdftk $tmpDir/new_???.pdf output $target");

Posts: 1
Joined: 2012-10-05 18:17

Re: HowTo Add Page Numbers to a PDF File

#7 Post by danielglz »

Thank you very much. It works very well.


Posts: 4
Joined: 2013-02-04 22:52

Re: HowTo Add Page Numbers to a PDF File

#8 Post by trampjuice »

It doesn't work very well. But its 90% there. OK. Its quite a difficult thing to do. Its taken me days to get this working right.

To add to the above. I had to change this. The php in the post above proved unsuable for me as;

1. they didn't work with longfilenames with spaces in them.
2. didn't 'escape' any text to pdflatex when I tried to add the filename of the document.
3. They didn't delete the temp directory afterwards leaving a inconsistant state for the next file.
4. didn't work with pdflatex so it was in batch mode - you had to press return to get past its interactive mode. It seemed like it was taking forever, when really it needed user input mode switched off.
5. Didn't meet all my requirements (below).
6. Used latex and multido to create multiple pages. I had all sorts of problems with this for some reason. After struggling with this, I simply used a php loop to create the multiple pages with a $newpage var, which means latex's rather limited multido package is not needed.

I would not have had a clue where to start without the post above though!

My Requirements: I needed to add page number's to LOADS of PDF's (60) so they could be supporting documents for a court case.
1. Each PDF HAD to have page numbering added.
2. I needed to add the FILENAME (not directory etc..) at the top of each page so the documents could be cross referenced easily.
(BTW: its a task to sort after, printing without the filename on my printer).
3. It needed to deal with long filenames with spaces in them.
4. It needed to have a WHITEBOX background so image files or text would not obscure the reference and numbering.
5. FINALLY - After getting it working I wanted a 'blackhole' network shared directory so I could just drop any PDF into the directory at any time and expect ALL the above done to it, and a output directory would contain the numbered PDFs (Not destroying the original). I can then drag and drop whole directories of files.

The last thing I wanted to do was to have to type filenames etc.. and watch it process them from a shell.

So after days getting this all working here it is;

System Requirements: YOU NEED.
1. Linux (I have debian).
2. phpnfo
3. pdflatex (I think it was the tetex package in aptitude its contained in...I forget..)
4. pdftk
5. php

My code doesn't test for these - make sure they are installed. OK - To get this working.

1. CREATE YOUR PDFNumbering Directory; eg 'BLACKHOLE_pdfAddPageNumbers'
2. Add the following php as pdfAddPageNumbers.php inside this Directory

Code: Select all

// Take out $ref from  $newpage to remove the flename from being written to the pdf
        (count($argv) == 2) || DIE("Usage: $argv[0] input.pdf\n");
        $filename = $argv[1];
        $tmpDir = 'tmp'; /*in curent directory */
	$outDir = 'pdfs_numbered';
        $tmpName = 'addPdfPages';
	if (file_exists($outDir)) {
		echo "$outDir exists..\n";
	} else {
		echo "$outDir Directory does not exist - creating this now in current directory\n";
		exec("mkdir $outDir");
	if (file_exists($tmpDir)) {
		echo "$tmpDir exists so deleting this and recreating\n";
		exec("rm -rf $tmpDir");
	} else {
		echo "$tmpDir does not exist - creating this now\n";
	echo("Creating tmp dir in current dir\n");
	exec("mkdir $tmpDir");
	$target = $outDir."/".$filename;  // ALL CHANGED FILES  GO INTO SUBDIRECTORY!! NO NAME CHANGES
	/* work out the number of pages with pdfinfo */
        exec("pdfinfo \"$filename\"",$outList);
        /* obtain contains page number */        
        foreach ($outList as $l) {
                if (preg_match('/^Pages/',$l)) {
                        $a = preg_split('/[ ]+/ ',$l);
        echo "Input file $filename has $a[1] pages.\n";
        $numPages = $a[1];

        $escPages = '{' . $numPages . '}';
         /*create /tmp/addPDFPages.tex.w file */
	 /* \\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry} */
	 $tex = "\\documentclass[12pt,a4paper]{article} \\usepackage[hmargin=1.5cm,vmargin=0.3cm,nohead,nofoot]{geometry}\\usepackage[usenames]{color}\\begin{document}\\pagestyle{empty}\n";
$pages = "";
for ($i = 0; $i<$numPages; $i++)
{	$p = $i+1;
	$ref = latexSpecialChars($filename);
	$newpage = "\\colorbox{white}{  ".$ref." [{$p} of {$numPages}]}\\newpage\n";
	$pages = $pages.$newpage;
$tex = $tex.$pages."\\end{document}";
        if ($fp = fopen($tmpDir . '/' . $tmpName . '.tex','w')) {
		//*\title{$filename} */
                echo("Generating PDF with page numbers\n");
		echo("pdflatex -interaction=batchmode -output-directory=$tmpDir $tmpDir/$tmpName >> null\n");
                exec("pdflatex  -interaction=batchmode -output-directory=$tmpDir $tmpDir/$tmpName >> null");
		echo("cp $filename $tmpDir/source.pdf\n");
                echo("Bursting input file\n");
		echo("pdftk $tmpDir/source.pdf burst output $tmpDir/file_%03d.pdf\n");
		exec("pdftk $tmpDir/source.pdf burst output $tmpDir/file_%03d.pdf");
                echo("Bursting page number file\n");
                echo("pdftk $tmpDir/$tmpName.pdf burst output $tmpDir/numb_%03d.pdf\n");                
		exec("pdftk $tmpDir/$tmpName.pdf burst output $tmpDir/numb_%03d.pdf");
                echo("Generating Stamped (merged) new pages with pdftk for each page\n");
                for($ii=1;$ii<=$numPages;$ii++) {
                        $jj = sprintf("%03d",$ii);
                 echo("pdftk $tmpDir/file_$jj.pdf stamp $tmpDir/numb_$jj.pdf output $tmpDir/new_$jj.pdf\n");
		 exec("pdftk $tmpDir/file_$jj.pdf stamp $tmpDir/numb_$jj.pdf output $tmpDir/new_$jj.pdf");
                echo("Merging into new document...\n");
                echo("pdftk $tmpDir/new_???.pdf output \"$target\"\n");
		exec("pdftk $tmpDir/new_???.pdf output \"$target\"");
		// reduces  image and print quality but can do this to greatly reduce filesize... to fo this simply ncommnent the lines below
		//echo("...Compressing target with ghostscript...\n");
		//exec("mv \"$target\" temppdf");
		//echo("gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=\"$target\" temppdf \n");
		//exec("gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=\"$target\" temppdf ");	
                echo("...Done removing temp files\n");
		exec("rm -rf $tmpDir");
function latexSpecialChars( $string )
    $map = array( 
    return preg_replace( "/([\^\%~\\\\#\$%&_\{\}])/e", "\$map['$1']", $string );

3. chmod +x pdfAddPageNumbers.php
to make it executable.

At this point its worth a test. Add your pdf's and from the shell

Code: Select all

php pdfAddPageNumbers.php filename.pdf
You should see a 'pdfs_numbered' subdirectory created and a tmp subdirectory created. Your numbered pdf will be in the pdfs_numbered subdir. (tmp will be deleted afterwards). No change will be made to your orginal pdf.

Okay - to make it a 'blackhole' directory.

Create the following file in the directory:

Code: Select all

#echo "Executing dirdrop"
shopt -s nullglob
for fullfile in $_dfiles
filename=$(basename "$fullfile")

echo "fullfile -$fullfile"
echo "extension - $extension"
echo "filename - $filename"
echo "outputfile - $outputfile" 
	echo "$filename" to be processed
	/usr/bin/php pdfAddPageNumbers.php "$fullfile" 
	mv "$fullfile" "$fullfile".done #rename the file so no other instance processes it
	#mv "$fullfile".done /media/hdd/data/gdocs/processed/
exit 0

Code: Select all

crontab -e
and add

Code: Select all

# exec the blackhole script every min
* * * * * bash /media/hdd/data/BLACKHOLE_pdfAddPageNumbers/
Obvoiusly change the directory names to what you have set up...!!

You should now have a blackhole network share directory, which you can drop any pdf files in and they will be page numbered, and found in the pdf_numbered subdirectory. The pdf you drop in will be renamed to xxx.pdf.done (to stop being endlessly processed by the blackhole script)

Posts: 2
Joined: 2013-07-16 02:51

Re: HowTo Add Page Numbers to a PDF File

#9 Post by sherbert »

Thanks to EMD for putting together an important topic. Like others elsewhere, I am a lawyer who requires to present a decent bundle of documents to the Court with consecutively numbered pages. My bundle will contain all the correspondence between me and the other lawyer. I pdf everything I send and either he/she sends back a pdf or I scan an inwards letter to a pdf file. Suffice to say I end up with a 'newbook.pdf' in EMD's parlance.

I have encountered a syntax error at line 26, which reads
if [ "$x" -lt 10 ]; then

my shell expected "done".

I cut and pasted EMD's script into LibreOffice Writer so that I could find and replace his generic newbook.pdf filename with my\ actual\ filename.pdf Then I copied it all across to gedit and saved as the recommended
Should it make any difference that I am running Linux Mint 13?

I appreciate this is an old thread but be sure it is getting read by those of us who need to know. Thanks

Posts: 2
Joined: 2013-07-16 02:51

Re: long file names with spaces

#10 Post by sherbert »

As a separate comment, for trampjuice, long file names work if one precedes each space in the filename with \
Eg. 'file of concatenated pdf's.pdf' is rendered 'file\ of\ concatenated\ pdf's.pdf'

Posts: 1
Joined: 2013-08-26 01:03

Re: HowTo Add Page Numbers to a PDF File

#11 Post by jonjermey »

Many thanks for this -- I had got as far as the OpenOffice solution myself, but the PHP version is more elegant. However, I have a problem with A3 PDF files, which I break up into two A4 halves with a bash command:

pdfposter -p 2x1a4 $1 A4$1

The resulting file looks like an A4 file in every way, but when I try and add page numbers to it using the methods given here, they don't work -- page numbers are off-centre on the page or missing altogether. All I can guess is that the pdfposter command is somehow hiding the right and left halves of each page rather than removing them. I'm happy to use an alternative method if there is one that works. Any suggestions?

Thanks in advance.

Posts: 4
Joined: 2013-02-04 22:52

Re: HowTo Add Page Numbers to a PDF File

#12 Post by trampjuice »

To reply to Sherbert. My court bundle is a directory of pdf's numbered 2013.09.23_LetterBeforeAction.pdf, 2012.09.12_insurance_offer.pdf, 2012.07.12_engineers_report.pdf etc..
I set up a network watch directory (i.e.blackhole), and have had no problems in simply dragging and dropping the many files into the folder.
The cron then executed the php on each file. I have had no problems with it and it has been well tested. (I dislike manually having to manually escape spaces in filenames).
There are always differences, usually minor, in the shell commands depending on the shell you are running in whatever version of linux, I use the bash shell in debian.

As for A3 splitting; Unfortunately, just getting the whitebox background for me took hours of research and trial and error in a unfamilar territory. I would try and find out why the pdf is not splitting into A4 properly with some sort of pdf verfifier and work from there.
Last edited by trampjuice on 2013-09-18 16:05, edited 1 time in total.

Posts: 4
Joined: 2013-02-04 22:52

Re: HowTo Add Page Numbers to a PDF File

#13 Post by trampjuice »

OK - its the texlive package in debian's source repository you want to install to give you access to pdflatex. I have today upgraded to debian wheezy which gives me the 2012 version, but it was all ok on squeeze as well. In detail the packages which contain the goodies are;

Install via aptitiude (or apt-get install) pdftk texlive poppler-utils php5
pdftk - 'tool for manipulating PDF documents'
texlive - 'TeX Live: A decent selection of the TeX Live packages' (here we find pdflatex)
poppler-utils - 'A PDF rendering library based on poppler' (this contains pdfinfo)

example of setting up a SAMBA shared network directory with full permissions
1. mkdir /share
2. chmod 777 /share

3. nano /etc/samba/smb.conf
path = /share
writeable = yes
browseable = yes
create mask =0777
directory mask = 0777
guest ok = yes
4. /etc/init.d/samba restart

you should be able to see the share under your network and drag and drop files to it - if you create a subdirectory 'pdfpagenumbers' and setup the cron and script above around it you should be able to use the above.

And thanks very much to all the efforts of the posters above - its very pleasing to use this on a watch directory and simply drag and drop a file to be numbered into a folder.

Posts: 1
Joined: 2013-10-01 12:09

Re: HowTo Add Page Numbers to a PDF File

#14 Post by datandrews »

One suggestion: the scripts can be simplified considerably by using the "multistamp" feature of pdftk, available since version 1.43 of pdftk (version 1.41 is in squeeze, while version 1.44 is in wheezy). Also, one could use pdftk's dump_data flag instead of pdfinfo in order to take out at least one package dependency (no need for poppler-utils).

User avatar
Soul Singin'
Posts: 1607
Joined: 2008-12-21 07:02

Re: HowTo Add Page Numbers to a PDF File

#15 Post by Soul Singin' »

Here's a solution that only requires PDFLaTeX. It's a complete implementation of the "pagecommand" suggestion at StackOverFlow.

Code: Select all






Posts: 4
Joined: 2013-02-04 22:52

Re: HowTo Add Page Numbers to a PDF File

#16 Post by trampjuice »

Well, this is a much better solution if it works as you add a table of contents without fuss. Thanks!!

Posts: 532
Joined: 2004-09-11 11:50
Location: Hong Kong

Re: HowTo Add Page Numbers to a PDF File

#17 Post by satimis »

Hi all,

I just found this thread on Internet and have tested the script provided by EMD on 2008-09-06.

It worked but with following problems;

page-1 is not numbered but with "1 of 1" marked at the bottom
Page-2 is numbered but also with "1 of 1" marked at the bottom

other pages are numbered correctly.

How can I;
1) number Page-1 ?
2) delete "1 of 1" at the bottom of Page-1 and Page-2 as well ?

Kindly advise. Thanks in advance.

Hoping that it is NOT too late.


Post Reply