Free Support Forum - aspose.com

DocumentBuilder.InsertImage

Hi,
We are replacing bookmarks in text box’s inside a word document with images using the DocumentBuilder class’ InsertImage method. This inserts the image at 100% scale, while the expected behaviour is for the image to be constrained to the text box.

Is there an alternative solution to facilitate this, as manually resizing the image requires knowledge of the shape dimensions, internal margins etc which Aspose appears to only expose in a limited manner.

Hi Robert,

Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document("in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

Shape imgShape = new Shape(doc, ShapeType.Image);

using (Bitmap img = new Bitmap(@"d:\Desert.jpg"))

{

imgShape.ImageData.SetImage(img);

imgShape.Width = 150;

imgShape.Height = 150;

imgShape.WrapType = WrapType.Square;

imgShape.BehindText = true;

}

builder.MoveToBookmark("bm");

builder.InsertNode(imgShape);

doc.Save(MyDir + "Out.docx");