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

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