How to add alternative text for an image in Tagged PDF?

Tags: PDF/UAtagged pdfimagesiText 5

I know that iText can generate tagged PDF documents from scratch, but is it possible to insert alternative text for images in an existing tagged PDF without changing anything else? I need to implement this feature in a program without using GUI applications such as Adobe Acrobat Pro.

Posted on StackOverflow on Dec 2, 2015 by tsforsure

Please take a look at the AddAltTags example.

In this example, we take a PDF with images of a fox and a dog where the Alt keys are missing: no_alt_attribute.pdf

Structure element without /Alt key
Structure element without /Alt key

Code can't recognize a fox or a dog, so we create a new document with Alt attributes saying "Figure without an Alt description": added_alt_attributes.pdf)

Structure element with /Alt key
Structure element with /Alt key

We add this description by walking through the structure tree, looking for structural elements marked as /Figure elements:

  1. public void manipulatePdf(String src, String dest)
  2. throws IOException, DocumentException {
  3. PdfReader reader = new PdfReader(src);
  4. PdfDictionary catalog = reader.getCatalog();
  5. PdfDictionary structTreeRoot =
  6. catalog.getAsDict(PdfName.STRUCTTREEROOT);
  7. manipulate(structTreeRoot);
  8. PdfStamper stamper = new PdfStamper(
  9. reader, new FileOutputStream(dest));
  10. stamper.close();
  11. }
  12.  
  13. public void manipulate(PdfDictionary element) {
  14. if (element == null)
  15. return;
  16. if (PdfName.FIGURE.equals(element.get(PdfName.S))) {
  17. element.put(PdfName.ALT,
  18. new PdfString("Figure without an Alt description"));
  19. }
  20. PdfArray kids = element.getAsArray(PdfName.K);
  21. if (kids == null) return;
  22. for (int i = 0; i < kids.size(); i++)
  23. manipulate(kids.getAsDict(i));
  24. }

You can easily port this Java example to C#:

  • Get the root dictionary from the PdfReader object,

  • Get the root of the structure tree (a dictionary),

  • Loop over all the kids of every branch of that tree,

  • When a lead is a figure, add an /Alt entry.

Once this is done, use PdfStamper to save the altered file.