How to Resize Image in Word using Java

This quick tutorial guides on how to resize image in Word using Java. It contains information about the environment setting, a step-by-step process to write the application, and a runnable sample code to change picture size in Word using Java. You will get an introduction to the important classes, methods, and properties required for writing this application and then saving the Word document having resized images as DOCX, DOC, or any other desired format.

Steps to Resize Photo in Word using Java

  1. Set the IDE to use Aspose.Words for Java to add resized images
  2. Create or load a Word file using the Document class object to add image with custom size
  3. Instantiate a DocumentBuilder class object using the Document object
  4. Write some optional text using the write() method
  5. Insert an image using the insertImage() method and get its reference to change its size
  6. Resize the image by setting its width and height and save the document

These steps explain how to resize a picture in Word using Java by sharing the environment setting and then creating a Word file with the help of the Document class object. The DocumentBuilder class is used for adding the optional text with write() method and inserting an image using the insertImage() method by providing the image file name and path. A Shape class object is returned by the insertImage() that is used to set the width and height using the setWidth() and setHeight() methods.

Code to Resize Image in MS Word using Java

import com.aspose.words.ConvertUtil;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.Shape;
public class AsposeTest {
public static void main(String[] args) throws Exception {//Main function to add resized image to a Word file using Java
// Instantiate the license
License lic = new License();
lic.setLicense("Aspose.Total.lic");
// Instantiate a new Document object
Document wordDoc = new Document();
// Create a DocumentBuilder and initialize it with the Document class object
DocumentBuilder documentBuilder = new DocumentBuilder(wordDoc);
// Write some text for reference to the document before inserting an image
documentBuilder.write("Here is the image with its original size");
// Insert an image with its original size
Shape image = documentBuilder.insertImage("sampleImage.jpg");
// Write some sample text before the image that will be resized
documentBuilder.write("Following image is resized");
// Insert another image and get its reference to change size
image = documentBuilder.insertImage("sampleImage.jpg");
// Set image properties width and height
image.setWidth(ConvertUtil.inchToPoint(0.60));
image.setHeight(ConvertUtil.inchToPoint(0.60));
// Save the document with a resized image in it
wordDoc.save("FileWithResizedImages.docx");
System.out.println("Done");
}
}

This sample code demonstrates the process to resize picture in Word using Java. It uses the Shape class object to set size with the help of setWidth() and setHeight() methods by providing the desired parameters in inches that are converted to points using the utility class ConvertUtil. You can set other properties of the image as well by using the methods setBehindText() to place the image behind the text, setBounds() to define the placement, and setHorizontalAlignment() and setVerticalAlignment() to set the shape alignment.

This tutorial has guided us to resize image in Word using Java. If you want to learn the process to add a watermark in a Word document, refer to the article on how to add watermark to Word using Java.

 English