Accessible Images on MailMerge (AltText)

Hi, I’m trying to do MailMerge that way so the result will be Accessible.
I’ve encounter a problem when working with Images - I need to be able to set AlternativeText for an image and ability to set “Decorative” check on image would be great too. So far I can’t find that option.

I know that Aspose is supporting AltText in some scenarios, I’ve check that I can insert an image with alt text:

DocumentBuilder builder = new DocumentBuilder(originalDoc);
var shape = builder.InsertImage(File.OpenRead("file.jpeg"));
shape.AlternativeText = "something";

and it’s using shape underneath. Now I’d like to do something similar when replacing an image on mail merge. I’ve tried this:

originalDoc.MailMerge.FieldMergingCallback = new HandleMergeImageField();
(...)
public class HandleMergeImageField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        if (e.FieldName == "Picture")
        {
            e.Shape = new Shape(e.Document, ShapeType.Image);
            e.Shape.ImageData.ImageBytes = File.ReadAllBytes("image.jpeg");
            e.Shape.AlternativeText = "something";
        }
    }
}

but that way I’m ending up with Shape in Word instead of Image. Can you help me with that?
Also, as I’ve mentioned in the begining, ability to set “Decorative” check would be very helpful (of course on other images, then the ones with alt text).
I’m using .NET 7 and Aspose.Words v23.3

@xxMateusz,

I made this sample for you. Hopefully will set you in the right way:

private void Logic()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Create Merge Fields.
    builder.InsertField(" MERGEFIELD Name ");
    builder.InsertParagraph();
    builder.InsertField(" MERGEFIELD Image:MyPicture ");
    builder.InsertField(" MERGEFIELD Image:MyCheckBox ");

    builder.Document.Save($"{PartialPath}_input.docx");

    doc = new Document($"{PartialPath}_input.docx");
    doc.MailMerge.FieldMergingCallback = new MailMergeFieldsMergingCallback();
    doc.MailMerge.Execute(
        new string[] { "Name", "MyPicture", "MyCheckBox" },
        new object[] { "John Doe", $"{PartialPath}_john.jpg", $"{PartialPath}_checkbox.png" }
    );

    doc.Save($"{PartialPath}_output.docx");            
}

public class MailMergeFieldsMergingCallback : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldName == "Name")
        {
            args.FieldValue = $"Name: {args.FieldValue}";
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        if (e.FieldName == "MyPicture" && e.FieldValue != null)
        {
            e.Shape = new Shape(e.Document, ShapeType.Image);
            e.Shape.ImageData.ImageBytes = File.ReadAllBytes(e.FieldValue.ToString());
            e.Shape.Height = 200;
            e.Shape.Width = 200;
            e.Shape.AlternativeText = "My first Alt";
            e.Shape.AllowOverlap = false;
        }

        if (e.FieldName == "MyCheckBox" && e.FieldValue != null)
        {
            e.Shape = new Shape(e.Document, ShapeType.Image);
            e.Shape.ImageData.ImageBytes = File.ReadAllBytes(e.FieldValue.ToString());
            e.Shape.Height = 20;
            e.Shape.Width = 20;
            e.Shape.AlternativeText = "Checked";
            e.Shape.AllowOverlap = false;                    
        }
    }
}

This is the output.
AddingImage_output.docx (52.5 KB)

It is a simple example to give you an idea so you can carry on with more complex business logic.

From accessibility standpoint, I don’t see any difference between my previous results and this one. When I opened the file that you’ve attach in Word and right click on tucan picture I can see attached menu (tucan_menu.jpg),

but what I to see is in second screen (proper_menu.jpg).

After saving to pdf alt text is not there too.

@xxMateusz The difference in context menu is because in the first case the image is inserted as VML and in the second case as DML. You can insert DML shape using Aspose.Words using DocumentBuilder. Please try modifying your code like this:

public void ImageFieldMerging(ImageFieldMergingArgs args)
{
    DocumentBuilder builder = new DocumentBuilder(args.Document);
    args.Shape = builder.InsertImage(args.FieldValue.ToString());
    args.Shape.AlternativeText = "My first Alt";
}

in.docx (12.3 KB)
out.docx (11.5 KB)

FYI @carlos.molina

2 Likes

Hmmm, it looks like that solved the issue, thanks!

1 Like