Insert inline images

You can insert an image into a document using the InsertInlineImageRequest method. You can optionally use the objectSize field to resize the image.

Java

List<Request>requests=newArrayList<>();
requests.add(newRequest().setInsertInlineImage(newInsertInlineImageRequest()
.setUri("https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png")
.setLocation(newLocation().setIndex(1).setTabId(TAB_ID))
.setObjectSize(newSize()
.setHeight(newDimension()
.setMagnitude(50.0)
.setUnit("PT"))
.setWidth(newDimension()
.setMagnitude(50.0)
.setUnit("PT")))));
BatchUpdateDocumentRequestbody=newBatchUpdateDocumentRequest().setRequests(requests);
BatchUpdateDocumentResponseresponse=docsService.documents()
.batchUpdate(DOCUMENT_ID,body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
 'insertInlineImage' => array(
 'uri' => 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
 'location' => array(
 'index' => 1,
 'tabId' => TAB_ID,
 ),
 'objectSize' => array(
 'height' => array(
 'magnitude' => 50,
 'unit' => 'PT',
 ),
 'width' => array(
 'magnitude' => 50,
 'unit' => 'PT',
 ),
 )
 )
));
// Execute the requests.
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
 'requests' => $requests
));
$response =
 $docsService->documents->batchUpdate(DOCUMENT_ID, $batchUpdateRequest);

Python

requests = [{
 'insertInlineImage': {
 'location': {
 'index': 1,
 'tabId': TAB_ID
 },
 'uri':
 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
 'objectSize': {
 'height': {
 'magnitude': 50,
 'unit': 'PT'
 },
 'width': {
 'magnitude': 50,
 'unit': 'PT'
 }
 }
 }
}]
# Execute the request.
body = {'requests': requests}
response = service.documents().batchUpdate(
 documentId=document_id, body=body).execute()
insert_inline_image_response = response.get('replies')[0].get(
 'insertInlineImage')
print('Inserted image with object ID: {0}'.format(
 insert_inline_image_response.get('objectId')))

The method inserts the image as a new ParagraphElement with an InlineObjectElement of length 1, where the startIndex is the request's location.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月03日 UTC.