How to scale images in presentations with an ONLYOFFICE macro
While working with images in presentations, there are scenarios where manually adjusting the size of each image can become time-consuming, especially in image-heavy presentations. With this macro, you can effortlessly resize all the images in your presentation. Let’s dive in and see how you can build this simple macro and improve your presentation workflow.
Building the macro
const resizeImg = (images, width, height, slideIndex) => {
const finalWdth = width * 36000; //Convert mm to EMUs
const finalHgt = height * 36000; //Convert mm to EMUs
images.forEach((element) => {
try {
element.SetSize(finalWdth, finalHgt);
} catch (error) {
console.error(
`Could not change size for one or more images in slide no. ${slideIndex}. Check your presentation once.`
);
}
});
};
We start by defining a function resizeImg(), which is responsible for resizing the images. The function takes four parameters: an array of images, the desired width, the desired height, and the index of the current slide.
The entered width and height are converted from millimeters to EMUs (English Metric Units), which are then passed as parameters to the SetSize() method. The SetSize() method sets the size of each image to the entered width and height.
const oPresentation = Api.GetPresentation();
const slidesCount = oPresentation.GetSlidesCount();
Next comes the main part of the macro. We start off by retrieving the active presentation using the GetPresentation() method (stored in oPresentation variable) and then the slides count using the GetSlidesCount() method (stored in the slidesCount variable).
const width = 100; //in mm (modify this value)
const height = 100; //in mm (modify this value)
Then, we set the desired width and height for the images in millimeters(mm). You can modify these values according to your needs.
if (slidesCount === 0) {
console.warn("You dont seem to have any slides in your presentation.");
return;
}
If the presentation has no slides, we log a warning and stop the execution of the macro.
for (let i = 0; i < slidesCount; i++) {
const oSlide = oPresentation.GetSlideByIndex(i);
const slideImgs = oSlide.GetAllImages();
Next, we use a for loop to iterate through all the slides. For each iteration, we retrieve all the images in the slide using the GetAllImages() method.
if (slideImgs.length === 0) {
console.warn(`No images found on slide ${i + 1}`);
continue; //continue to next slide
}
If the slide has no images, we log a warning and move on to the next slide.
resizeImg(slideImgs, width, height, i + 1);
Once we ensure that images are indeed present on the slide, the resizeImg() method is called. We pass in the array of all the images on the slide, followed by the height, width, and the current slide’s index.
The full macro code
Here is the code for the entire macro:
(function () {
const resizeImg = (images, width, height, slideIndex) => {
const finalWdth = width * 36000; //Convert mm to EMUs
const finalHgt = height * 36000; //Convert mm to EMUs
images.forEach((element) => {
try {
element.SetSize(finalWdth, finalHgt);
} catch (error) {
console.error(
`Could not change size for one or more images in slide no. ${slideIndex}. Check your presentation once.`
);
}
});
};
try {
const oPresentation = Api.GetPresentation();
const slidesCount = oPresentation.GetSlidesCount();
const width = 100; //in mm (modify this value)
const height = 100; //in mm (modify this value)
if (slidesCount === 0) {
console.warn("You don't seem to have any slides in your presentation.");
return;
}
for (let i = 0; i < slidesCount; i++) {
const oSlide = oPresentation.GetSlideByIndex(i);
const slideImgs = oSlide.GetAllImages();
if (slideImgs.length === 0) {
console.warn(`No images found on slide ${i + 1}`);
continue; //continue to next slide
}
resizeImg(slideImgs, width, height, i + 1);
}
} catch (error) {
console.error("An unexpected error occurred while running the macro", error);
}
})();
Now, let us see how our macro performs.
That was it! A simple macro that lets you resize all the images in your presentation just with a click of a button. 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
Create your free ONLYOFFICE account
View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.