Answers.Body: <p>The "problem" is that PDF does not know anything about its contents and the structure of the document (unless structure has been added — maybe in order to make it accessible). That means that you will have to find other ways to recognize (in the case of a TOC) titles etc. </p>
  
  <p>One possibility (and I think PDFpen is doing it that way) is enumerating all text elements, comparing them with the properties you defined to be "Title", stitching them together (note that a word in PDF may not correspond to what we see as "word"), making a list of titles, creating another page, and create the TOC, and finally establish links between the TOC and the corresponding targets.</p>
  
  <p>Another thing to know is that it is way more complex to read a PDF than to write one. This is the reason why there are more libraries (and applications) to just write PDF than being able to read and write (and even fewer to understand PDF…).</p>
  
  <p>Now, because creating a TOC is something which does not happen that frequently, and requires a visual check anyway, it would be reasonable instead of trying to create an application to do it, to seriously consider a plug-in for Acrobat. Acrobat's API for plug-ins is rather extensive, and reasonably well documented, and there is a community which provides support. AFAIK, this API is free to use, unless you want the plug-in work with Reader (in that case, you will need a license from Adobe). The advantage of this approach is that you can rely on Acrobat's capabilities to write a proper, good quality PDF.</p>
  
  <p>To answer your (rather rhetoric) question why editing tools are so rare… Well, PDF is much more than a simple text file…</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-03-30T10:45:46.720Z
Answers.LastActivityDate: 2014-03-30T10:45:46.720Z
Answers.UserDisplayName: Max Wyss
Answers.Body: <p>PDF is a fixed layout format. It is <em>not</em> designed for editing content. The only information that exists is the visual layout: a series of draw character 'c' with font 'Times' at coordinate x, y instructions. There is no information in PDF that tells you anything about the content like paragraph breaks or headings.</p>
  
  <p>If you want to change the contents you should edit the source document and regenerate the PDF.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-04-08T15:43:53.977Z
Answers.LastActivityDate: 2014-04-08T15:43:53.977Z
Answers.UserDisplayName: ccxvii
Answers.Body: <p>You can use <a href="https://www.pdfescape.com/" rel="nofollow">Pdfescape.com</a>, it's free, super easy to do anything, The PDF must be less than 10MB and less than 100 pages.</p>
  
  <p>I guess if you pay them, there will be no more limitations, but I'm not sure about that, you can ask them. It's $20 per year.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-04-02T15:41:33.173Z
