Insert Inline Images

You can insert an image into a document using the InsertInlineImageRequest method. You can optionally specify a size to resize the image.

Java

    List<Request> requests = new ArrayList<>();
    requests.add(new Request().setInsertInlineImage(new InsertInlineImageRequest()
            .setUri("https://www.gstatic.com/images/branding/product/1x/docs_64dp.png")
            .setLocation(new Location().setIndex(1))
            .setObjectSize(new Size()
                    .setHeight(new Dimension()
                            .setMagnitude(50.0)
                            .setUnit("PT"))
                    .setWidth(new Dimension()
                            .setMagnitude(50.0)
                            .setUnit("PT")))));

    BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
    BatchUpdateDocumentResponse response = docsService.documents()
            .batchUpdate(DOCUMENT_ID, body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
    'insertInlineImage' => array(
        'uri' => 'https://www.gstatic.com/images/branding/product/1x/docs_64dp.png',
        'location' => array(
            'index' => 1,
        ),
        '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
        },
        'uri':
            'https://www.gstatic.com/images/branding/product/1x/docs_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.

다음에 대한 의견 보내기...

도움이 필요하시나요? 지원 페이지를 방문하세요.