Answers.Body: <p>I have used jPdfBookmarks on both Windows and Linux to do exactly what you describe - create your own bookmarks. Find it <a href="http://sourceforge.net/projects/jpdfbookmarks/">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T01:52:30.677Z
Answers.Text: This is my recommendation if you have a table of contents beforehand. See my [full tutorial](https://ebooks.stackexchange.com/a/7763/10251) below
Answers.UserDisplayName: Ooker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-12-21T03:46:21.723Z
Answers.LastActivityDate: 2013-12-21T03:46:21.723Z
Answers.UserDisplayName: Jeffrey DeLeo
Answers.Body: <p><a href="http://willus.com/k2pdfopt" rel="noreferrer">k2pdfopt</a> (free, open source) can also do this by supplying a text file.  See the <a href="https://www.willus.com/k2pdfopt/help/options.shtml" rel="noreferrer"><code>-toclist</code> option</a>.  Use like so:</p>
  
  <pre><code>k2pdfopt -mode copy -n -toclist my_chapter_list.txt srcfile.pdf -o outfile.pdf
  </code></pre>
  
  <p>...where my_chapter_list.txt is a simple ASCII file with page numbers beginning each line, e.g.</p>
  
  <pre><code>1 Cover
  2 Table of Contents
  5 Chapter 1
  25 Chapter 2
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-12T18:01:35.540Z
Answers.Text: Does this edit the PDF in any way other than adding the toc as metadata?
Answers.UserDisplayName: MattHusz
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-12T18:17:27.573Z
Answers.Text: Actually cpdf is a better tool to use for this--it is more straightforward to use and has a very similar option (-add-bookmarks) demonstrated at https://www.coherentpdf.com/usage-examples.html.
Answers.UserDisplayName: willus
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-04-03T15:01:43.393Z
Answers.LastActivityDate: 2019-08-30T17:59:34.417Z
Answers.LastEditDate: 2019-08-30T17:59:34.417Z
Answers.UserDisplayName: willus
Answers.Body: <p>I will extend on @Donald's answer but I would also like to note I do <strong>not</strong> recommend, personally for quality issues, ever using Calibre for ebook development.  </p>
  
  <p>As stated I would suggest getting a copy of <a href="http://success.adobe.com/en/na/sem/products/acrobat/acrobat.html?kw=purch&amp;sdid=KARFP&amp;skwcid=AL!3085!3!33670697302!e!!g!!adobe%20acrobat&amp;ef_id=Upy1YgAAAUH18d7P:20131219152230:s" rel="noreferrer">Acrobat</a> and you can (for this example I am using Acrobat X Standard but note the I have not seen any difference in the shortcut buttons BUT the GUI has changed since version 9):</p>
  
  <ul>
  <li>Open the <code>.pdf</code> file in Acrobat</li>
  </ul>
  
  <p><img src="https://i.sstatic.net/aRP68.png" alt="enter image description here"></p>
  
  <ul>
  <li>Select the bookmark tab on the left side</li>
  </ul>
  
  <p><img src="https://i.sstatic.net/gvaSI.png" alt="enter image description here"></p>
  
  <ul>
  <li>Create the bookmark (two different ways):
  By button <img src="https://i.sstatic.net/UTboR.png" alt="enter image description here">, or use shortcut <kbd>Ctrl + B</kbd> (windows) or <kbd>Cmd + B</kbd> (mac):</li>
  </ul>
  
  <p><img src="https://i.sstatic.net/RR9EC.png" alt="enter image description here"></p>
  
  <p>You can create bookmarks by dragging and dropping the sub-level bookmark onto the level 1 bookmark such as:</p>
  
  <p><img src="https://i.sstatic.net/1XBXb.png" alt="enter image description here"></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-12-20T05:20:36.493Z
Answers.Text: A slightly cheaper alternative to Adobe Acrobat is Foxit's [PhantomPDF](http://www.foxitsoftware.com/Secure_PDF_Security/). I use it at the office a lot to manipulate bookmarks in PDF documents.
Answers.UserDisplayName: Nathan Osman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-12-19T15:34:28.687Z
Answers.LastActivityDate: 2013-12-20T05:02:01.510Z
Answers.LastEditDate: 2013-12-20T05:02:01.510Z
Answers.UserDisplayName: DᴀʀᴛʜVᴀᴅᴇʀ
Answers.Body: <h1><a href="https://sourceforge.net/projects/jpdfbookmarks/" rel="nofollow noreferrer">Jpdfbookmark</a> can work for scanned books</h1>
  <ol>
  <li><p>Prepare the TOC in .txt file</p>
  <pre><code> Chapter 1. The Beginning/23
       Para 1.1 Child of The Beginning/25,FitWidth,96
           Para 1.1.1 Child of Child of The Beginning/26,FitHeight,43
   Chapter 2. The Continue/30,TopLeft,120,42
       Para 2.1 Child of The Beginning/32,FitPage
  </code></pre>
  <p>You can extract the TOC by using <a href="https://ocr.space/tablerecognition" rel="nofollow noreferrer">table recognition with OCR</a>, then use regex to fix it.</p>
  </li>
  <li><p>Load that TOC</p>
  <p><img src="https://i.sstatic.net/GEZqN.png" alt="" /></p>
  </li>
  <li><p>Expand all bookmarks (<kbd>Ctrl + E</kbd>), select all of them, then go to Tools &gt; Apply Page Offset</p>
  <p><img src="https://i.sstatic.net/wmZfU.png" alt="" /></p>
  </li>
  <li><p>Enter the first pages that outmatch the page number in the TOC</p>
  </li>
  </ol>
  <p>Your can read its <a href="http://jpdfbookmarks.altervista.org/InsertBookmarks.html#1_3_1" rel="nofollow noreferrer">manual</a> or watch a <a href="https://youtu.be/7DUkvH7_wII?t=30" rel="nofollow noreferrer">quick video tutorial</a>. It has command line mode and can work on Linux, Mac.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-03T17:39:34.223Z
Answers.Text: Your OCR to TOC link is broken, can you share a new link?
Answers.UserDisplayName: cuppajoeman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-04T06:10:26.587Z
Answers.Text: Thanks for your notice. I've changed to a better link
Answers.UserDisplayName: Ooker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-06T01:14:31.953Z
Answers.Text: To get pictures of the original TOC I followed this guide: https://askubuntu.com/questions/50170/how-to-convert-pdf-to-image/50180#50180
Answers.UserDisplayName: cuppajoeman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-04-22T17:42:50.257Z
Answers.LastActivityDate: 2021-10-04T06:07:47.707Z
Answers.LastEditDate: 2021-10-04T06:07:47.707Z
Answers.UserDisplayName: Ooker
Answers.Body: <p>There are also free tools that allow editing/adding bookmarks. A cross platform example is <a href="http://jpdftweak.sourceforge.net/" rel="noreferrer">jPdf Tweak</a>.</p>
  
  <p>It is a little clumsy to use, but you can create the table of contents in your favourite spreadsheet program, export as csv and then just import it.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-03T12:42:53.883Z
Answers.Text: I also have used jpdftweak, and it has served me quite well. Page numbers can be a bit confusing if you want them labeled i, ii, iii, iv, ... and then 1, 2, 3 ..., but it's worth taking the time to figure it out.
Answers.UserDisplayName: Diagon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-12-19T17:03:45.880Z
Answers.LastActivityDate: 2013-12-19T17:03:45.880Z
Answers.UserDisplayName: Tim
Answers.Body: <p>I wrote an open source command line toolset called <a href="https://krasjet.com/voice/pdf.tocgen/" rel="nofollow noreferrer">pdf.tocgen</a> just for doing this. It uses the embedded font attributes and position information of headings to generate a table of contents automatically.</p>
  <p>For example, for the PDF version Paul Graham's <em>On Lisp</em>, available for download on <a href="http://www.paulgraham.com/onlisptext.html" rel="nofollow noreferrer">his website</a> but comes without a table of contents. You could use the <code>pdfxmeta</code> tool to build a &quot;recipe&quot; file</p>
  <pre><code>[[heading]]
  level = 1
  font.name = &quot;Times-Bold&quot;
  font.size = 19.92530059814453
  
  [[heading]]
  level = 2
  font.name = &quot;Times-Bold&quot;
  font.size = 11.9552001953125
  </code></pre>
  <p>save it as <code>recipe.toml</code>, and use the <code>pdftocgen</code> command to automatically generate an outline</p>
  <pre><code>$ pdftocgen onlisp.pdf &lt; recipe.toml
  &quot;Preface&quot; 5
      &quot;Bottom-up Design&quot; 5
      &quot;Plan of the Book&quot; 7
      &quot;Examples&quot; 9
      &quot;Acknowledgements&quot; 9
  &quot;Contents&quot; 11
  &quot;The Extensible Language&quot; 14
      &quot;1.1 Design by Evolution&quot; 14
      &quot;1.2 Programming Bottom-Up&quot; 16
      &quot;1.3 Extensible Software&quot; 18
      &quot;1.4 Extending Lisp&quot; 19
      &quot;1.5 Why Lisp (or When)&quot; 21
  &quot;Functions&quot; 22
      &quot;2.1 Functions as Data&quot; 22
      &quot;2.2 Defining Functions&quot; 23
      &quot;2.3 Functional Arguments&quot; 26
      &quot;2.4 Functions as Properties&quot; 28
      &quot;2.5 Scope&quot; 29
      &quot;2.6 Closures&quot; 30
      &quot;2.7 Local Functions&quot; 34
      &quot;2.8 Tail-Recursion&quot; 35
      &quot;2.9 Compilation&quot; 37
      &quot;2.10 Functions from Lists&quot; 40
  [--snip--]
  </code></pre>
  <p>You could save the output to a file called <code>toc</code></p>
  <pre><code>$ pdftocgen onlisp.pdf &lt; recipe.toml &gt; toc
  </code></pre>
  <p>and import it to the PDF file using <code>pdftocio</code>:</p>
  <pre><code>$ pdftocio -o output.pdf onlisp.pdf &lt; toc
  </code></pre>
  <p>Please read the <a href="https://krasjet.com/voice/pdf.tocgen/" rel="nofollow noreferrer">homepage</a> for the details on how to use this toolset. I hope you find it useful.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-29T04:52:56.537Z
Answers.LastActivityDate: 2020-07-29T04:52:56.537Z
Answers.UserDisplayName: Krasjet
Answers.Body: <p><em><strong>THIS PART IS EDITED</strong></em></p>
  <p>For 'software-generated' PDF-files, i.e. PDF's not created from scans, I recommend to use (and upvote the answer by Krasjet) <a href="https://krasjet.com/voice/pdf.tocgen/" rel="nofollow noreferrer">pdf.tocgen</a>. Using this package becomes even easier with the <a href="https://github.com/dalanicolai/toc-mode" rel="nofollow noreferrer">toc-mode</a> package for (Spac)Emacs described next.</p>
  <p>For all other PDF and DJVU documents there is <a href="https://github.com/dalanicolai/toc-mode" rel="nofollow noreferrer">a package called toc-mode</a> for Emacs, which in my opinion provides the easiest way to add Table of Contents to documents (for linux and possibly also for different OS's). It includes options to extract the TOC via OCR, and one can use Emacs/Evil (vim) keyboard macro's and the handy table editor to fix it.</p>
  <p>In case this package's functionality is not sufficient or using Emacs is no option then the remaining part of this answer remains valid.</p>
  <p><em><strong>END EDITED PART</strong></em></p>
  <p>(Not enough reputation points to comment) Like the answer by Patrick Bourdon, I would also recommend <a href="http://handyoutlinerfo.sourceforge.net/" rel="nofollow noreferrer">HandyOutliner</a>. However, I would suggest you try the <a href="https://pypi.org/project/document-contents-extractor/" rel="nofollow noreferrer">python script called document-contents-extractor</a> to extract the contents.</p>
  <p>If these options do not work, then I would also like to recommend <a href="https://www.tracker-software.com/product/pdf-xchange-viewer" rel="nofollow noreferrer">PDF-XChange Viewer</a> as a very powerful bookmark/contents extractor (selected text can be easily added to the bookmarks). It works well under wine.</p>
  <p>Although not related to the question, I just would like to add that at the moment PDF X-Change viewer appears to me to be the most powerful PDF editor/viewer on linux. Okular is doing very well of course also. Anyway, still <a href="https://github.com/vedang/pdf-tools" rel="nofollow noreferrer">Emacs its amazing PDF-tools</a> and <a href="https://pwmt.org/projects/zathura/" rel="nofollow noreferrer">Zathura</a> are my favorite PDF editor and viewer respectively).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-09T13:05:04.733Z
Answers.LastActivityDate: 2023-03-14T10:15:57.703Z
Answers.LastEditDate: 2023-03-14T10:15:57.703Z
Answers.UserDisplayName: dalanicolai
Answers.Body: <p>I am used to a simple and free tool that adds clickable bookmarks to PDF or DjVU files: <a href="http://handyoutlinerfo.sourceforge.net/" rel="nofollow noreferrer">http://handyoutlinerfo.sourceforge.net/</a>.</p>
  
  <p>You first have to prepare (and import in the tool) bookmarks entered in a text file as an indented list of labels and pages. Then the tool creates them in the document as bookmarks you can open/reduce and click on using the left panel.</p>
  
  <p>There are some good options such as shifting all page numbers first with a given constant. This is useful when the prepared file is actually a copy/paste extract from the table of contents existing (but only as text and without bookmarks) in the document: as cover, preface, introduction, ... are generally numbered separately. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-27T10:58:28.037Z
Answers.Text: Welcome to SE-ebooks. You may note that this is an older question, so that reactions may be less. Nevertheless, answering older questions is encouraged when it provides new data or knowledge.
Answers.UserDisplayName: babou
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-23T19:19:30.663Z
Answers.LastActivityDate: 2018-09-23T19:19:30.663Z
Answers.UserDisplayName: Patrick Bourdon
Answers.Body: <p>The best solution I found:</p>
  <p><a href="https://wiki.gnome.org/Attic/PdfMod" rel="nofollow noreferrer">https://wiki.gnome.org/Attic/PdfMod</a></p>
  <p>Extract:</p>
  <blockquote>
  <p>You can reorder, rotate, and remove pages, export images from a document, edit the title, subject, author, and keywords, and combine documents via drag and drop.</p>
  </blockquote>
  <p>It also allow to modify and/or create the Table of content.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-04T08:07:09.110Z
Answers.LastActivityDate: 2021-07-04T08:07:09.110Z
Answers.UserDisplayName: Dieudo
Answers.Body: <p>WPS office suite on Windows allows creating or editing pdf's TOC</p>
  
  <p>Be aware that it's a bit invasive though (shortcuts, default, runs in background...)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-18T11:54:05.007Z
Answers.LastActivityDate: 2019-11-18T11:54:05.007Z
Answers.UserDisplayName: Arthur
Answers.Body: <p>So in 2023, a better way to do this is to use <code>cpdf</code> and an LLM (large language model), like GPT4.
  To add bookmarks to a PDF using its Table of Contents (TOC) with cpdf, follow these steps:</p>
  <ol>
  <li><p><strong>Extract the TOC</strong>: Utilize an Optical Character Recognition (OCR) tool (e.g. <code>ocrmypdf</code>) and then extract the TOC from your PDF in text form.</p>
  </li>
  <li><p><strong>Format the TOC for <code>cpdf</code></strong>: The tool requires a specific format <code>level page &quot;Title&quot;</code></p>
  <p>Where:</p>
  <ul>
  <li>level is 0 for main chapters, 1 for sub-chapters, etc.</li>
  <li>page is the page number.</li>
  <li>Title is the chapter or sub-chapter name.</li>
  </ul>
  <p>For instance:</p>
  <pre><code>0 5 &quot;Introduction&quot;
  1 6 &quot;History&quot;
  0 10 &quot;Main Content&quot;
  1 11 &quot;Sub-topic 1&quot;
  </code></pre>
  <p>You can generally use prompting of an LLM to get the text from 1. in this format.</p>
  </li>
  <li><p><strong>Save Formatted TOC</strong>: Save the data to a text file, e.g., toc_cpdf.txt.</p>
  </li>
  <li><p><strong>Add Bookmarks with cpdf</strong>:</p>
  <pre><code>cpdf -add-bookmarks toc_cpdf.txt in.pdf -o out.pdf
  </code></pre>
  <p>This takes <code>in.pdf</code>, uses <code>toc_cpdf.txt</code> for bookmarks, and outputs to <code>out.pdf</code>.</p>
  </li>
  </ol>
  <p>Now, <code>out.pdf</code> will have bookmarks according to the original document's TOC.</p>
  <p>For details on <code>cpdf</code> including installation instructions consider the <a href="https://www.coherentpdf.com/usage-examples.html" rel="nofollow noreferrer"><code>cpdf</code> usage examples</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-26T14:08:52.647Z
Answers.LastActivityDate: 2023-09-26T14:08:52.647Z
Answers.UserDisplayName: HaoZeke
Answers.Body: <p>Here is my repository that I plan to automate the procedure.
  <a href="https://github.com/aminya/tocPDF" rel="nofollow noreferrer">https://github.com/aminya/tocPDF</a></p>
  
  <p>For now, it is the manual procedure (which is also inspired by other people answers).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-03T03:32:10.213Z
Answers.LastActivityDate: 2019-07-03T03:32:10.213Z
Answers.UserDisplayName: Amin Ya
Answers.Body: <p>I understand most of you probably prefer an open source command line based solution but sometimes a good app can save you a lot of time.</p>
  <p>If you're on a Mac I can recommend:</p>
  <p><a href="https://www.onekerato.com/pdfoutliner.html" rel="nofollow noreferrer">https://www.onekerato.com/pdfoutliner.html</a></p>
  <p>It only costs $4.55 and gives you an excellent gui to build that TOC.  It has both autogenerate and a manual mode.</p>
  <p>note: I'm not related to the developer or have any incentive promoting it, I just used his tool to add a toc a pdf and it works really well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-12T14:18:10.530Z
Answers.LastActivityDate: 2022-01-12T14:18:10.530Z
Answers.UserDisplayName: Stofke
Answers.Body: <p>The full Adobe Acrobat Pro ver.8 is available as a free legal download from <a href="http://www.techspot.com/downloads/4683-adobe-acrobat-8-free.html" rel="nofollow">http://www.techspot.com/downloads/4683-adobe-acrobat-8-free.html</a> for both Mac and Windows. Sure, it's not the latest version, but free is good, and it works just fine for adding or editing a table of contents.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-25T18:16:22.757Z
Answers.Text: That does not look legal - I would suspect it of having viruses and trojans. Where on Adobe's site can you get it? or at least an Adobe document saying it is legal
Answers.UserDisplayName: mmmmmm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-03T13:20:45.613Z
Answers.Text: Now, at least, it's only free for 7 days.
Answers.UserDisplayName: Diagon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-25T04:11:40.760Z
Answers.LastActivityDate: 2016-08-25T04:11:40.760Z
Answers.UserDisplayName: Gunnar Heiberg
Answers.Body: <p>Well, you could always buy a copy of <a href="http://www.adobe.com/products/acrobat.html">Adobe Acrobat</a>, which is actually designed solely for the purpose of creating and editing PDF files.</p>
  
  <p>Or you could import the file into <a href="http://calibre-ebook.com/">Calibre</a>, convert it to ePub format, edit the ePub to add the table of contents, then convert it back to PDF.</p>
  
  <p>There are other free tools for working with PDF directly. If you do a Google search, you would find <a href="http://www.labnol.org/software/edit-pdf-files/10870/">this page</a> which lists several free tools for editing or modifying PDF in various ways.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-12-19T12:53:49.170Z
Answers.LastActivityDate: 2013-12-19T12:53:49.170Z
Answers.UserDisplayName: Donald.McLean
Body: <p>I have many technical PDF ebooks where there are no chapters (no clickable table of contents or other means for quick navigation through a document) and therefore it's really painful to search for information without full-text search. How could I create them? I would like to just take a PDF book and generate exact structure of chapters and subchapters, like:</p>
  
  <ol>
  <li>Intro</li>
  <li>First chapter
  <ul>
  <li>Tools you will need</li>
  <li>Tool XYZ</li>
  </ul></li>
  <li>Second chapter</li>
  </ol>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-06T11:35:35.577Z
Comments.Text: As there are more than one option to edit PDFs (both free & paid), For Mac: Adobe Reader, PDF Expert could be useful options, For Windows: Foxit Reader, Xodo are possible options. You can learn almost everything about editing PDFs and many other possible PDF Editors via [this guide to editing PDFs.](https://www.jotform.com/edit-pdf)
Comments.UserDisplayName: Erman Ergün
Tags: <pdf><table-of-contents><hyperlinking><chapters>
Title: How to create clickable table of contents in a PDF?
UserDisplayName: Ondrej Janacek
ThreadText: # How to create clickable table of contents in a PDF?
  
  (Asked by: username_0 on 2013-12-19)
  
  I have many technical PDF ebooks where there are no chapters (no clickable table of contents or other means for quick navigation through a document) and therefore it's really painful to search for information without full-text search. How could I create them? I would like to just take a PDF book and generate exact structure of chapters and subchapters, like:
  
  1. Intro
  2. First chapter
     * Tools you will need
     * Tool XYZ
  3. Second chapter
  
  **Tags:** pdf, table-of-contents, hyperlinking, chapters
  
  > **Comments:**
  > **username_1**: As there are more than one option to edit PDFs (both free & paid), For Mac: Adobe Reader, PDF Expert could be useful options, For Windows: Foxit Reader, Xodo are possible options. You can learn almost everything about editing PDFs and many other possible PDF Editors via [this guide to editing PDFs.](https://www.jotform.com/edit-pdf)  
  
  ---
  
  ## 15 answers
  
  ### Answer by username_22 on 2013-12-19. Score: 14 | Accepted answer
  
  Well, you could always buy a copy of [Adobe Acrobat](http://www.adobe.com/products/acrobat.html), which is actually designed solely for the purpose of creating and editing PDF files.
  
  Or you could import the file into [Calibre](http://calibre-ebook.com/), convert it to ePub format, edit the ePub to add the table of contents, then convert it back to PDF.
  
  There are other free tools for working with PDF directly. If you do a Google search, you would find [this page](http://www.labnol.org/software/edit-pdf-files/10870/) which lists several free tools for editing or modifying PDF in various ways.
  
  
  ---
  
  ### Answer by username_2 on 2013-12-21. Score: 13
  
  I have used jPdfBookmarks on both Windows and Linux to do exactly what you describe - create your own bookmarks. Find it [here](http://sourceforge.net/projects/jpdfbookmarks/).
  
  > **Comments:**
  > **username_3**: This is my recommendation if you have a table of contents beforehand. See my [full tutorial](https://ebooks.stackexchange.com/a/7763/10251) below  
  
  ---
  
  ### Answer by username_4 on 2017-04-03. Score: 10
  
  [k2pdfopt](http://username_4.com/k2pdfopt) (free, open source) can also do this by supplying a text file. See the [`-toclist` option](https://www.username_4.com/k2pdfopt/help/options.shtml). Use like so:
  
  ```
  k2pdfopt -mode copy -n -toclist my_chapter_list.txt srcfile.pdf -o outfile.pdf
  
  ```
  
  ...where my\_chapter\_list.txt is a simple ASCII file with page numbers beginning each line, e.g.
  
  ```
  1 Cover
  2 Table of Contents
  5 Chapter 1
  25 Chapter 2
  ...
  
  ```
  
  > **Comments:**
  > **username_5**: Does this edit the PDF in any way other than adding the toc as metadata?  
  > **username_4**: Actually cpdf is a better tool to use for this--it is more straightforward to use and has a very similar option (-add-bookmarks) demonstrated at https://www.coherentpdf.com/usage-examples.html.  
  
  ---
  
  ### Answer by username_6 on 2013-12-19. Score: 8
  
  I will extend on @Donald's answer but I would also like to note I do **not** recommend, personally for quality issues, ever using Calibre for ebook development.
  
  As stated I would suggest getting a copy of [Acrobat](http://success.adobe.com/en/na/sem/products/acrobat/acrobat.html?kw=purch&sdid=KARFP&skwcid=AL!3085!3!33670697302!e!!g!!adobe%20acrobat&ef_id=Upy1YgAAAUH18d7P:20131219152230:s) and you can (for this example I am using Acrobat X Standard but note the I have not seen any difference in the shortcut buttons BUT the GUI has changed since version 9):
  
  * Open the `.pdf` file in Acrobat
  
  ![enter image description here](https://i.sstatic.net/aRP68.png)
  
  * Select the bookmark tab on the left side
  
  ![enter image description here](https://i.sstatic.net/gvaSI.png)
  
  * Create the bookmark (two different ways):
    By button ![enter image description here](https://i.sstatic.net/UTboR.png), or use shortcut `Ctrl + B` (windows) or `Cmd + B` (mac):
  
  ![enter image description here](https://i.sstatic.net/RR9EC.png)
  
  You can create bookmarks by dragging and dropping the sub-level bookmark onto the level 1 bookmark such as:
  
  ![enter image description here](https://i.sstatic.net/1XBXb.png)
  
  > **Comments:**
  > **username_7**: A slightly cheaper alternative to Adobe Acrobat is Foxit's [PhantomPDF](http://www.foxitsoftware.com/Secure_PDF_Security/). I use it at the office a lot to manipulate bookmarks in PDF documents.  
  
  ---
  
  ### Answer by username_3 on 2018-04-22. Score: 7
  
  # [Jpdfbookmark](https://sourceforge.net/projects/jpdfbookmarks/) can work for scanned books
  
  1. Prepare the TOC in .txt file
  
     ```
      Chapter 1. The Beginning/23
          Para 1.1 Child of The Beginning/25,FitWidth,96
              Para 1.1.1 Child of Child of The Beginning/26,FitHeight,43
      Chapter 2. The Continue/30,TopLeft,120,42
          Para 2.1 Child of The Beginning/32,FitPage
  
     ```
  
     You can extract the TOC by using [table recognition with OCR](https://ocr.space/tablerecognition), then use regex to fix it.
  2. Load that TOC
  
     ![](https://i.sstatic.net/GEZqN.png)
  3. Expand all bookmarks (`Ctrl + E`), select all of them, then go to Tools > Apply Page Offset
  
     ![](https://i.sstatic.net/wmZfU.png)
  4. Enter the first pages that outmatch the page number in the TOC
  
  Your can read its [manual](http://jpdfbookmarks.altervista.org/InsertBookmarks.html#1_3_1) or watch a [quick video tutorial](https://youtu.be/7DUkvH7_wII?t=30). It has command line mode and can work on Linux, Mac.
  
  > **Comments:**
  > **username_8**: Your OCR to TOC link is broken, can you share a new link?  
  > **username_3**: Thanks for your notice. I've changed to a better link  
  > **username_8**: To get pictures of the original TOC I followed this guide: https://askubuntu.com/questions/50170/how-to-convert-pdf-to-image/50180#50180  
  
  ---
  
  ### Answer by username_9 on 2013-12-19. Score: 7
  
  There are also free tools that allow editing/adding bookmarks. A cross platform example is [jPdf Tweak](http://jpdftweak.sourceforge.net/).
  
  It is a little clumsy to use, but you can create the table of contents in your favourite spreadsheet program, export as csv and then just import it.
  
  > **Comments:**
  > **username_10**: I also have used jpdftweak, and it has served me quite well. Page numbers can be a bit confusing if you want them labeled i, ii, iii, iv, ... and then 1, 2, 3 ..., but it's worth taking the time to figure it out.  
  
  ---
  
  ### Answer by username_11 on 2020-07-29. Score: 3
  
  I wrote an open source command line toolset called [pdf.tocgen](https://username_11.com/voice/pdf.tocgen/) just for doing this. It uses the embedded font attributes and position information of headings to generate a table of contents automatically.
  
  For example, for the PDF version Paul Graham's *On Lisp*, available for download on [his website](http://www.paulgraham.com/onlisptext.html) but comes without a table of contents. You could use the `pdfxmeta` tool to build a "recipe" file
  
  ```
  [[heading]]
  level = 1
  font.name = "Times-Bold"
  font.size = 19.92530059814453
  
  [[heading]]
  level = 2
  font.name = "Times-Bold"
  font.size = 11.9552001953125
  
  ```
  
  save it as `recipe.toml`, and use the `pdftocgen` command to automatically generate an outline
  
  ```
  $ pdftocgen onlisp.pdf < recipe.toml
  "Preface" 5
      "Bottom-up Design" 5
      "Plan of the Book" 7
      "Examples" 9
      "Acknowledgements" 9
  "Contents" 11
  "The Extensible Language" 14
      "1.1 Design by Evolution" 14
      "1.2 Programming Bottom-Up" 16
      "1.3 Extensible Software" 18
      "1.4 Extending Lisp" 19
      "1.5 Why Lisp (or When)" 21
  "Functions" 22
      "2.1 Functions as Data" 22
      "2.2 Defining Functions" 23
      "2.3 Functional Arguments" 26
      "2.4 Functions as Properties" 28
      "2.5 Scope" 29
      "2.6 Closures" 30
      "2.7 Local Functions" 34
      "2.8 Tail-Recursion" 35
      "2.9 Compilation" 37
      "2.10 Functions from Lists" 40
  [--snip--]
  
  ```
  
  You could save the output to a file called `toc`
  
  ```
  $ pdftocgen onlisp.pdf < recipe.toml > toc
  
  ```
  
  and import it to the PDF file using `pdftocio`:
  
  ```
  $ pdftocio -o output.pdf onlisp.pdf < toc
  
  ```
  
  Please read the [homepage](https://username_11.com/voice/pdf.tocgen/) for the details on how to use this toolset. I hope you find it useful.
  
  
  ---
  
  ### Answer by username_12 on 2020-06-09. Score: 3
  
  ***THIS PART IS EDITED***
  
  For 'software-generated' PDF-files, i.e. PDF's not created from scans, I recommend to use (and upvote the answer by username_11) [pdf.tocgen](https://username_11.com/voice/pdf.tocgen/). Using this package becomes even easier with the [toc-mode](https://github.com/username_12/toc-mode) package for (Spac)Emacs described next.
  
  For all other PDF and DJVU documents there is [a package called toc-mode](https://github.com/username_12/toc-mode) for Emacs, which in my opinion provides the easiest way to add Table of Contents to documents (for linux and possibly also for different OS's). It includes options to extract the TOC via OCR, and one can use Emacs/Evil (vim) keyboard macro's and the handy table editor to fix it.
  
  In case this package's functionality is not sufficient or using Emacs is no option then the remaining part of this answer remains valid.
  
  ***END EDITED PART***
  
  (Not enough reputation points to comment) Like the answer by username_13 username_13, I would also recommend [HandyOutliner](http://handyoutlinerfo.sourceforge.net/). However, I would suggest you try the [python script called document-contents-extractor](https://pypi.org/project/document-contents-extractor/) to extract the contents.
  
  If these options do not work, then I would also like to recommend [PDF-XChange Viewer](https://www.tracker-software.com/product/pdf-xchange-viewer) as a very powerful bookmark/contents extractor (selected text can be easily added to the bookmarks). It works well under wine.
  
  Although not related to the question, I just would like to add that at the moment PDF X-Change viewer appears to me to be the most powerful PDF editor/viewer on linux. Okular is doing very well of course also. Anyway, still [Emacs its amazing PDF-tools](https://github.com/vedang/pdf-tools) and [Zathura](https://pwmt.org/projects/zathura/) are my favorite PDF editor and viewer respectively).
  
  
  ---
  
  ### Answer by username_13 on 2018-09-23. Score: 3
  
  I am used to a simple and free tool that adds clickable bookmarks to PDF or DjVU files: <http://handyoutlinerfo.sourceforge.net/>.
  
  You first have to prepare (and import in the tool) bookmarks entered in a text file as an indented list of labels and pages. Then the tool creates them in the document as bookmarks you can open/reduce and click on using the left panel.
  
  There are some good options such as shifting all page numbers first with a given constant. This is useful when the prepared file is actually a copy/paste extract from the table of contents existing (but only as text and without bookmarks) in the document: as cover, preface, introduction, ... are generally numbered separately.
  
  > **Comments:**
  > **username_14**: Welcome to SE-ebooks. You may note that this is an older question, so that reactions may be less. Nevertheless, answering older questions is encouraged when it provides new data or knowledge.  
  
  ---
  
  ### Answer by username_15 on 2021-07-04. Score: 2
  
  The best solution I found:
  
  <https://wiki.gnome.org/Attic/PdfMod>
  
  Extract:
  
  > You can reorder, rotate, and remove pages, export images from a document, edit the title, subject, author, and keywords, and combine documents via drag and drop.
  
  It also allow to modify and/or create the Table of content.
  
  
  ---
  
  ### Answer by username_16 on 2019-11-18. Score: 1
  
  WPS office suite on Windows allows creating or editing pdf's TOC
  
  Be aware that it's a bit invasive though (shortcuts, default, runs in background...)
  
  
  ---
  
  ### Answer by username_17 on 2023-09-26. Score: 1
  
  So in 2023, a better way to do this is to use `cpdf` and an LLM (large language model), like GPT4.
  To add bookmarks to a PDF using its Table of Contents (TOC) with cpdf, follow these steps:
  
  1. **Extract the TOC**: Utilize an Optical Character Recognition (OCR) tool (e.g. `ocrmypdf`) and then extract the TOC from your PDF in text form.
  2. **Format the TOC for `cpdf`**: The tool requires a specific format `level page "Title"`
  
     Where:
  
     * level is 0 for main chapters, 1 for sub-chapters, etc.
     * page is the page number.
     * Title is the chapter or sub-chapter name.
  
     For instance:
  
     ```
     0 5 "Introduction"
     1 6 "History"
     0 10 "Main Content"
     1 11 "Sub-topic 1"
  
     ```
  
     You can generally use prompting of an LLM to get the text from 1. in this format.
  3. **Save Formatted TOC**: Save the data to a text file, e.g., toc\_cpdf.txt.
  4. **Add Bookmarks with cpdf**:
  
     ```
     cpdf -add-bookmarks toc_cpdf.txt in.pdf -o out.pdf
  
     ```
  
     This takes `in.pdf`, uses `toc_cpdf.txt` for bookmarks, and outputs to `out.pdf`.
  
  Now, `out.pdf` will have bookmarks according to the original document's TOC.
  
  For details on `cpdf` including installation instructions consider the [`cpdf` usage examples](https://www.coherentpdf.com/usage-examples.html).
  
  
  ---
  
  ### Answer by username_18 on 2019-07-03. Score: 1
  
  Here is my repository that I plan to automate the procedure.
  <https://github.com/username_18/tocPDF>
  
  For now, it is the manual procedure (which is also inspired by other people answers).
  
  
  ---
  
  ### Answer by username_19 on 2022-01-12. Score: 1
  
  I understand most of you probably prefer an open source command line based solution but sometimes a good app can save you a lot of time.
  
  If you're on a Mac I can recommend:
  
  <https://www.onekerato.com/pdfoutliner.html>
  
  It only costs $4.55 and gives you an excellent gui to build that TOC. It has both autogenerate and a manual mode.
  
  note: I'm not related to the developer or have any incentive promoting it, I just used his tool to add a toc a pdf and it works really well.
  
  
  ---
  
  ### Answer by username_20 on 2016-08-25. Score: 0
  
  The full Adobe Acrobat Pro ver.8 is available as a free legal download from <http://www.techspot.com/downloads/4683-adobe-acrobat-8-free.html> for both Mac and Windows. Sure, it's not the latest version, but free is good, and it works just fine for adding or editing a table of contents.
  
  > **Comments:**
  > **username_21**: That does not look legal - I would suspect it of having viruses and trojans. Where on Adobe's site can you get it? or at least an Adobe document saying it is legal  
  > **username_10**: Now, at least, it's only free for 7 days.  
  
  ---
  
  