TransWikia.com

How To Add Multiple PDF Pages as Figures / Images / Graphics

TeX - LaTeX Asked on December 21, 2020

I found a solution to include PDF documents into my LaTeX document, different from using usepackage{pdfpages}.
usepackage{pdfpages} does a fine job in just putting the pages from the PDF where I want them, as full pages. But today I wanted to include a PDF document, and keep my overall document style, including page numbering and headers and footers and so on. This can be done by using the includegraphics[width=textwidth,page=1]{Document.pdf} command. The specified page of the PDF is integrated into my LaTeX document like a figure. Therefore, it only covers the area that would otherwise be covered by text and footers and headers and everything remains intact. This is what I achieve with the following solution.

As a side note: The PDF document I wanted to integrate was in A5 format, my LaTeX Document in A4 (both portrait). To my surprise, A5 in portrait mode just fills the whole space available for text in my A4 LaTeX document. So I can easily include 1 PDF page on 1 LaTeX page and it looks fine.

One Answer

To include multiple pages can be tedious, as the includegraphics command has to be adjusted for every page (page=1, page=2, page=3,...). Instead, I wrote a short program in C that creates the tex-code that can then be inserted into the LaTeX document:

#include <stdio.h>
int a=0, b=0, i=0;

int main()
{
    FILE *f = fopen("myfile.tex","w");
    for (i=1 ; i<b+1 ; i++)
    {
        fprintf(f,"includegraphics[width=textwidth,page=%d]{document.pdf}nn",i);
    }
    return 0;
}

This creates a file myfile.tex with the includegraphics[width=textwidth,page=1]{Document.pdf} followed by a double line break for the pages 1 to b (so b should be the number of the last page you want to include, just change it in the second line (int a=0, b=0, i=0;)). Also, change the part saying {document.pdf} to the name of the PDF you want to include (the name you would tell LaTeX).

Once you changed b to the last page you want to include, and {document.pdf} to the appropriate name/location for LaTeX, you can save this file, e.g. as write.c (the ending .c is important here, as it is a program written in C).

Then, open a terminal and go to the file location, e.g.

cd ./MyCodeFolder

make sure that you are in the right folder and that your write.c-file is there:

ls

lists all files in the current folder. If so, compile it, to get a executable program:

gcc write.c -o write.exe 

gcc is the compiler, write.c the source code we just created, and write.exe the program that will get created by the compiler after hitting enter.

Now, execute write.exe. This creates myfile.tex. Final step: Go to you LaTeX code, make sure Document.pdf is there as well, add myfile.tex and include it where you want your PDF pages in your LaTeX document using

input{myfile.tex}

Profit.

Answered by kalle on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP