# Add page and line numbers to a pdf

Is there any quick script to add page and line numbers to each page of a pdf document?

1. I often enough get articles in pdf to review, with no page number. I end up writing them by hand to refer to each page when pointing errors.

2. When referring an error, I end up counting by hand the lines from the beginning or from the end, or copying the context, to precise the location of the error. It would be much more practical to have a standard way to add line numbers to an existing document.

I could manage editing the LaTeX source to obtain this, but not when I receive a pdf. PDF format does not contain lines per se, so identifying them would require to cluster the $y$-coordinates of the letters, and adding those numbers in the margin would require to take the min of the $x$-coordinates and remove a fixed amount from it. Anybody did this script already, or seen another way?

## 10 Answers

Alright, here's a go at numbering lines in a PDF (or any other image format) without access to the source.

I wrote a little shell script that, using ImageMagick (at least version 6.6.9-4), converts a given PDF into separate raster images for each page, splits these into half pages, shrinks them to a width of one pixel (so takes the horizontal average, basically), turns this into a monochrome image with a given threshold (black=text, white=no text), shrinks every black sequence down to one pixel (=middle of a line), outputs this as a text, pipes it to sed to clean it up and remove all the non-text lines and finally writes a txt file with the position of each line as 1/1000 of the text height.

findlines.sh:

convert $1.pdf -crop 50x100% png:$1

Notice that there is another variable $threshold: this is the minimal distance between two lines. Indeed, when you typeset, for example, a superscript, then there is small jump in the PDF file which my script considers as a separate line. But by asking for line skips of at least the threshold, these small jumps are not taken into account. Here is the script: use CAM::PDF; use PDF::API2;$file=$ARGV[0];$newfile=$ARGV[1];$leftmargin=70;
$threshold=8; if (-e$file) {

$pdf = CAM::PDF->new($file);

$nbpages=$pdf->numPages();

foreach $i (1 ..$nbpages) {
$page1 =$pdf->getPageContent($i); @BTS=(); while ($page1 =~ m/^BT\n((.|\n|\r)+?)\nET/gm) {
push @BTS, $1; } foreach$BT (@BTS) {
$x=0;$y=0;
while ($BT =~ m/([0-9.-]+) ([0-9.-]+) Td/g) {$x=$x + ($1);
$y=$y + ($2); if ($2 > $threshold or$2 < -$threshold) { push @{"PAGES".$i}, $y; } } } @{"PAGES".$i} = sort { $b <=>$a; } @{"PAGES".$i};$prey=10000000; @X=();
foreach $y (@{"PAGES".$i}) {
if ($prey -$y < $threshold) {} else { push @X,$y; }
$prey=$y;
}
@{"PAGES".$i}=@X; }$pdf = PDF::API2->open($file); # Add a built-in font to the PDF$font = $pdf->corefont('Times-Roman'); # Add an external TTF font to the PDF #$font = $pdf->ttfont('/path/to/font.ttf'); # Add some text to the page foreach$i (1 .. $nbpages) {$page = $pdf->openpage($i);
$j=0; foreach$y (@{"PAGES".$i}) {$text = $page->text();$text->font($font, 10);$text->fillcolor('blue');
$text->translate($leftmargin, $y);$text->text($j);$j++;
}
}

# Save the PDF
$pdf->saveas($newfile);

}

• That's a very elegant approach! It's a bit unfortunate that it requires the document to be compiled with pdflatex and a special option, though. The chance that a document has been created that way are pretty slim, and if you had access to the source to recompile the document correctly, you might as well just use the lineno package as benregn suggests.
– Jake
Sep 17, 2012 at 14:25
• You can always use ps2pdf14 to change the PDF level of your file. Sep 17, 2012 at 17:15
• Ah, that's a useful hint! Do I understand correctly, though, that the files have to be produced by pdflatex? I haven't been able to get the script to work with PDFs of journal articles (for example the one linked to in my answer).
– Jake
Sep 17, 2012 at 18:21
• The limitation is on the level of PDF code parsing. I'm only considering the Td operator (as pdflatex always uses solely that one), but there are others. You should uncompress your file and look at what operators are used for moving to the next line, in the ET/BT areas. Sep 21, 2012 at 13:34
• I've had good luck with this script on many files by also grabbing Tm. This is a simple modification, @yannis, would you be opposed to adding it? Feb 3 at 22:39

If you really want to add them to a PDF file, such as for legal documents, these tricks might work for you.

# Add page numbers to a doc

1. fix xref table if necessary with pdftk test-foo.pdf output test-bar.pdf
2. pspdftool 'number(start=1, size=20, x=550 pt, y=10 pt)' test-bar.pdf test-baz.pdf

These numbers are a little big, but this works great for scanned evidence sections. I prepare the evidence section as a separate doc and then number it with the correct "start" number to start after the last page of the brief.

pspdftool takes font options, but you have to figure out the font name from the name of the font file, not how it appears in a word processor.

