Working with Images

Aspose.Words allows users to work with images in a very flexible way. In this article, you can explore only some of the possibilities of working with images.

How to Insert an Image

DocumentBuilder provides several overloads of the InsertImage method that allows you to insert an inline or floating image. If the image is an EMF or WMF metafile, it will be inserted into the document in metafile format. All other images will be stored in PNG format. The InsertImage method can use images from different sources:

  • From a file or URL by passing a String parameter InsertImage
  • From a stream by passing a Stream parameter InsertImage
  • From an Image object by passing an Image parameter InsertImage
  • From a byte array by passing a byte array parameter InsertImage

For each of the InsertImage methods, there are further overloads which allow you to insert an image with the following options:

  • Inline or floating at a specific position, for example, InsertImage
  • Percentage scale or custom size, for example, InsertImage; furthermore, the InsertImage method returns a Shape object that was just created and inserted so you can further modify properties of the Shape

How to Insert an Inline Image

Pass a single string representing a file that contains the image to InsertImage to insert the image into the document as an inline graphic. 

The following code example shows how to insert an inline image at the cursor position into a document:

How to Insert a Floating Image

The following code example shows how to insert a floating image from a file or URL at a specified position and size:

How to Extract Images from a Document

All images are stored inside Shape nodes in a Document. To extract all images or images having specific type from the document, follow these steps:

  • Use the GetChildNodes method to select all Shape nodes.
  • Iterate through resulting node collections.
  • Check the HasImage boolean property.
  • Extract image data using the ImageData property.
  • Save image data to a file.

The following code example shows how to extract images from a document and save them as files:

How to Insert Barcode on each Document Page

This example demonstrates you to add the same or different barcodes on all or specific pages of a Word document. There is no direct way to add barcodes on all pages of a document but you can use the MoveToSection, MoveToHeaderFooter and InsertImage methods to move to any section or headers/footers and insert the barcode images as you can see in the following code.

The following code example shows how to insert a barcode image on each page of a document:

Lock Aspect Ratio of Image

The aspect ratio of a geometric shape is the ratio of its sizes in different dimensions. You can lock the aspect ratio of the image using AspectRatioLocked. The default value of the shape’s aspect ratio depends on the ShapeType. It is true for ShapeType.Image and false for other shape types.

The following code example shows how to work with aspect ratio:

How to Get Actual Bounds of Shape in Points

If you want the actual bounding box of the shape as rendered on the page, you can achieve this by using the BoundsInPoints property.

The following code example shows how to use this property:

Crop Images

The cropping of an image usually refers to the removal of the unwanted outer parts of an image to help improve the framing. It is also used for the removal of some of the portions of an image to increase the focus on a particular area.

The following code example shows how to achieve this using Aspose.Words API:

Save Images as WMF

Aspose.Words provides functionality to save all the available images in a document to WMFformat while converting DOCX to RTF.

The following code example shows how to save images as WMF with RTF save options: