Creating coloured hyperlinks in an index in a PDF file

This article explains how to create coloured hyperlinks in an index in a PDF file, using Microsoft Word as the source document for the PDF file. The following screen shot shows an example from a hyperlinked index in a PDF file:

coloured hyperlinks in an index

We assume that:

Background

PDF files often contain hyperlinks, and often, these are highlighted in some way to indicate to readers that text is a clickable link. Typically, clickable text is coloured blue, and sometimes, it is underlined.

Many authors create PDF files using Word as the source document. Most Word-to-PDF converters create a hyperlink in the PDF file if a hyperlink exists in the Word document. Unfortunately, Word does not create hyperlinked cross-references in an index, so no PDF creation tool can directly generate a hyperlinked index.

A very slow way of creating hyperlinks in the PDF file would be to use the Link tool in Acrobat. A much faster way is to use the Sonar Bookends Activate plug-in for Acrobat. Amongst other things, this creates hyperlinks for page numbers in indexes in PDF files.

But how do we get the colour into the hyperlinks in the index in the PDF file? The plug-in does not change the colour of new hyperlinks, and it does not create visible rectangles for the hyperlinks. You could change the colour of the hyperlinks using the TouchUp Text tool or you could create visible rectangles using the Link tool, but this is not practical because it takes too long. (Possibly, the task could be automated using an Acrobat macro, but at TechScribe we do not know of one that is available in the public domain.)

Our approach is to:

  1. Use a macro in Word to change the colour of locators in the index
  2. Generate the PDF using Adobe Acrobat
  3. Use Sonar Bookends Activate to create the hyperlinks.

The following sections give details of how to do this.

Setting up Word to use the macro

In a Word index, the headings (top-level entries) have the paragraph style 'Index 1', subheadings have the style 'Index 2', and so on. When you run the macro, it applies a character style called 'cross-reference' to digits in the paragraphs that have the style 'Index 1', 'Index 2', or 'Index 3'.

The macro is simple, and sometimes applies the style 'cross-reference' to digits that should not be or will not be hyperlinks:

To set up Word

  1. Create a character style called 'cross-reference' in your Word document. Typically, make the font colour blue, and possibly, make the text underlined.
  2. Import the macro 'IndexLocatorColour' into Word.

IndexLocatorColour macro

Sub IndexLocatorColour()

'Formats all digits in an index with the style "cross-reference"
'The style must exist in the document.

Call formatdigit("Index 1")
Call formatdigit("Index 2")
Call formatdigit("Index 3")

MsgBox ("Applied style cross-reference to Index 1 thru Index 3" & vbCrLf & _
        "Manually remove format from digits that are not in locators!")

End Sub

Function formatdigit(StyleToFind As String) As Boolean

On Error GoTo ErrorHandler:

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(StyleToFind)
Selection.Find.Replacement.Style = ActiveDocument.Styles("cross-reference")
With Selection.Find
    .Text = "^#"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

ErrorHandler:
    If Str(Err.Number) <> 0 Then  ' error number 0 is correct termination.
        strErrorMsg = "Error # " & Str(Err.Number) & " was generated by " _
                & Err.Source & Chr(13) & Err.Description & vbCrLf & _
                 "(some error)."
        MsgBox strErrorMsg
    End If
End Function

Note. This is a rather crude macro; if you have a better one, let us know, and we will direct readers to your website.

Creating the hyperlinks

Ensure that you have set up Word before you proceed.

To create coloured hyperlinks in an index in a PDF file

  1. Create the index in Word.
  2. Optional, but recommended. Lock the index (CTRL+F11) so that you cannot accidentally update it. (If you update the index, you lose the formatting.)
  3. Run the IndexLocatorColour macro. It applies the character style 'cross-reference' to all digits in paragraphs that have the paragraph styles 'Index 1', 'Index 2' or 'Index 3'.
  4. Manually remove the style 'cross-reference' from digits that will not be hyperlinked (see the limitations mentioned previously). A quick method is to select the text and press CTRL+SPACEBAR.
  5. Create the PDF file from the Word document.
  6. Open the PDF file in Adobe Acrobat and use Sonar Bookends Activate to create hyperlinks for the index locators (page numbers).
RSS feed