# Add evenly-spaced line numbers to a doc

I do this because in a trial court filing, the lines should be numbered with even spaces, even if I use blockquotes with thin spacing.

LibreOffice links the line numbers to specific paragraphs on the page.

I got tired of using awkward text area blocks for blockquotes and linking them across pages.

I just want to write whatever I want in the page with different styles and stamp on the numbers later.

Solution:

1. Format one page with line numbers turned on in your word processor, and nothing else.
2. Export the line numbers page to nums.text.pdf.
3. Vectorize it so the numbers don't show up in text search. This worked:
gs -o temp.ps -dNOCACHE -sDEVICE=pswrite nums.text.pdf;
gs -o nums-outlines.pdf -sDEVICE=pdfwrite temp.ps
4. Stamp the numbers:
pdftk unnumbered-fulldoc.pdf multibackground nums-outlines.pdf output combinedfile.pdf

Hope this helps. -Mark

Here, we give a simple answer to this question by using a few latex packages. For example, the LaTeX code:

%% To make "loremipsum.pdf"
% \documentclass[10pt]{article}
% \usepackage{lipsum}
%\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
%  \renewcommand{\baselinestretch}{1.33}
% \begin{document}
% \section*{Lorem Ipsum}
% \lipsum
% \end{document}

\documentclass[12pt]{article}
\usepackage[top=2.5cm, bottom=2.5cm, left=0.5cm, right=0.5cm]{geometry}
\usepackage{ifthen}
\usepackage{pdfpages}
\usepackage[left]{lineno}
\renewcommand\thelinenumber{\bf\scriptsize\color{red}\arabic{linenumber}}
\renewcommand{\baselinestretch}{1.0} % interline spacing

\begin{document}
\newcounter{ctr}
\newcounter{ct}
\setcounter{ct}{1}
\whiledo {\value{ct} < 3} % 2 is the number of the Pdf pages
{
\enlargethispage{3cm}
\begin{minipage}[t]{0.1\textwidth}
\internallinenumbers
\begin{runninglinenumbers*}
\setcounter{ctr}{1}
\whiledo {\value{ctr} < 48} % each page contain 32 line
{
$\longleftrightarrow$\
\stepcounter {ctr}%
}
\end{runninglinenumbers*}
\end{minipage}
\begin{minipage}[t]{0.8\textwidth}
\includepdf[pages=\thect,pagecommand={\thispagestyle{empty}}]{loremipsum.pdf}
\end{minipage}
\clearpage
\stepcounter {ct}%
}
\end{document}

enter code here


produces the output

• The line numbers and the lines are not aligned, the shift becomes more and more dominant at the end of the page.
– user31729
Aug 4, 2014 at 12:10
• As for Johnson's solution above: This might solve the problem for some, but in my case the coordinates (page,line) used in an anonymous review should be understandable by the authors: it won't be the case if the numbers do not correspond to the lines of the document. Aug 5, 2014 at 13:31

Here is a minor variation on Alan Munn's second answer for adding page numbers to a PDF.

The difference here is that the page numbers are placed in the lower corner of the page. With the twosided option, the numbers are placed on the right lower corner on odd pages, and the left lower corner on even pages. The position of the page numbers are adjustable via the put parameters. Also, the page numbers are circled, which makes them show up better.

Thanks to David Carlisle for providing the code and explanations.

\documentclass[14pt,twoside]{article}
\usepackage[margin=.5in]{geometry}
\usepackage{pdfpages}
\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\usepackage{tikz}
\newcommand*\numcircledtikz[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=1.2pt] (char) {#1};}}
\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[LE,RO]{\begin{picture}(0,0)\put(-10,30){\numcircledtikz{\thepage}}\end{picture}}
\begin{document}
\includepdf[pages=1-,pagecommand={\thispagestyle{fancy}}]{<pdffile>}
\end{document}


Here's how I figured out how to add line numbers to an existing pdf. It doesn't use TeX and it's a little fiddly to get the sizing right, but here goes:

Prepare a file of numbers, from 1 to approximately 32, using Excel or something else. Somehow make a png file. You could do this by saving to pdf in Excel and then using an online conversion tool, or by making a screenshot. Make sure the column of numbers is waay over to the left edge of the page, and size your row heights so that the numbers take up a reasonable amount of space.

Download and install PDFill.

Choose Item 8, "Add Watermark to Image." (It will walk you through opening two files.)

Open the pdf whose pages need line numbers.

Open the png file with the column of numbers.

Now you will choose an amplification factor and an offset value. Here, as an example, is what worked for me:

Hit "Save as" and save the file. The new file will probably open up automatically. (If not, check your settings in PDFill.)

Now count the line numbers on a sample page, and see if you need to adjust the amplification factor. Rinse and repeat. Once you've got the right number of lines, play with the offset value until "1" lines up with the first line of your pdf text. Save a screenshot of the successful values so you can remember next time!