How to scale images in docx documents with an ONLYOFFICE macro

Originally published at: How to scale images in docx documents with an ONLYOFFICE macro | ONLYOFFICE Blog


Recently, we created a macro to scale images in our Presentation Editor. Continuing with our image modification macros, we will build a macro to easily modify images within documents. This simple macro will allow us to make adjustments like image resizing, cropping and more. Let’s see how to build this simple macro.

An ONLYOFFICE macro to modify images in documents

Building the macro

  const processImage = (images, width = 100, height = 100) => {
    images.forEach((element) => {

We start by defining the processImage() function, which handles the modification of images in the document. This function iterates over each image in the document using the forEach() method.

        element.SetSize(width * 36000, height * 36000);
        // Optional settings for flipping and wrapping
        //  element.SetVertFlip(true);
        //  element.SetHorFlip(true);
        //  element.SetWrappingStyle("inline");

Inside the forEach loop, we use the SetSize() method to resize each image to the desired width and height. You can adjust the width and height parameters to your preference, but if left unchanged, they default to 100 x 100 mm.

Note: The dimensions used in the SetSize() method are in English Metric Units (EMUs). The width and height you provide in millimeters are multiplied by 36,000 to convert them into EMUs. Passing in the values in (mm) without conversion will not work.

  const document = Api.GetDocument();
  const allImg = document.GetAllImages();

Next comes the main part of the macro. We start off by retrieving the document in the document variable and storing all the images in the document in the allImg array.

  if (!allImg || allImg.length === 0) {
    console.error(
      `No images found in your document. Add some images and try again.`
    );
    return;
  }

Next, we run an if loop to make sure that the allImg array exists, and is not empty. If either of the conditions is not satisfied, we stop the execution of the macro.

  processImage(allImg);

Lastly, we call the processImage() function (defined above), passing in the allImg array.

The full macro code

Here is the code for the entire macro:

(function () {
  const processImage = (images, width = 100, height = 100) => {
    images.forEach((element) => {
      try {
        // Resizes the image to the passed height and width. The *36000 converts to EMUs (Office Open XML unit).
        element.SetSize(width * 36000, height * 36000);
        // Optional settings for flipping and wrapping
        //  element.SetVertFlip(true);
        //  element.SetHorFlip(true);
        //  element.SetWrappingStyle("inline");   //other available wrapping styles : "square" | "tight" | "through" | "topAndBottom" | "behind" | "inFront"
      } catch (error) {
        console.error(
          `Could not tweak one or more images. Please check your document once.`
        );
      }
    });
  };

const document = Api.GetDocument();
const allImg = document.GetAllImages();

if (!allImg || allImg.length === 0) {
console.error(
No images found in your document. Add some images and try again.
);
return;
}

processImage(allImg);
})();

Now, let’s see how our macro performs.

That was it! A simple macro that lets you tweak images in your text documents. The ONLYOFFICE API is a powerful tool, capable of handling a variety of tasks and opening up endless possibilities for developing more macros and plugins. With this API, you can unlock the full power of ONLYOFFICE to enhance your productivity and streamline your workflows.

If you have any questions or innovative concepts, we encourage you to share them with us. We value your input and look forward to collaborating with you. Best of luck in your exploratory endeavors!

Useful links

Custom Functions for ONLYOFFICE Macros

ONLYOFFICE Text Document API

Sample Macros

ONLYOFFICE on GitHub

Other Macros