Search code examples
c#mailmergeaspose.words

Insert text if image doesn't exist Aspose Mail Merge


I'm working on inserting images and here is my code:

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
    if (this.mBuilder == null)
        this.mBuilder = new DocumentBuilder(e.Document);

    string filename = GetFilePath();
    if (!File.Exists(filename))
    {
        //todo insert text "<Image not found>" instead image
        this.mBuilder.MoveToField(e.Field, false);
        this.mBuilder.Write("<Image not found>");
        e.ImageFileName = string.Empty;
        return;
    }

    e.ImageFileName = filename;
}

And I want to insert text "<Image not found>" if image doesn't exist. But this code throws error Cannot load image from field '...'. The field contains data in unsupported format. Could not find file.

What should I insert to fix my bug?


Solution

  • This solve my problem. I just need to remove field into my if statement:

    this.mBuilder.MoveToField(e.Field, false);
    this.mBuilder.Write("<Image not found>");
    e.Field.Remove();
    return;