Answers.LastActivityDate: 2014-04-02T15:41:33.173Z
Answers.UserDisplayName: Lynob
Answers.Body: <p>Answering the more general questions, there are three basic kinds of editing.</p>
  
  <ol>
  <li><p><strong>Add, remove or rearrange pages</strong></p>
  
  <p>Use software like <a href="http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/" rel="nofollow noreferrer">PDFtk</a> which can do all of this.</p></li>
  <li><p><strong>Add content to existing pages</strong></p>
  
  <p>You can use LaTeX to draw anything on PDF pages. Basically, you can use the existing PDF and background and all your LaTeX on top; see e.g. <a href="https://tex.stackexchange.com/questions/64634/how-to-add-a-picture-onto-an-existing-pdf-file">here</a>.</p>
  
  <p>If LaTeX is too much of a roadblock (meaning, you don't know it yet), using Inkscape may work nicely (see below).</p></li>
  <li><p><strong>Change content on existing pages</strong></p>
  
  <p>That's the hardest one. You can use software like <a href="http://sourceforge.net/projects/pdfedit/" rel="nofollow noreferrer">PDFEdit</a> to make changes, but as long as you don't use Adobe's expensive tools, things are going to be nasty (or so I hear).</p>
  
  <p>You can use <a href="http://www.inkscape.org/en/" rel="nofollow noreferrer">Inkscape</a> to convert individual pages to SVG, edit and reconvert to PDF (cf 1.). This has limitations, though.</p></li>
  </ol>
  
  <p>For your example of adding a table of contents, let me recommend the following approach. Stop whenever you have the result.</p>
  
  <ol>
  <li>Get the sources, add the ToC there, and reconvert to PDF.</li>
  <li>Get the ToC pages (as PDF) and insert them (see above).</li>
  <li>Write your own ToC with any tool, laboriously matching the original documents formatting, and insert it (see above).</li>
  </ol>
  
  <p>Since all but the first alternative <em>will</em> break page numbering and the third <em>will</em> (most likely) break formatting, it's clear what the best alternative is.</p>
  
  <p><sup>Tell your client to start using LaTeX.</sup></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-04-01T13:53:07.323Z
Answers.LastActivityDate: 2014-04-01T14:09:26.420Z
Answers.LastEditDate: 2017-04-13T12:34:32.050Z
Answers.UserDisplayName: Raphael
Body: <p>Recently I got a task to manually add a <strong>Table of Contents</strong> to a PDF ebook generated by someone who left it out. I didn't know how to do it at first so I googled a lot and found some small tools but none works really well. Finally I have to head to a <strong>commercial</strong> software called <code>PDFpenPro</code>, and it eventually fulfilled my needs. </p>
  
  <p>My question is, since <code>PDF</code> is a <em>open standard</em>, why the excellent open-source editing tools are so scarce? I'm not an expert on ebooks and standards, but I still wonder what blocked programmers from building up great open-source tools? </p>
  
  <p>Anyway, adding a <code>TOC</code> (and many other basic but useful editing) seems not a very difficult task, can we developer do it by ourselves, by making use of and contributing to some open-source libraries? Can anyone lists some handy resources I can count on?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-30T12:00:17.733Z
Comments.Text: Personal view: PDF is not a good ebook format. In fact it is the worst one. I personally will not spend a single second of my free, unpaid time on optimizing its usage. In fact I would see this as counter productive  as it reduces the pressure for content creators to use a real ebook format.
Comments.UserDisplayName: his
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-30T14:51:24.670Z
Comments.Text: My understanding that PDF is mainly an output format. Some tools (like Adobe Creative Suite) use PDF as a source file format, but mainly you would edit the source file (Markdown, Word, InDesign etc) to create/edit the content and then output finally as PDF. However I think your question about why there aren't good choices for opensource PDF editing tools is a good one.
Comments.UserDisplayName: Dipak Patel
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-31T16:54:59.123Z
Comments.Text: Duplicate of [What's the correct way for developers to edit PDF?](http://stackoverflow.com/q/22740706). Please don't crosspost!
Comments.UserDisplayName: Martin Schröder
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-01T13:41:01.733Z
Comments.Text: You probably should not (get the sources!). However, there are more or less cumbersome ways to add content to a PDF.
Comments.UserDisplayName: Raphael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-01T13:42:08.783Z
Comments.Text: @his For some kinds of books, e.g. technical textbooks, PDF is as of yet *the* format since other formats suck. That is to say, the tools for producing and viewing them suck; EPUB3 specifies all facilities you need.
Comments.UserDisplayName: Raphael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-02T09:50:06.367Z
Comments.Text: @Raphael No, the simple reason is that it is even harder to create a technical book compared to a "simple" text for a graphical oriented designer (and sadly those with the in this case misleading education are still the ones doing this) while the benefit is marginal. Most people just don't read technical books on small screens (6" or smaller). For bigger screens PDF still is awful but it works. It just doesn't pay for books for a small market. Yet. We (as software developers) need to automate the process and leave the designers no choice than to do it right (or, truthfully, to kick them out).
Comments.UserDisplayName: his
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-02T10:02:07.913Z
Comments.Text: @his I don't think PDF is "awful" for screen viewing; it has pros and cons. Technical books *do* have a smaller market, and often the printed book is preferable over any digital version. But when it comes down to the question, "Do I make an EPUB, too?" the answer is, "No, because it's not really possible." I'm thinking of LaTeX sources with formulae, technical (vector) drawings, cross-references, footnotes, margin notes and so on -- there is just not way to transform that into an EPUB (as of now). You have to completely redo it, and that's prohibitive effort given limited market.
Comments.UserDisplayName: Raphael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-02T10:03:44.490Z
Comments.Text: @his Also, don't underestimate the power of page numbers. "As you can see from formula (3) on page 37..." --  how does that work with reflowed ebooks?
Comments.UserDisplayName: Raphael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-12T03:36:32.993Z
Comments.Text: Price aside, PhantomPDF (by Foxit) is probably the most affordable PDF editor out there that actually offers enough features to adequately edit a PDF document. The next step up is Acrobat.
Comments.UserDisplayName: Nathan Osman
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-04-25T02:07:29.623Z
Comments.Text: Interestingly, the PDF file format reference is distributed as a... PDF file.
Comments.UserDisplayName: Nathan Osman
Tags: <pdf>
Title: What's the correct way for developers to edit PDF?
UserDisplayName: qweruiop
ThreadText: # What's the correct way for developers to edit PDF?
  
  (Asked by: username_0 on 2014-03-30)
  
  Recently I got a task to manually add a **Table of Contents** to a PDF ebook generated by someone who left it out. I didn't know how to do it at first so I googled a lot and found some small tools but none works really well. Finally I have to head to a **commercial** software called `PDFpenPro`, and it eventually fulfilled my needs.
  
  My question is, since `PDF` is a *open standard*, why the excellent open-source editing tools are so scarce? I'm not an expert on ebooks and standards, but I still wonder what blocked programmers from building up great open-source tools?
  
  Anyway, adding a `TOC` (and many other basic but useful editing) seems not a very difficult task, can we developer do it by ourselves, by making use of and contributing to some open-source libraries? Can anyone lists some handy resources I can count on?
  
  **Tags:** pdf
  
  > **Comments:**
  > **username_1**: Personal view: PDF is not a good ebook format. In fact it is the worst one. I personally will not spend a single second of my free, unpaid time on optimizing its usage. In fact I would see tusername_1 as counter productive  as it reduces the pressure for content creators to use a real ebook format.  
  > **username_2**: My understanding that PDF is mainly an output format. Some tools (like Adobe Creative Suite) use PDF as a source file format, but mainly you would edit the source file (Markdown, Word, InDesign etc) to create/edit the content and then output finally as PDF. However I think your question about why there aren't good choices for opensource PDF editing tools is a good one.  
  > **username_3**: Duplicate of [What's the correct way for developers to edit PDF?](http://stackoverflow.com/q/22740706). Please don't crosspost!  
  > **username_4**: You probably should not (get the sources!). However, there are more or less cumbersome ways to add content to a PDF.  
  > **username_4**: @username_1 For some kinds of books, e.g. technical textbooks, PDF is as of yet *the* format since other formats suck. That is to say, the tools for producing and viewing them suck; EPUB3 specifies all facilities you need.  
  > **username_1**: @username_4 No, the simple reason is that it is even harder to create a technical book compared to a "simple" text for a graphical oriented designer (and sadly those with the in tusername_1 case misleading education are still the ones doing tusername_1) while the benefit is marginal. Most people just don't read technical books on small screens (6" or smaller). For bigger screens PDF still is awful but it works. It just doesn't pay for books for a small market. Yet. We (as software developers) need to automate the process and leave the designers no choice than to do it right (or, truthfully, to kick them out).  
  > **username_4**: @username_1 I don't think PDF is "awful" for screen viewing; it has pros and cons. Technical books *do* have a smaller market, and often the printed book is preferable over any digital version. But when it comes down to the question, "Do I make an EPUB, too?" the answer is, "No, because it's not really possible." I'm thinking of LaTeX sources with formulae, technical (vector) drawings, cross-references, footnotes, margin notes and so on -- there is just not way to transform that into an EPUB (as of now). You have to completely redo it, and that's prohibitive effort given limited market.  
  > **username_4**: @username_1 Also, don't underestimate the power of page numbers. "As you can see from formula (3) on page 37..." --  how does that work with reflowed ebooks?  
  > **username_5**: Price aside, PhantomPDF (by Foxit) is probably the most affordable PDF editor out there that actually offers enough features to adequately edit a PDF document. The next step up is Acrobat.  
  > **username_5**: Interestingly, the PDF file format reference is distributed as a... PDF file.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2014-03-30. Score: 8
  
  The "problem" is that PDF does not know anything about its contents and the structure of the document (unless structure has been added — maybe in order to make it accessible). That means that you will have to find other ways to recognize (in the case of a TOC) titles etc.
  
  One possibility (and I think PDFpen is doing it that way) is enumerating all text elements, comparing them with the properties you defined to be "Title", stitching them together (note that a word in PDF may not correspond to what we see as "word"), making a list of titles, creating another page, and create the TOC, and finally establish links between the TOC and the corresponding targets.
  
  Another thing to know is that it is way more complex to read a PDF than to write one. Tusername_1 is the reason why there are more libraries (and applications) to just write PDF than being able to read and write (and even fewer to understand PDF…).
  
  Now, because creating a TOC is something which does not happen that frequently, and requires a visual check anyway, it would be reasonable instead of trying to create an application to do it, to seriously consider a plug-in for Acrobat. Acrobat's API for plug-ins is rather extensive, and reasonably well documented, and there is a community which provides support. AFAIK, tusername_1 API is free to use, unless you want the plug-in work with Reader (in that case, you will need a license from Adobe). The advantage of tusername_1 approach is that you can rely on Acrobat's capabilities to write a proper, good quality PDF.
  
  To answer your (rather rhetoric) question why editing tools are so rare… Well, PDF is much more than a simple text file…
  
  
  ---
  
  ### Answer by username_7 on 2014-04-08. Score: 2
  
  PDF is a fixed layout format. It is *not* designed for editing content. The only information that exists is the visual layout: a series of draw character 'c' with font 'Times' at coordinate x, y instructions. There is no information in PDF that tells you anything about the content like paragraph breaks or headings.
  
  If you want to change the contents you should edit the source document and regenerate the PDF.
  
  
  ---
  
  ### Answer by username_8 on 2014-04-02. Score: 1
  
  You can use [Pdfescape.com](https://www.pdfescape.com/), it's free, super easy to do anything, The PDF must be less than 10MB and less than 100 pages.
  
  I guess if you pay them, there will be no more limitations, but I'm not sure about that, you can ask them. It's $20 per year.
  
  
  ---
  
  ### Answer by username_4 on 2014-04-01. Score: 1
  
  Answering the more general questions, there are three basic kinds of editing.
  
  1. **Add, remove or rearrange pages**
  
     Use software like [PDFtk](http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) which can do all of tusername_1.
  2. **Add content to existing pages**
  
     You can use LaTeX to draw anything on PDF pages. Basically, you can use the existing PDF and background and all your LaTeX on top; see e.g. [here](https://tex.stackexchange.com/questions/64634/how-to-add-a-picture-onto-an-existing-pdf-file).
  
     If LaTeX is too much of a roadblock (meaning, you don't know it yet), using Inkscape may work nicely (see below).
  3. **Change content on existing pages**
  
     That's the hardest one. You can use software like [PDFEdit](http://sourceforge.net/projects/pdfedit/) to make changes, but as long as you don't use Adobe's expensive tools, things are going to be nasty (or so I hear).
  
     You can use [Inkscape](http://www.inkscape.org/en/) to convert individual pages to SVG, edit and reconvert to PDF (cf 1.). Tusername_1 has limitations, though.
  
  For your example of adding a table of contents, let me recommend the following approach. Stop whenever you have the result.
  
  1. Get the sources, add the ToC there, and reconvert to PDF.
  2. Get the ToC pages (as PDF) and insert them (see above).
  3. Write your own ToC with any tool, laboriously matching the original documents formatting, and insert it (see above).
  
  Since all but the first alternative *will* break page numbering and the third *will* (most likely) break formatting, it's clear what the best alternative is.
  
  Tell your client to start using LaTeX.
  
  
  ---
  
  