Manipulate PositionedImage and wrap text around image in Google Docs
Categories:
Manipulate PositionedImage and Wrap Text in Google Docs with Apps Script

Learn how to programmatically insert, position, and wrap text around images in Google Docs using Google Apps Script, enhancing document automation and layout control.
Google Docs offers powerful features for document creation, but automating complex layouts like text wrapping around images can be challenging. Google Apps Script provides the tools to programmatically control these aspects, allowing for dynamic document generation and precise image placement. This article will guide you through the process of inserting a PositionedImage
, setting its properties, and configuring text wrapping behavior using Apps Script.
Understanding PositionedImage in Google Docs
In Google Docs, images can be inserted in two primary ways: inline or positioned. Inline images behave like characters within the text flow, while positioned images are anchored to a specific paragraph but can float freely on the page, allowing text to wrap around them. The PositionedImage
class in Google Apps Script is crucial for achieving advanced layout control, including text wrapping.
flowchart TD A[Start] --> B{Insert Image into Doc}; B --> C{Get Image Blob}; C --> D{Insert PositionedImage at Paragraph}; D --> E{Set Image Width/Height}; E --> F{Set Layout: Wrap Text}; F --> G{Set Horizontal/Vertical Alignment}; G --> H[End];
Workflow for inserting and positioning an image with text wrapping.
Inserting and Positioning an Image
To insert a PositionedImage
, you first need an image blob (e.g., from Google Drive, a URL, or an existing image in the document). Then, you specify the paragraph to which the image should be anchored. Once inserted, you can manipulate its size, position, and most importantly, its text wrapping properties.
function insertAndWrapImage() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// 1. Get an image blob (example: from Google Drive)
// Replace 'YOUR_IMAGE_FILE_ID' with an actual Google Drive file ID
const imageFileId = 'YOUR_IMAGE_FILE_ID';
const imageBlob = DriveApp.getFileById(imageFileId).getBlob();
// 2. Insert the image as a PositionedImage at a specific paragraph
// For this example, we'll insert it at the first paragraph.
// You might want to insert it at a newly created paragraph or a specific one.
const paragraph = body.getParagraphs()[0];
if (!paragraph) {
paragraph = body.appendParagraph(' '); // Ensure there's at least one paragraph
}
const positionedImage = paragraph.addPositionedImage(imageBlob);
// 3. Set image size (optional, but recommended)
positionedImage.setWidth(200).setHeight(150);
// 4. Configure text wrapping
positionedImage.setLayout(DocumentApp.PositionedLayout.WRAP_TEXT);
// 5. Set horizontal and vertical alignment/offset
// Horizontal alignment relative to the page margin
positionedImage.setHorizontalAlignment(DocumentApp.HorizontalAlignment.LEFT);
positionedImage.setLeftOffset(50); // 50 points from the left margin
// Vertical alignment relative to the paragraph
positionedImage.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP);
positionedImage.setTopOffset(10); // 10 points from the top of the paragraph
Logger.log('Image inserted and configured for text wrapping.');
}
Google Apps Script function to insert a positioned image and enable text wrapping.
'YOUR_IMAGE_FILE_ID'
with the actual ID of an image file in your Google Drive. Ensure the Apps Script project has the necessary permissions to access Google Drive (DriveApp scope).Controlling Text Wrapping Properties
The setLayout()
method is key to controlling how text interacts with your PositionedImage
. Google Apps Script provides several PositionedLayout
options:
WRAP_TEXT
: Text flows around the image.BREAK_TEXT
: Text breaks above and below the image.IN_FRONT_OF_TEXT
: Image appears on top of the text.BEHIND_TEXT
: Image appears behind the text.
Additionally, you can fine-tune the image's position using setLeftOffset()
, setTopOffset()
, setHorizontalAlignment()
, and setVerticalAlignment()
to achieve the desired layout.
function updateImageWrappingAndPosition() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Assuming the image was previously inserted and is the first positioned image
const positionedImages = body.getPositionedImages();
if (positionedImages.length === 0) {
Logger.log('No positioned images found to update.');
return;
}
const firstImage = positionedImages[0];
// Change layout to BREAK_TEXT
firstImage.setLayout(DocumentApp.PositionedLayout.BREAK_TEXT);
Logger.log('Image layout changed to BREAK_TEXT.');
// Reposition the image to the center of the page horizontally
firstImage.setHorizontalAlignment(DocumentApp.HorizontalAlignment.CENTER);
firstImage.setLeftOffset(0); // Reset offset for center alignment
Logger.log('Image repositioned to center horizontally.');
// You can also adjust margins around the image for WRAP_TEXT layout
// Note: These methods are not directly available on PositionedImage in Apps Script
// For fine-grained text wrapping margins, you might need to adjust the image size
// or use a table/layout workaround in the document itself.
}
Function to update an existing positioned image's layout and position.
PositionedImage
properties, some advanced text wrapping features like specific padding around the image (often found in desktop publishing software) are not directly exposed through the PositionedImage
API. For such cases, consider using tables for layout or adjusting image dimensions.Practical Considerations and Best Practices
When working with PositionedImage
in Google Docs via Apps Script, keep the following in mind:
- Anchoring Paragraph: A
PositionedImage
is always anchored to a specific paragraph. If that paragraph is deleted, the image may also be deleted or its behavior might become unpredictable. Choose a stable anchoring point. - Image Source: Ensure your script has access to the image source (e.g., DriveApp permissions). For external URLs, you might need to fetch the image using
UrlFetchApp
first. - Performance: Inserting many images or performing complex layout operations can impact script execution time. Optimize your script by minimizing API calls and processing images efficiently.
- Error Handling: Implement error handling, especially when dealing with file IDs or external resources, to gracefully manage scenarios where an image might not be found or accessible.
1. Prepare your Google Doc
Open a new or existing Google Doc where you want to insert the image. Ensure it has at least one paragraph of text for anchoring.
2. Upload Image to Google Drive
Upload the image you wish to use to your Google Drive. Make a note of its File ID. You can find this in the URL when viewing the image in Drive (e.g., https://drive.google.com/file/d/FILE_ID/view
).
3. Open Apps Script Editor
Go to Extensions > Apps Script
in your Google Doc to open the script editor.
4. Paste and Modify Script
Copy the insertAndWrapImage()
function into the script editor. Replace 'YOUR_IMAGE_FILE_ID'
with the actual File ID from step 2.
5. Run the Script
Save the script and run the insertAndWrapImage
function. You may be prompted to authorize the script to access your Google Docs and Google Drive. After successful execution, check your Google Doc to see the image inserted with text wrapping.