38

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.

8
  • 5
    Did you try \phantomsection\addcontentsline{toc}{section}{Title}?
    – egreg
    Commented Apr 17, 2011 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
    Commented Apr 17, 2011 at 10:02
  • @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
    Commented Apr 17, 2011 at 10:05
  • I'd like to suggest a different title: TOC entries for included PDF pages or similar. The adjective invisible is misleading. Commented Apr 17, 2011 at 11:02
  • 3
    Your problem in Edit is not clear. What needs to be labeled? I'd also suggest to create a new question for this. Commented Apr 17, 2011 at 11:42

2 Answers 2

40

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    
\includepdf[pages=-,addtotoc={
     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. Caveat: with pages={3-10}, the smallest possible number would be 3.
  • 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). In the example, the first two entries go to the same page. A label (like p2) can be used to refer to a page that is part of the included PDF.

Unfortunately error messages from pdfpages are not helpful when the above list contains errors. I encountered the following error, which was hard to spot:

  • With pages={3-10} the first entry in an addtotoc record must be 3 or larger. It was 1 in my case but the error message wasn't pointing to it.
5
  • That was exactly what I tried to do, but having two entries for the same page didn't seem to work.
    – Zhen Lin
    Commented Apr 17, 2011 at 10:35
  • The third field is for the depth of the section. 1 for section level, 2 for subsection level ... Commented Sep 4, 2014 at 14:22
  • 4
    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 ?
    – oowekyala
    Commented Jul 5, 2015 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}. Commented Jul 5, 2015 at 17:58
  • yeah, level and depth confuse me, too. Sounds very redundant to me. What's the semantic of addtotoc={1,section,2,Section-level but depth 2,p1} or addtotoc={1,subsection,1,Subsection-level but depth 1,p1}? Commented Mar 19, 2021 at 8:06
24

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.

5
  • 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
    Commented Apr 17, 2011 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
    Commented Apr 17, 2011 at 10:06
  • Genius, this feels like the slightly more elegant way of doing this.
    – Nick Hawes
    Commented May 15, 2013 at 22:30
  • 3
    I used egreg's comment above to avoid adding the text title on top of the pdf: \includepdf[pages=1,pagecommand={\phantomsection\addcontentsline{toc}{section}{title}}]{filename}
    – juniper-
    Commented Nov 12, 2015 at 13:53
  • this is SO clever! Thank you, what a lifesaver
    – LonLon
    Commented May 18, 2021 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .