insertImage method

insertImage(image)

insertImage(image: Aspose.Words.JSImage)
ParameterTypeDescription
imageJSImage

insertImage(image, width, height)

insertImage(image: Aspose.Words.JSImage, width: number, height: number)
ParameterTypeDescription
imageJSImage
widthnumber
heightnumber

insertImage(image, horzPos, left, vertPos, top, width, height, wrapType)

insertImage(image: Aspose.Words.JSImage, horzPos: Aspose.Words.Drawing.RelativeHorizontalPosition, left: number, vertPos: Aspose.Words.Drawing.RelativeVerticalPosition, top: number, width: number, height: number, wrapType: Aspose.Words.Drawing.WrapType)
ParameterTypeDescription
imageJSImage
horzPosRelativeHorizontalPosition
leftnumber
vertPosRelativeVerticalPosition
topnumber
widthnumber
heightnumber
wrapTypeWrapType

insertImage(fileName)

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

insertImage(fileName: string)
ParameterTypeDescription
fileNamestringThe file with the image. Can be any valid local or remote URI.

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.

Returns

The image node that was just inserted.

insertImage(stream)

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

insertImage(stream: Buffer)
ParameterTypeDescription
streamBufferThe stream that contains the image.

Remarks

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

Returns

The image node that was just inserted.

insertImage(imageBytes) {#number[]}

Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.

insertImage(imageBytes: number[])
ParameterTypeDescription
imageBytesnumber[]The byte array that contains the image.

Remarks

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

Returns

The image node that was just inserted.

insertImage(fileName, width, height)

Inserts an inline image from a file or URL into the document and scales it to the specified size.

insertImage(fileName: string, width: number, height: number)
ParameterTypeDescription
fileNamestringThe file that contains the image.
widthnumberThe width of the image in points. Can be a negative or zero value to request 100% scale.
heightnumberThe height of the image in points. Can be a negative or zero value to request 100% scale.

Remarks

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

Returns

The image node that was just inserted.

insertImage(stream, width, height)

Inserts an inline image from a stream into the document and scales it to the specified size.

insertImage(stream: Buffer, width: number, height: number)
ParameterTypeDescription
streamBufferThe stream that contains the image.
widthnumberThe width of the image in points. Can be a negative or zero value to request 100% scale.
heightnumberThe height of the image in points. Can be a negative or zero value to request 100% scale.

Remarks

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

Returns

The image node that was just inserted.

insertImage(imageBytes, width, height) {#number[]_number_number}

Inserts an inline image from a byte array into the document and scales it to the specified size.

insertImage(imageBytes: number[], width: number, height: number)
ParameterTypeDescription
imageBytesnumber[]The byte array that contains the image.
widthnumberThe width of the image in points. Can be a negative or zero value to request 100% scale.
heightnumberThe height of the image in points. Can be a negative or zero value to request 100% scale.

Remarks

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

Returns

The image node that was just inserted.

insertImage(fileName, horzPos, left, vertPos, top, width, height, wrapType)

Inserts an image from a file or URL at the specified position and size.

insertImage(fileName: string, horzPos: Aspose.Words.Drawing.RelativeHorizontalPosition, left: number, vertPos: Aspose.Words.Drawing.RelativeVerticalPosition, top: number, width: number, height: number, wrapType: Aspose.Words.Drawing.WrapType)
ParameterTypeDescription
fileNamestringThe file that contains the image.
horzPosRelativeHorizontalPositionSpecifies where the distance to the image is measured from.
leftnumberDistance in points from the origin to the left side of the image.
vertPosRelativeVerticalPositionSpecifies where the distance to the image measured from.
topnumberDistance in points from the origin to the top side of the image.
widthnumberThe width of the image in points. Can be a negative or zero value to request 100% scale.
heightnumberThe height of the image in points. Can be a negative or zero value to request 100% scale.
wrapTypeWrapTypeSpecifies how to wrap text around the image.

Remarks

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

Returns

The image node that was just inserted.

insertImage(stream, horzPos, left, vertPos, top, width, height, wrapType)

Inserts an image from a stream at the specified position and size.

insertImage(stream: Buffer, horzPos: Aspose.Words.Drawing.RelativeHorizontalPosition, left: number, vertPos: Aspose.Words.Drawing.RelativeVerticalPosition, top: number, width: number, height: number, wrapType: Aspose.Words.Drawing.WrapType)
ParameterTypeDescription
streamBufferThe stream that contains the image.
horzPosRelativeHorizontalPosition
leftnumber
vertPosRelativeVerticalPosition
topnumber
widthnumber
heightnumber
wrapTypeWrapType

Remarks

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

insertImage(imageBytes, horzPos, left, vertPos, top, width, height, wrapType) {#number[]_relativehorizontalposition_number_relativeverticalposition_number_number_number_wraptype}

Inserts an image from a byte array at the specified position and size.

insertImage(imageBytes: number[], horzPos: Aspose.Words.Drawing.RelativeHorizontalPosition, left: number, vertPos: Aspose.Words.Drawing.RelativeVerticalPosition, top: number, width: number, height: number, wrapType: Aspose.Words.Drawing.WrapType)
ParameterTypeDescription
imageBytesnumber[]The byte array that contains the image.
horzPosRelativeHorizontalPosition
leftnumber
vertPosRelativeVerticalPosition
topnumber
widthnumber
heightnumber
wrapTypeWrapType

Remarks

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

Examples

Shows how to insert an image from the local file system into a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

// Below are three ways of inserting an image from a local system filename.
// 1 -  Inline shape with a default size based on the image's original dimensions:
builder.insertImage(base.imageDir + "Logo.jpg");

builder.insertBreak(aw.BreakType.PageBreak);

// 2 -  Inline shape with custom dimensions:
builder.insertImage(base.imageDir + "Transparent background logo.png", aw.ConvertUtil.pixelToPoint(250), aw.ConvertUtil.pixelToPoint(144));

builder.insertBreak(aw.BreakType.PageBreak);

// 3 -  Floating shape with custom dimensions:
builder.insertImage(base.imageDir + "Windows MetaFile.wmf", aw.Drawing.RelativeHorizontalPosition.Margin, 100,  aw.Drawing.RelativeVerticalPosition.Margin, 100, 200, 100, aw.Drawing.WrapType.Square);

doc.save(base.artifactsDir + "DocumentBuilderImages.InsertImageFromFilename.docx");

Shows how to determine which image will be inserted.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.insertImage(base.imageDir + "Scalable Vector Graphics.svg");

// Aspose.words insert SVG image to the document as PNG with svgBlip extension
// that contains the original vector SVG image representation.
doc.save(base.artifactsDir + "DocumentBuilderImages.InsertSvgImage.SvgWithSvgBlip.docx");

// Aspose.words insert SVG image to the document as PNG, just like Microsoft Word does for old format.
doc.save(base.artifactsDir + "DocumentBuilderImages.InsertSvgImage.svg.doc");

doc.compatibilityOptions.optimizeFor(aw.Settings.MsWordVersion.Word2003);

// Aspose.words insert SVG image to the document as EMF metafile to keep the image in vector representation.
doc.save(base.artifactsDir + "DocumentBuilderImages.InsertSvgImage.emf.docx");

Shows how to insert gif image to the document.

let builder = new aw.DocumentBuilder();

// We can insert gif image using path or bytes array.
// It works only if DocumentBuilder optimized to Word version 2010 or higher.
// Note, that access to the image bytes causes conversion Gif to Png.
let gifImage = builder.insertImage(base.imageDir + "Graphics Interchange Format.gif");

gifImage = builder.insertImage(fs.readFileSync(base.imageDir + "Graphics Interchange Format.gif"));

builder.document.save(base.artifactsDir + "InsertGif.docx");

Shows how to insert a shape with an image into a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

// Below are two locations where the document builder's "InsertShape" method
// can source the image that the shape will display.
// 1 -  Pass a local file system filename of an image file:
builder.write("Image from local file: ");
builder.insertImage(base.imageDir + "Logo.jpg");
builder.writeln();

// 2 -  Pass a URL which points to an image.
builder.write("Image from a URL: ");
builder.insertImage(base.imageUrl.toString());
builder.writeln();

doc.save(base.artifactsDir + "Image.FromUrl.docx");

Shows how to insert a floating image to the center of a page.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
let shape = builder.insertImage(base.imageDir + "Logo.jpg");
shape.wrapType = aw.Drawing.WrapType.None;
shape.behindText = true;
shape.relativeHorizontalPosition = aw.Drawing.RelativeHorizontalPosition.Page;
shape.relativeVerticalPosition = aw.Drawing.RelativeVerticalPosition.Page;
shape.horizontalAlignment = aw.Drawing.HorizontalAlignment.Center;
shape.verticalAlignment = aw.Drawing.VerticalAlignment.Center;

doc.save(base.artifactsDir + "Image.CreateFloatingPageCenter.docx");

Shows how to insert WebP image.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.insertImage(base.imageDir + "WebP image.webp");

doc.save(base.artifactsDir + "Image.InsertWebpImage.docx");

Shows how to insert an image from a stream into a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

let stream = fs.readFileSync(base.imageDir + "Logo.jpg");
// Below are three ways of inserting an image from a stream.
// 1 -  Inline shape with a default size based on the image's original dimensions:
builder.insertImage(stream);

builder.insertBreak(aw.BreakType.PageBreak);

// 2 -  Inline shape with custom dimensions:
builder.insertImage(stream, aw.ConvertUtil.pixelToPoint(250), aw.ConvertUtil.pixelToPoint(144));

builder.insertBreak(aw.BreakType.PageBreak);

// 3 -  Floating shape with custom dimensions:
builder.insertImage(stream, aw.Drawing.RelativeHorizontalPosition.Margin, 100, aw.Drawing.RelativeVerticalPosition.Margin, 100, 200, 100, aw.Drawing.WrapType.Square);

doc.save(base.artifactsDir + "DocumentBuilderImages.InsertImageFromStream.docx");

Shows how to insert a shape with an image from a stream into a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

let stream = fs.createReadStream(base.imageDir + "Logo.jpg")
builder.write("Image from stream: ");
builder.insertImage(stream);

doc.save(base.artifactsDir + "Image.FromStream.docx");

Shows how to insert an image.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

// There are two ways of using a document builder to source an image and then insert it as a floating shape.
// 1 -  From a file in the local file system:
builder.insertImage(base.imageDir + "Transparent background logo.png", aw.Drawing.RelativeHorizontalPosition.Margin, 100,
  aw.Drawing.RelativeVerticalPosition.Margin, 0, 200, 200, aw.Drawing.WrapType.Square);

// 2 -  From a URL:
builder.insertImage(base.imageUrl.toString(), aw.Drawing.RelativeHorizontalPosition.Margin, 100,
  aw.Drawing.RelativeVerticalPosition.Margin, 250, 200, 200, aw.Drawing.WrapType.Square);

doc.save(base.artifactsDir + "DocumentBuilder.InsertFloatingImage.docx");

Shows how to insert an image from the local file system into a document while preserving its dimensions.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

// The InsertImage method creates a floating shape with the passed image in its image data.
// We can specify the dimensions of the shape can be passing them to this method.
let imageShape = builder.insertImage(base.imageDir + "Logo.jpg", aw.Drawing.RelativeHorizontalPosition.Margin, 0,
  aw.Drawing.RelativeVerticalPosition.Margin, 0, -1, -1, aw.Drawing.WrapType.Square);

// Passing negative values as the intended dimensions will automatically define
// the shape's dimensions based on the dimensions of its image.
expect(imageShape.width).toEqual(300.0);
expect(imageShape.height).toEqual(300.0);

doc.save(base.artifactsDir + "DocumentBuilder.InsertImageOriginalSize.docx");

See Also