Search code examples
itext7

Add AltText to image Itext 7 version9


How to add AltText to an image using Itext 7 version 9. I found this code snippet on the NET but the SetAltText method does not exist in the version I'm using.

 Document document = new Document(pdfDoc);
        
 // Add an image with alt text
 Image img = new Image(ImageDataFactory.Create("path/to/image.png"))
            .SetRole(StandardRoles.FIGURE)
            .SetAltText("Description of the image for accessibility");
        
  document.Add(img);


Solution

  • Your code should be something like:

    Document document = new Document(pdfDoc);
    
    iText.Layout.Element.Image img = new Image(ImageDataFactory
      .Create("path/to/image.png"));
            
    img.GetAccessibilityProperties()
      .SetAlternateDescription("Description of the image for accessibility");
        
    document.Add(img);
    

    you can find an example on the official GitHub repo, and here is the API entry.