Resizing and Centering Images in Google Docs with Google Apps Script

In today’s blog post, we’ll explore a useful Google Apps Script function that helps automate a common task: resizing and centering images in a Google Doc. This script ensures that all images are resized to 75% of the page width and are centered for a cleaner, more professional look.

The Script

Below is the complete Google Apps Script that performs the resizing and centering of images within a Google Doc. We’ll break down the script step-by-step to understand how it works.

function resizeAndCenterImages() {
  // Open the document by ID. Replace with your document ID.
  var docId = 'YOUR_DOCUMENT_ID_HERE';
  var doc = DocumentApp.openById(docId);
  var body = doc.getBody();
  
  // Get the page width in points
  var pageWidth = doc.getPageWidth();
  var maxImageWidth = pageWidth * 0.75;

  // Get all the elements in the document
  var totalElements = body.getNumChildren();
  
  for (var i = 0; i < totalElements; i++) {
    var element = body.getChild(i);
    
    if (element.getType() == DocumentApp.ElementType.PARAGRAPH) {
      var paragraph = element.asParagraph();
      var numChildren = paragraph.getNumChildren();
      
      for (var j = 0; j < numChildren; j++) {
        var child = paragraph.getChild(j);
        
        if (child.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
          var image = child.asInlineImage();
          var originalWidth = image.getWidth();
          var originalHeight = image.getHeight();
          
          // Calculate the new dimensions maintaining the aspect ratio
          var newWidth = maxImageWidth;
          var newHeight = (newWidth / originalWidth) * originalHeight;
          
          // Resize the image
          image.setWidth(newWidth);
          image.setHeight(newHeight);
          
          // Center the image by wrapping it in a centered paragraph
          paragraph.removeChild(image);
          var centeredParagraph = body.insertParagraph(i + 1, "");
          centeredParagraph.appendInlineImage(image);
          centeredParagraph.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
          i++;  // Adjust index to account for newly inserted paragraph
          break;  // Exit inner loop after processing the image
        }
      }
    }
  }
}

Script Breakdown
Opening the Document:

var docId = ‘YOUR_DOCUMENT_ID_HERE’;
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
Replace ‘YOUR_DOCUMENT_ID_HERE’ with your actual Google Doc ID. This part of the script opens the specified Google Doc and retrieves the document body.

Calculating Maximum Image Width:

var pageWidth = doc.getPageWidth();
var maxImageWidth = pageWidth * 0.75;
This calculates 75% of the page width to determine the maximum allowable width for images.

Iterating Through Document Elements:

var totalElements = body.getNumChildren();

for (var i = 0; i < totalElements; i++) {
var element = body.getChild(i);
The script retrieves the number of elements in the document and iterates through them.

Checking for Paragraphs and Images:

if (element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var paragraph = element.asParagraph();
var numChildren = paragraph.getNumChildren();

for (var j = 0; j < numChildren; j++) {
var child = paragraph.getChild(j);

if (child.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var image = child.asInlineImage();

The script checks if each element is a paragraph and then iterates through the children of the paragraph to find images.

Resizing and Centering the Image:

var originalWidth = image.getWidth();
var originalHeight = image.getHeight();

var newWidth = maxImageWidth;
var newHeight = (newWidth / originalWidth) * originalHeight;

image.setWidth(newWidth);
image.setHeight(newHeight);

paragraph.removeChild(image);
var centeredParagraph = body.insertParagraph(i + 1, “”);
centeredParagraph.appendInlineImage(image);
centeredParagraph.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
i++;
break;
Once an image is found, the script calculates the new dimensions while maintaining the aspect ratio, resizes the image, and centers it in a new paragraph.

How to Use the Script
Open Google Docs and create a new Google Apps Script project:

In your Google Doc, click on Extensions > Apps Script.
Copy and Paste the Script:

Replace the placeholder ‘YOUR_DOCUMENT_ID_HERE’ with your Google Doc ID. Save the script.
Run the Script:

Click the Run button in the Apps Script editor. Authorize the script if prompted.
Conclusion
This Google Apps Script is a powerful tool for anyone looking to streamline their document formatting process. By automating the resizing and centering of images, you can save time and ensure a consistent, polished look in your Google Docs.