Inserting pages from an external PDF document within a LaTeX document

Suppose you are using the pdflatex command to generate a PDF from your LaTeX document, and you need to insert pages from an external PDF document in the PDF document that you are creating. You can do this quite easily using the pdfpages package by Andreas Matthias. I learnt about this package from this post on Stackoverflow. The documentation accompanying the package is very good and quite easy to understand. Nevertheless, I find it helpful to have some ready examples for common use cases. The following code snippets provide some such examples. They build on the answer in the linked Stackoverflow post.

First, add \usepackage{pdfpages} to the preamble of your LaTeX document. All the snippets below assume that you have done this.

The command syntax is:

\includepdf[key1=val1, key2=val2, key3=val3]{pathToFile}

where:

\includepdf is the name of the command
key=val is a comma separated list of options
pathToFile is path to the external PDF file that you want to include.

For this post we will assume that the external PDF file is in the same directory where the LaTeX file is, and is called external.pdf.


Most basic use (no options are specified)

%--------------
%% Snippet 1
%--------------
\includepdf{external.pdf} 

This will insert the first page of external.pdf without numbering the inserted page.


The pagecommand option

If you want to the inserted page to have a page number add the option pagecommand={}. With this option, the command will be:

%--------------
%% Snippet 2
%--------------
\includepdf[pagecommand={}]{external.pdf}

Now you will have the first page of the external.pdf file and it will numbered as if it were part of your LaTeX document. The default value of the pagecommand option is { \thispagestyle{empty} }, which is why page number was missing in the output of Snippet 1. By including pagecommand = {} you override this option. In general, according to the documentation, "[pagecommand] declares LaTeX commands, which are executed on each sheet of paper".


Including specific pages

Snippets 1 and 2 insert only the first page of external.pdf document. What if you wanted to insert more pages or other pages? For this you would use the pages option. I have left the pagecommand option in there so that you can see how different options can be combined. It is not required for including specific pages.

To insert all pages from external.pdf, use:

%--------------
%% Snippet 3
%--------------
\includepdf[pages=-, pagecommand={}]{external.pdf}   

To insert specific pages, say pages 2, 4, and 7, use:

%--------------
%% Snippet 4
%--------------
\includepdf[pages={2, 4, 7}, pagecommand={}]{external.pdf}

To insert a range of pages, say pages 9 through 13, use:

%--------------
%% Snippet 5
%--------------
\includepdf[pages={9-13}, pagecommand={}]{external.pdf}

If you want to include specific pages and a range of pages, you can combine snippets 4 and 5 as:

%--------------
%% Snippet 6
%--------------
\includepdf[pages={2, 4, 7, 9-13}, pagecommand={}]{external.pdf}

Scaling the pages

According to the documentation, pages are scaled automatically as they are inserted. This can be prevented by including noautoscale = true option. However, for a more fine-grained control on scaling you can use the scale option provided by the graphicx package. For example, you can use:

%--------------
%% Snippet 7
%--------------
\includepdf[pages=-, scale=0.9, pagecommand={}]{external.pdf}

Value of scale option should be between 0 and 1.


comments powered by Disqus