soapergem
soapergem

Reputation: 9989

Is there a way to add "alt text" to links in PDFs in Adobe Acrobat?

In Adobe Acrobat Pro, it's not that difficult to add links to a page, but I'm wondering if there's also a way to add "alt text" (sometimes called "title text") to links as well. In HTML, this would be done as such:

<a href="url" title="Text goes here">link</a>

Then when the mouse is hovering over the link, the text appears as a little tooltip. Is there an equivalent for PDFs? And if so, how do you add it?

Upvotes: 1

Views: 4354

Answers (7)

Foxlab
Foxlab

Reputation: 932

Facing the same problem I used the JS lib "pdf-lib" (https://pdf-lib.js.org/docs/api/classes/pdfdocument) to edit the content of the pdf file and add the missing attributes on annotations.

    const pdfLib = require('pdf-lib');
    const fs = require('fs');

    function getNewMap(pdfDoc, str){
      return pdfDoc.context.obj(
       {
        Alt: new pdfLib.PDFString(str),
        Contents: new pdfLib.PDFString(str)
      }).dict;
    }

    const pdfData = await fs.readFile('your-pdf-document.pdf');
    const pdfDoc = await pdfLib.PDFDocument.load(pdfData);
    pdfDoc.context.enumerateIndirectObjects().forEach(_o => {
      const pdfRef = _o[0];
      const pdfObject = _o[1];
      if (typeof pdfObject?.lookup === "function"){
        if (pdfObject.lookup(pdfLib.PDFName.of('Type'))?.encodedName === "/Annot"){
          // We use the link URI to implement annotation "Alt" & "Contents" attributes
          const annotLinkUri = pdfObject.lookup(pdfLib.PDFName.of('A')).get(pdfLib.PDFName.of('URI')).value;
          const titleFromUri = annotLinkUri.replace(/(http:|)(^|\/\/)(.*?\/)/g, "").replace(/\//g, "").trim();
          // We build the new map with "Alt" and "Contents" attributes and assign it to the "Annot" object dictionary
          const newMap = getNewMap(pdfDoc, titleFromUri);
          const newdict = new Map([...pdfObject.dict, ...newMap]);
          pdfObject.dict = newdict;
        }
      }
    })

    // We save the file
    const pdfBytes = await pdfDoc.save();
    await fs.promises.writeFile("your-pdf-document-accessible.pdf", pdfBytes);

Upvotes: 0

Lucas Cimon
Lucas Cimon

Reputation: 2043

In PDF syntax, Link annotations support a Contents entry to serve as an alternate description:

/Annots [
  <<
    /Type /Annot
    /Subtype /Link
    /Border [1 1 1]
    /Dest [4 0 R /XYZ 0 0 null]
    /Rect [ 50 50 80 60 ]
    /Contents (Link)
  >>
]

Quoting "PDF Reference - 6th edition - Adobe® Portable Document Format - Version 1.7 - November 2006" :

Contents text string (Optional) Text to be displayed for the annotation or, if this type of annotation does not display text, an alternate description of the annotation’s contents in human-readable form. In either case, this text is useful when extracting the document’s contents in support of accessibility to users with disabilities or for other purposes

And later on:

For all other annotation types (Link , Movie , Widget , PrinterMark , and TrapNet), the Contents entry provides an alternate representation of the annotation’s contents in human-readable form

This is displayed well with Sumatra PDF v3.1.2, when a border is present: enter image description here

However this is not widely supported by other PDF readers.

The W3C, in its PDF Techniques for WCAG 2.0 recommend another ways to display alternative texts on links for accessibility purposes:

Upvotes: 1

Olaf Dr&#252;mmer
Olaf Dr&#252;mmer

Reputation: 76

Officially, per PDF 1.7 as defined in ISO 32000-1 14.9.3 (see Adobe website for a free download of a document that is equivaent to the ISO standard for PDF 1.7), one would provide alternate text for an annotation - like for example a Link annotation - by adding a key "Contents" to its data structure and provide the alt text as a text string value for that key.

Unfortunately Acrobat does not seem to provide a user interface to add or edit this "Contents" text string for Link annotations, and even if it is present it will not be used for the tool tip. Instead the tool tip always seems to be the target of the Link annotation, at least if it points to a URL.

So on a visual level one could hack around this by adding some other invisible elements on top of the area of the Link annotation that have the desired behavior. Not a very nice hack, at least for my taste. In addition it does not help with accessibility of the PDF, as it introduces yet another stray object...

Upvotes: 0

John K.
John K.

Reputation: 1

There is also a trick using an invisible form button that doesn't do anything but allows a small popup tooltip text to be added when the mouse hovers over it.

Upvotes: 0

Dwight Kelly
Dwight Kelly

Reputation: 1238

Actually PDF does support alternate text. It's part of Logical Structure documented PDF Reference 1.7 section 10.8.2 "Alternate Descriptions"

/Span << /Lang (en-us) /Alt (six-point star) >> BDC (✡) Tj EMC

Upvotes: 2

austin cheney
austin cheney

Reputation:

Alt text, or alternate text, is not the same as title text. Title text is meta data intended for human consumption. Alt text is alternate text content upon media in case the media fails to load.

Upvotes: 0

Rowan
Rowan

Reputation: 2430

No, it's not possible to add alt text to a link in a PDF. There's no provision for this in the PDF reference.

On a related note, links in PDFs and links in HTML documents are handled quite differently. A link in a PDF is actually a type of annotation, which sits on top of the page at specified co-ordinates, and has no technical relationship to the text or image in the document. Where as links in HTML documents bare a direct relationpship to the elements which they hyperlink.

Upvotes: 0

Related Questions