# TOC entries and labels for included PDF pages

I am embedding full pages from another PDF document (using the pdfpages package), but I would like to be able to declare my own section headings before each embedded document (for e.g. hyperlinking / TOC purposes). pdfpages has an experimental feature which lets me do this, but the problem is that I want to put all of these embedded documents inside a section, so I need to insert a section heading and a subsection heading on the first document I embed. Unfortunately pdfpages doesn't seem to be able to do this. Is there a way for me to do this?

Edit: Thank you for the suggestions. I am also in need of something similar for general labels — at the moment I am abusing the addtolist option of \includePDF but the objects I'm labelling aren't really figures or tables.

-
Did you try \phantomsection\addcontentsline{toc}{section}{Title}? –  egreg Apr 17 '11 at 9:37
Invisible? I understand the text of your question that you wish to insert your own headings, which I would expect to be visible. –  Stefan Kottwitz Apr 17 '11 at 10:02
@egreg Works perfectly. Thanks. If you post it as an answer I'll accept. –  Zhen Lin Apr 17 '11 at 10:04
@Stefan: I can edit the documents being embedded to have the text of the correct heading, but what I really need is for \pageref etc. to work correctly. –  Zhen Lin Apr 17 '11 at 10:05
Your problem in Edit is not clear. What needs to be labeled? I'd also suggest to create a new question for this. –  Christian Lindig Apr 17 '11 at 11:42

You can create entries in the table of contents (TOC) when importing a PDF. Here is an example:

\documentclass{article}
\usepackage{pdfpages}

\begin{document}
\tableofcontents
1,section,1,First Section Entry,p1,
1,subsection,1,Subsection Entry,p2,
2,section,1,Second Section Entry,p3}]
{publishing-logo+layout.pdf}
\end{document}


Parameters for each TOC entry are:

• Page number relative to the first page of the included document
• Level for the TOC entry
• Depth of section (1 for section, 2 for subsection, etc.)
• TOC entry
• Label for the entry

You can create two entries for a single page on different levels (like section and subsection). The first two entries go the same page. A label (like p2) can be used to refer to a page that is part of the included PDF.

-
That was exactly what I tried to do, but having two entries for the same page didn't seem to work. –  Zhen Lin Apr 17 '11 at 10:35
thanks for the toc option reference. –  yolo Jun 4 '11 at 12:24
The third field is for the depth of the section. 1 for section level, 2 for subsection level ... –  Sohaib Afifi Sep 4 '14 at 14:22
I don't understand the difference between the "level" and the "depth" parameters of the key. Besides I'm getting the following error (Package pdfpages Warning : There's something wrong with the entries of addtotoc. Not all entries were processed.) when compiling this piece of code : \includepdf[pages=1, addtotoc={18,subsection,2,CV,cv}]{cv.pdf}. The .log file isn't more explicit than that either. Could someone help ? –  lepiment Jul 5 at 16:47
Just a guess: when you add a document of one page, you can't have a TOC entry for page 18 of it. Try addtotoc={1,subsection,2,CV,cv}. –  Christian Lindig Jul 5 at 17:58

You could use the pagecommand option of \includepdf:

\includepdf[pages=1,pagecommand={\section{title}}]{filename}
\includepdf[pages=2-]{filename}


Extend pagecommand as desired. The second \includepdf line includes the remaining pages without the page command.

You could also use the pagecommand option for setting hyperlink targets or bookmarks using commands of hyperref or bookmark there and also for \addcontentsline of \addtocontents if desired.

-
Ah. That's quite clever... but my first document has only one page. (I'm also having trouble with including multi-page PDF documents — not sure if that's a problem with my source material or the package itself...) –  Zhen Lin Apr 17 '11 at 10:01
@Zhen Lin: then you can omit the 2nd line. Regarding other trouble with multi-page inclusion you could describe that in another question, perhaps somebody is able to help. –  Stefan Kottwitz Apr 17 '11 at 10:06
Genius, this feels like the slightly more elegant way of doing this. –  Nick Hawes May 15 '13 at 22:30