This article will show you how to bookmark a PDF, but it’s not limited to that. You can also modify existing bookmarks in the same way.

In recent days, I have been studying Parsing of compilation principles, and I plan to read the classic book Parsing Techniques. As a result, when I opened the PDF I had downloaded before, I found that there was not even a bookmark table in the 600-page PDF.

So according to its catalog page, add a bookmarked version of the catalog to it, convenient for later browsing.

If you’re familiar with Vim, the whole process should only take about five minutes.

Tools:

  • Vim (emphasis)
  • FreePic2Pdf
  • PdgCntEditor

Vim needs no explanation. It’s a versatile editor for all kinds of text editing, and it still surprises you every now and then after years of use. You can use other regular replacement tools, but it’s not recommended to use a regular editor, because it takes a few minutes to do something that takes a long time.

FreePic2Pdf is a tool for modifying PDF files. You can convert a group of images into a PDF file, and also modify a PDF file, including contents and parameters. PdgCntEditor is a directory editing tool that makes it easier to modify PDF directories.

Next is the process of making PDF bookmarks directory.

Generate the PdgCntEditor directory

1. Extract directory information

Copy the directory text from the PDF and paste it into Vim:

2. Delete unnecessary text

There are some problems with PDF copy formatting, especially when you copy across pages, with header and footer text interspersed with the target text.

So, by command:%s/\d\zs\D\+$//gDelete extra text after page number:

Because the book is a three-level directory, too much is not easy to find, so you can only reserve to the second-level directory, delete the three-level directory, command:g/^\d\+.\d\+.\d/d:

3. Format

Target format:

Page number of Primary directory A Page number of secondary directory A Page number of primary directory B Page number of secondary directory BCopy the code

In PdgCntEditor, directories are graded according to the indentation (Tab), so the level 1 directory does not need to be indent, the level 2 directory should have an indent at the beginning of the line, where the level 3 directory has been deleted, and if not, there should be two indents.

The page number should follow the directory name, separated by a Tab.

So all you need to do is add an indent to the secondary directory, command:%s/\v^\ze(\d+\.\d+)/\t/:

Then replace the string between the directory name and the page number with Tab, command:%s/\v(\s\.) +\s/\t/:

4. Page number relocation

This is a very common problem. The page number on the book catalog page is usually not equal to the actual PDF page number, but there is usually a fixed difference.

You can see that the Introduction page number in the table of contents is 1, but it’s actually 23:

The actual page number is 22 larger than the page number given in the table of contents, or the offset is +22.

Therefore, we add 22 to all the pages, command:%s/\d\+$/\=submatch(0)+22/:

Now we have a canonical catalog.

Import the directory into a PDF

Once the catalog is made, we open the FreePic2Pdf program and click “Change PDF” :

Then select “Get bookmarks from PDF”, this step is to get the interface file, even if the PDF itself has no bookmarks:

Then select “Bookmark PDF” and click on the number 2 in the picture to open PdgCntEditor for editing directory information:

Paste in the directory text generated above, save and close:

Finally click “Start” to complete the import of bookmarks directory:

The effect

Before adding a directory:

After adding a directory:

Related articles

  • Vim uses advanced: 10 register profiling
  • Vim uses advanced: custom highlighting grouping
  • Python Standard Library: Re module function parsing
  • Automata: DFA is converted to a regular expression
  • Git tutorial: Basic commands used in local operations
  • Linux command line: 26 examples of use of find

like