Frequently Asked Question:
Create internal links from the PDF TOC to the start of the relevant chapter
I need to create internal links from the PDF TOC (it's a book) to the page where the chapter starts. I'm assuming that would mean extracting each text object on a page, and replacing it with an internal book mark, using the same text, font, etc.
Any tips?
Adding links from the TOC to individual chapter pages is possible, but there isn't a single function for this task. I've included details on how to do this below.
In the PDF specification the text on the page is part of the page description commands while the link itself is just an invisible area over the text stored in an "annotation" object.
You could use the Quick PDF Library functions LoadFromFile, SelectPage and GetPageText(3) to obtain a list of text found on the page. This information is not directly available from the PDF, rather the page description is passed through our rendering engine and the co-ordinates, text and font information for each "draw text" command is listed.
The kind of information returned by the GetPageText(3) function depends a lot on how your PDF was generated. Different PDF producers have different ways of embedding fonts and encoding characters. Furthermore, some words might be split up into little pieces in some documents (due to font kerning) and in other PDFs the order of the text might not be in any particular or useful order.
But if the GetPageText(3) function does return useful information for your particular PDF you should be able to get the following for each piece of text:
- Vertical and horizontal co-ordinates of the four corners of the box bounding the text
- Font name
- Text color
- Text size
- Actual text
The font name and color are not needed for adding a link to the page (as mentioned previously the link is just an invisible "hotspot" rectangle) but the font name, size and color could help in identifying certain blocks of text.
Once the details have been collected the SelectPage and AddLinkToPage functions can be used to add the required links.
Finally SaveToFile can be called to output the modified PDF to a new file.