Inserting inline image does not cause surrounding lines to reflow to make room

We have code that searches through a paragraph for a run that is acting as a placeholder for an image. When we locate the run, we remove it and insert the image. We have found that while the image is inserted in the correct spot, the text around the image does not reflow to make room as it would if manually inserting an image with Word.

@mhimlin can you please attach an example of your expected output to be able to provide an accurate solution.

@mhimlin please check if this code fit with your requirements. I’m replacing a word with an image:

byte[] byteImage;
using (FileStream image = File.OpenRead("C:\\Temp\\img.png"))
{
    // You dont need this because your image is actually a byte array
    byteImage = ReadFully(image);
}

using (FileStream fsInput = File.OpenRead("C:\\Temp\\input.docx"))
{
    MemoryStream msInput = new MemoryStream(ReadFully(fsInput));

    Document doc = new Document(msInput, new Aspose.Words.Loading.LoadOptions { LoadFormat = LoadFormat.Docx });

    FindReplaceOptions opt = new FindReplaceOptions()
    {
        ReplacingCallback = new ReplaceWithImageHandler(byteImage),
        Direction = FindReplaceDirection.Backward,
    };

    doc.Range.Replace("replace", string.Empty, opt);

    doc.UpdatePageLayout();

    doc.Save("C:\\Temp\\output.docx", SaveFormat.Docx);
}
public class ReplaceWithImageHandler : IReplacingCallback
{
    private readonly byte[] _image;

    public ReplaceWithImageHandler(byte[] image)
    {
        _image = image;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
        builder.MoveTo((Run)args.MatchNode);
        Shape img = builder.InsertImage(_image);
        img.WrapType = WrapType.Square;
        img.HorizontalAlignment = HorizontalAlignment.Center;

        return ReplaceAction.Replace;
    }
}

img.png (5.3 KB)
input.docx (2.4 MB)
output.docx (173.3 KB)