8

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?

  • 2
    On "seems not a difficult task": read the >1,000 pp. Adobe PDF Reference 1.7 and decide for yourself. – Jongware Mar 30 '14 at 11:02
  • 5
    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. – his Mar 30 '14 at 12:00
  • 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. – Dipak Patel Mar 30 '14 at 14:51
  • Duplicate of What's the correct way for developers to edit PDF?. Please don't crosspost! – Martin Schröder Mar 31 '14 at 16:54
  • You probably should not (get the sources!). However, there are more or less cumbersome ways to add content to a PDF. – Raphael Apr 1 '14 at 13:41
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. 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…).

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.

To answer your (rather rhetoric) question why editing tools are so rare… Well, PDF is much more than a simple text file…

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.

1

Answering the more general questions, there are three basic kinds of editing.

  1. Add, remove or rearrange pages

    Use software like PDFtk which can do all of this.

  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.

    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 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 to convert individual pages to SVG, edit and reconvert to PDF (cf 1.). This 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.

1

You can use 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.