Image Alt Text on a new document

I am using Aspose (latest) PDF. I can make an image by using the Image class like so:

var img = new Image
{
FixHeight = _options.ImageLeftHeight,
FixWidth = _options.ImageLeftWidth,
ImageStream = new MemoryStream(_options.ImageLeft),
HorizontalAlignment = _options.AlignmentLeft,
};

But I cannot seem to find a way to set the Alternative Text for an image.
I can set a title which displays underneath the image, but not the actual hidden alt text. This means any third party tagging just has “Image” as the text.

I found in the docs some examples of setting alt text but this looked to be for existing PDF, iterating over and adding alt text using the XImage class which is a different scenario.

Is there a way to set or influence the alt text for an image as this is quite a big accessibility issue.

@R_C_D_T,

Thank you for reaching out. It appears you are utilizing the Aspose.PDF API. I will transfer your thread to the appropriate category, where a member of the Aspose.PDF team will review your requirements or issue and provide assistance soon.

@R_C_D_T
Since version 25.6 to XImage has added methods:

/// <summary>
/// <summary>
/// Returns a list of strings with Alternative Text for an XImage.
/// </summary>
/// <param name="page">The page where XImage is located.</param>
/// <returns>List of strings with Alternative Text for an XImage.</returns>
public List<string> GetAlternativeText(Page page)


/// <summary>
/// Sets alternative text for an XImage on the page.
/// </summary>
/// <param name="alternativeText">The alternative text to be specified.</param>
/// <param name="page">Page where XImage is located.</param>
/// <returns>True if alternativeText for XImage is set. False if alternativeText for XImage not set.
/// <remarks>
/// The method returns false in the following cases:
/// - The XImage is not found on the specified page.
/// - The XImage appears multiple times on the page with different structural elements, 
///   making it ambiguous which instance should receive the alternative text.
/// </remarks> 
public bool TrySetAlternativeText(string alternativeText, Page page)

example of usage:

using (var doc = new Document(dataDir + "YourDocument.pdf"))
{
    string newAltText1 = "Modified Alt text for Im0 in page 1";
    XImage xImage = doc.Pages[1].Resources.Images[1];
    bool result = = xImage.TrySetAlternativeText(newAltText1, doc.Pages[1]);
    if (result)
    {
       Console.WriteLine("Alt text set");
       List<string> altTexts = xImage.GetAlternativeText(doc.Pages[1]);
       Console.WriteLine("altTexts[0]");
    }
}

This is what I was alluding to really, it looks like you can only do this on existing or rendered PDFs as a post-process and not in-line when creating the PDF from scratch where the images are being inserted using the Image class?

@R_C_D_T
You insert the image and after insertion, for the existing image, apply the given code

Does the document have to have passed through the final “ProcessParagraphs” first?
If not, could I use the Image and then call pages.Last().Resources.Images.Last() each time I add an image?

Long term this should really be on the Image class not the xImage

@R_C_D_T

Yes. And besides that, you should call the Save() method for the document (for speed to MemoryStream)

How do you see the API and its use case?

Thank you for confirming.

Ideally, as Image inherits BaseParagraph which is used to insert into any Paragraphs array, it would be nice to be able to specify it (like you can with Title, Width etc) at the point of creation like.

var img = new Image
{
FixHeight = 100
FixWidth = 100,
ImageStream = new MemoryStream(xxxx),
AltText = "My Image",
};
``

@R_C_D_T
Thanks, this looks reasonable and convenient.
Created a task for the development team to implement this feature.