Bookmarks in your PDFs
Note that since the writing of this article, Affinity Publisher can now publish Bookmarks. But this method can still be used on PDF you possess.
While I'm working on finalizing the PDF for Facing the Titan, I abandoned (several months ago now) Scribus for Affinity Publisher. Then by the whole Affinity range eventually (even though I use very little photo for the moment).
Maybe I'll talk about my feedback on Affinity in a few weeks. I think the product is excellent. But it does have a few shortcomings. One of the most important is that it doesn't allow you to manage Bookmarks in a PDF.
Bookmarks are those links you can find in a panel on the side of your PDF reader, and which allow you to navigate within the document. Affinity Publisher doesn't manage these elements, so if you generate a PDF with it, you're left with three bookmark solutions.
- No bookmarks (not OK except on one- or two-page documents)
- Create bookmarks “by hand” using a PDF editor.
- Figure out how to factor all this work
I chose number three.
A combination of softwares
To begin with, this method will require the use of three software packages. Note that I'm working under Windows, and that this will no doubt require adjustments under another environment.
To start with, we'll need a spreadsheet program. For my part, I'm using Calc from Google Drive. Any other should do.
Next, we'll need a modern, high-performance text editor. My undisputed favorite is **Notepad++. Install it if you haven't already.
Finally, the software that will insert the bookmarks is called PDFtk (for PDF ToolKit) and will also require installation.
Prepare yourself a blank directory, and off you go!
Step 1 - Create bookmarks
To begin with, you'll need to have a list of bookmarks somewhere. This will be in the spreadsheet.
This document will show you how I work
It's really quite simple. The first column contains the bookmark text. The second contains its hierarchical level (1 at the base, then 2, then 3 for lower levels). The third column contains a formula that you can see in the image above. It's an automatic calculation of the start page, based on the start page of the previous entry, and the size in number of pages of the previous entry (the fourth column).
It is possible to dispense with this formula and the fourth column, but on a “living” document, whose pages can still move, this allows all pages to be recalculated dynamically.
Update this document as you go along.
Step 2 - Source file
We're now going to create a “source” file that will make it easy to generate all these bookmarks using PDFtk.
Select all your bookmarks in the spreadsheet, taking only the first three columns. Copy the data, and paste it into a new text document in Notepad++.
If you're using a spreadsheet program other than Google Calc, check that the data in each of your columns is separated by a tab. To do this, activate the tab display (the arrows, in the document above) by following the menu below (Display/Special symbols/.
Save the file as sourcebookmarks.txt
(or whatever, but I'll use this).
PDFtk won't be able to handle this file. We'll have to transform it.
Step 3 - Macro and transformations
We're going to perform several transformations on this text before sending it to PDFtk. To avoid having to repeat the operation often, we're going to record a macro in Notepad++.
A macro is a set of operations that are recorded so that they can be reproduced automatically.
Let's begin by starting a macro.
From now on, any operation performed in Notepad++ will be recorded.
We're going to use a Find/Replace (Ctrl+H) using the power of Regex to format our document.
Here's the code to put in Search and Replace respectively:
(.*)\t([0-9]*)\t([0-9]\*)
BookmarkBegin\nBookmarkTitle: $1\nBookmarkLevel: $2\nBookmarkPageNumber: $3
Don't forget to check the Regular Expression search mode, otherwise it won't work! Then click on Replace All, and your file will look like this:
PDFtk does not handle accented characters correctly. You'll need to make further changes to this file. This could be done by hand, but it might as well be automated.
This could not be needed in english language. It's especially needed for latin characters.
You'll need to replace all accented characters with their HTML equivalents. You'll find these correspondences under this link (and many others). Still in our macro, we'll once again use Find/Replace (Ctrl+H) to modify the é
characters.
The code to replace all our é characters will be é
. Please refer to the linked table for other accented characters you may need. Please note that uppercase and lowercase letters have different codes!
In Notepad++, we configure our action as follows.
Be sure to check Case sensitive and return to Normal search mode.
Do the same for each accented character (or special character, such as cedilla) you need, so that your file no longer contains any.
When your file is ready, finish the macro and save it.
Choose a name for your Macro and give it a shortcut, even if it's a dummy. Avoid using a shortcut that's already been assigned. Whatever the shortcut, your Macro will now appear in the Notepad++ menu!
Don't run it! Save your file instead.
Then open a new blank file, copy the data back from your spreadsheet and run your macro from the menu. It's magic! No need to redo all those Find/Replace operations when you update!
Step 4 - Integration
You now have a sourcebookmarks.txt
file ready for use with PDFtk. You should also have a PDF file with missing bookmarks. Let's call it mygame.pdf
.
Let's create a new file via Notepad++, and call it create_bookmarks.bat
. The bat
extension means that this is a batch file which we'll use to run a command line. The idea behind this file is to avoid having to worry about file paths once the process is running.
This file should be in the same directory as the sourcebookmarks.txt
and mygame.pdf
files. If you feel more comfortable, you can adapt this file to other paths.
The following code will fill our file. Nothing else.
pdftk mygame.pdf update_info sourcebookmarks.txt output mygame_bookmarked.pdf
If you'd like to know more about the contents of this command, please consult the PDFtk documentation.
Save the file. In the directory, double-click on the creer_bookmarks.bat
file, which briefly opens a command prompt.
A mygame_bookmarked.pdf
file is created, roughly the same size as the original. But it contains the bookmarks, organized hierarchically! Just what we've been waiting for!
Afterwards, if you update your document, just follow the procedure below:
- Has the list of bookmarks been modified (list of entries, page of entries)?
- YES:** I modify the entries in the spreadsheet, copy the entries to the
sourcebookmarks.txt
file and apply my macro. - In any case:** I copy my new PDF into the directory and name it
mygame.pdf
. I runcreate_bookmarks.bat
.
- YES:** I modify the entries in the spreadsheet, copy the entries to the
The aim behind the macro and the creation of the batch file is to automate future updates as much as possible!
Now, no more excuses for not having bookmarks in your PDFs!