Extract PDF pages, Merge multiple PDF files, Write over PDF - with Latex

In the recent times I often found myself looking for tools to perform few simple operations on PDF files, such as extracting single or selective multiple pages from my scanned PDF files, and/or merge multiple scanned PDF documents into a single PDF, adding custom text as overlay to PDF pages and so on.

However, the solution did not come straight, but it just occurred to me while I was working on something else entirely different.

Here I was, preparing my Healthcare Analytics white paper working on its Title page from scratch, and the realization dawned upon me - if you are a Latex user like me, then you have all the necessary tools at your disposal to perform the tricks on PDF files - once you become aware of this small package called pdfpages, that is. You can quickly, in just a single line or two, do whatever you want with your scanned PDF documents all by your own Latex code.

Extract PDF Pages

The includepdf command is going to be your solution. For example, the below code lets you extract 3rd page from your scanned PDF file:

\documentclass{article} 
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=3]{scanned_Doc.pdf}
\end{document}
If you would like to embed the whole PDF document (all pages), you can specify
\includepdf[pages=-]{scanned_Doc.pdf}
If you would like to start from page 2 and embed all pages upto end, then you can specify
\includepdf[pages=2-]{scanned_Doc.pdf}

For scaling the extracted PDF page, you can also consider using \includegraphics (instead of includepdf). \includegraphics works with PDF files as well.

Merge pages from different PDF files

You might encounter online tools to merge few PDF documents into a single document, but they may not let you selectively pick pages from each of those documents and then do the merge. This kind of merge is pretty straight forward with includepdf as shown below.

Here is how you can combine pages from your cover letter PDF document with selective pages from your scanned certificate documents:

\documentclass{article} 
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=1]{CoverLetter.pdf}
\incluedepdf[pages={3,4}]{cert.pdf}
\end{document}

The package also lets you insert multiple extracted pages in a single page with nup option, as below (where first 1 to 4 pages are arranged in 3 columns x 2 rows on a single page):

\includepdf[nup=3x2, pages=1-4]{cert.pdf}

You should be able to offset the extracted PDF into correct location on your page using the regular setlength commands. For a full page PDF insertion, you can try something like below:

\documentclass{article} 
\usepackage{pdfpages}
\pagestyle{plain} % no page numbers
\begin{document}
\setlength\voffset{-0.1in} % adjust the vertical offset
\setlength\hoffset{-0.4in} % adjust the horizontal offset
\includepdf[pages={2,2,2}]{Scanned_Doc.pdf} % insert 2nd page from Scanned_Doc three times
\end{document}

Annotate / Overlay / Overwrite new text on PDF Pages

Sometimes you may want to add custom annotations to PDF files. There are multiple options in Latex that allow you to add your own text on top of the PDF pages. The Latex packages wallpaper, overpic, tikz etc. provide good solutions for this. The solution with overpic is as below:

\documentclass{article} 
\usepackage[abs]{overpic}
\usepackage{pict2e}
\begin{document}
  \begin{overpic}[scale=1,unit=1mm,grid]{Scanned_Doc.pdf}
    \put(15,240){\bfseries\LARGE My Overlay Text}
  \end{overpic}
\end{document}

The solution with tikz is easier, as it lets you put any tikz drawing along with PDF image. This solution uses the pagecommand option of \includepdf as shown below:

\documentclass{article} 
\usepackage{pdfpages}
\usepackage{tikz}
\begin{document}
  \newcommand{\MyText}{ 
    \begin{tikzpicture}[remember picture, overlay]\node at (5,-4.5) {\color{olive!80}\bfseries\LARGE Gopalakrishna Palem};\end{tikzpicture}
   }
  \includepdf[pages=1,pagecommand=\MyText]{Scanned_Doc.pdf}  % write something over the Title page
  \includepdf[pages=2-,pagecommand={}]{Scanned_Doc.pdf}	
\end{document}

Go ahead and use this package to create nice customized documents from your PDF library.

By

P.GopalaKrishna

Homepage    Other Articles