DocumentBuilderInsertImage Method (String)

Inserts an image from a file or URL into the document. The image is inserted inline and at 100% scale.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.5
Syntax
public Shape InsertImage(
	string fileName
)

Parameters

fileName
Type: SystemString
The file with the image. Can be any valid local or remote URI.

Return Value

Type: Shape
The image node that was just inserted.
Remarks

This overload will automatically download the image before inserting into the document if you specify a remote URI.

You can change the image size, location, positioning method and other settings using the Shape object returned by this method.

Examples
Shows how to inserts an image from a URL.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Image from local file: ");
builder.InsertImage(ImageDir + "Logo.jpg");
builder.Writeln();

builder.Write("Image from a URL: ");
builder.InsertImage(AsposeLogoUrl);
builder.Writeln();

doc.Save(ArtifactsDir + "Image.CreateFromUrl.docx");
Examples
Shows how to insert a floating image in the middle of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// By default, the image is inline
Shape shape = builder.InsertImage(ImageDir + "Logo.jpg");

// Make the image float, put it behind text and center on the page
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;

doc.Save(ArtifactsDir + "Image.CreateFloatingPageCenter.docx");
Examples
Shows different solutions of how to import an image into a document from a string.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("\nInserted image from string: ");
builder.InsertImage(ImageDir + "Logo.jpg");

builder.Writeln("\nInserted image from string with a custom size: ");
builder.InsertImage(ImageDir + "Transparent background logo.png", ConvertUtil.PixelToPoint(250),
    ConvertUtil.PixelToPoint(144));

builder.Writeln("\nInserted image from string using relative positions: ");
builder.InsertImage(ImageDir + "Windows Metafile.wmf", RelativeHorizontalPosition.Margin, 100, 
    RelativeVerticalPosition.Margin, 100, 200, 100, WrapType.Square);

doc.Save(ArtifactsDir + "DocumentBuilderImages.InsertImageFromString.docx");
See Also