How to Resize All Images in Google Docs to 50% Page Width (Using Apps Script)

https://github.com/lsvekis/Apps-Script-Code-Snippets

If you’ve ever worked on a long Google Doc with lots of images, you know the pain:
some images are too large, some too small, and manually resizing each one is tedious and inconsistent.

The good news? Google Apps Script can handle this automatically.

In this post, you’ll learn how to use a simple Apps Script to resize every image in a Google Doc to exactly 50% of the document’s usable width, based on the page layout and margins.


Why Resize Images Programmatically?

Manual resizing causes several common problems:

  • Images don’t align consistently across pages
  • Margins are ignored when resizing by hand
  • Updates take forever in long documents
  • Different contributors use different sizes

By using Apps Script, you get:

  • ✅ Consistent image sizing
  • ✅ Automatic respect for page margins
  • ✅ Aspect ratios preserved
  • ✅ One-click execution

What This Script Does

This script:

  1. Reads the document’s page width
  2. Subtracts the left and right margins
  3. Calculates 50% of the usable width
  4. Loops through all paragraphs
  5. Finds every inline image
  6. Resizes each image proportionally

⚠️ Important: Google Docs only allows scripts to resize inline images, not floating or drawing-based images.


The Apps Script Code

Copy and paste the following code into your Apps Script editor:

function resizeImagesToHalfPageWidth() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();

  // Get page size and margins (points)
  const pageWidth = body.getPageWidth();
  const marginLeft = body.getMarginLeft();
  const marginRight = body.getMarginRight();

  // Usable content width
  const usableWidth = pageWidth - marginLeft - marginRight;

  // Target width = 50%
  const targetWidth = usableWidth * 0.5;

  let imageCount = 0;

  // Walk through all elements
  const numChildren = body.getNumChildren();
  for (let i = 0; i < numChildren; i++) {
    const element = body.getChild(i);

    if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
      const paragraph = element.asParagraph();
      const numElements = paragraph.getNumChildren();

      for (let j = 0; j < numElements; j++) {
        const child = paragraph.getChild(j);

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

          // Maintain aspect ratio
          const originalWidth = image.getWidth();
          const originalHeight = image.getHeight();
          const scale = targetWidth / originalWidth;

          image.setWidth(targetWidth);
          image.setHeight(originalHeight * scale);

          imageCount++;
        }
      }
    }
  }

  Logger.log(`Resized ${imageCount} images to 50% page width.`);
}

How to Run the Script

  1. Open your Google Doc
  2. Click Extensions → Apps Script
  3. Paste the code above
  4. Save the project
  5. Run resizeImagesToHalfPageWidth
  6. Approve permissions when prompted

That’s it. Every inline image in your document is now resized consistently.


Common Questions

Does this affect image quality?

No. The script only changes display size, not the underlying image file.

Will this resize drawings or floating images?

No. Google Docs scripting only supports inline images.

Can I change the width percentage?

Absolutely. Just change this line:

const targetWidth = usableWidth * 0.5;

For example:

  • 0.6 → 60% width
  • 1.0 → full-width images

Why This Matters for Automation

This pattern is useful beyond image resizing:

  • Formatting cleanup before publishing
  • Enforcing brand/layout standards
  • Preparing documents for export to PDF
  • Automating editorial workflows

Once you start using Apps Script this way, Google Docs becomes a programmable editor, not just a word processor.