How can we align an image and texts in same single line using google docs api with PHP?

enter image description hereI am working on google docs api with PHP code. I want to insert an image and texts in same line.Is it possible to do this with google docs api? I can insert image and texts but not in single line.Is there any way to do so?

For inserting texts-

'insertText' => [
     'text' => "hello",
        'location' => [
        'index' => 1,
    ]
]

and for inserting images-

"insertInlineImage" => [
            "location" => [
                "index" => 1
            ],
            "uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
            "objectSize" => [
                "height"=> [
                    "magnitude"=> 10,
                    "unit" => "PT"
                ],
                "width"=> [
                    "magnitude"=> 10,
                    "unit"=> "PT"
                ]
            ]
        ]

Answer

Solution:

Now, I noticed that your question had been updated. From your updated question, unfortunately, I thought that in the current stage, your goal cannot be directly achieved. So, in this updated answer, as a workaround, how about using a table as follows?

Sample script:

$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
    new Google_Service_Docs_Request([
        'insertTable' => [
            'location' => ['index' => 1],
            'columns' => 2,
            'rows' => 1
        ]
    ]),
    new Google_Service_Docs_Request([
        "updateTableCellStyle" => [
            "tableCellStyle" => [
                "borderBottom" => $b,
                "borderTop" => $b,
                "borderLeft" => $b,
                "borderRight" => $b,
            ],
            "tableStartLocation" => ["index" => 2],
            "fields" => "borderBottom,borderTop,borderLeft,borderRight"
        ]
    ]),
    new Google_Service_Docs_Request([
        "insertInlineImage" => [
        "location" => [
            "index" => 5
        ],
        "uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
        "objectSize" => [
            "height"=> [
                "magnitude"=> 100,
                "unit" => "PT"
            ],
            "width"=> [
                "magnitude"=> 100,
                "unit"=> "PT"
            ]
        ]
    ]]),
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => "hello1\nhello2\nhello3",
               'location' => [
               'index' => 8,
           ]
    ]]),
    new Google_Service_Docs_Request([
        "updateParagraphStyle" => [
        "paragraphStyle" => [
            "alignment" => "START"
        ],
        "range" => [
            "startIndex" => 5,
            "endIndex" => 5,
        ],
        "fields" => "alignment"
    ]]),
    new Google_Service_Docs_Request([
        "updateParagraphStyle" => [
        "paragraphStyle" => [
            "alignment" => "END"
        ],
        "range" => [
            "startIndex" => 8,
            "endIndex" => 23,
        ],
        "fields" => "alignment"
    ]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
  • When this script is run, a table of 1 row and 2 columns is created and the table border is removed. And, the image and text are put to the columns "A" and "B", respectively. And then, each cell is aligned.

Note:

  • In this sample, I used a table of 1 row and 2 columns. If you want to modify this alignment, please modify the above script for your actual situation.

Added:

From OP's following reply,

But as you see in my picture I have shown it in my header and from your script what I have to change to make it on my header and for your information I am able to get my header id. Just help me to modify your answer to place that table and image in my header.

Unfortunately, I couldn't notice you wanted to put it to the header from your question and your showing script. In this case, a simple modification is reflected in the above script. Please add segmentId to each request as follows.

Sample script:

$headerId = "kix.###"; // Please set your header ID.
$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
    new Google_Service_Docs_Request([
        'insertTable' => [
            'location' => ['index' => 1, "segmentId" => $headerId],
            'columns' => 2,
            'rows' => 1
        ]
    ]),
    new Google_Service_Docs_Request([
        "updateTableCellStyle" => [
            "tableCellStyle" => [
                "borderBottom" => $b,
                "borderTop" => $b,
                "borderLeft" => $b,
                "borderRight" => $b,
            ],
            "tableStartLocation" => ["index" => 2, "segmentId" => $headerId],
            "fields" => "borderBottom,borderTop,borderLeft,borderRight"
        ]
    ]),
    new Google_Service_Docs_Request([
        "insertInlineImage" => [
        "location" => ["index" => 5, "segmentId" => $headerId],
        "uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
        "objectSize" => [
            "height"=> [
                "magnitude"=> 100,
                "unit" => "PT"
            ],
            "width"=> [
                "magnitude"=> 100,
                "unit"=> "PT"
            ]
        ]
    ]]),
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => "hello1\nhello2\nhello3",
               'location' => ['index' => 8, "segmentId" => $headerId]
    ]]),
    new Google_Service_Docs_Request([
        "updateParagraphStyle" => [
        "paragraphStyle" => [
            "alignment" => "START"
        ],
        "range" => [
            "startIndex" => 5,
            "endIndex" => 5,
            "segmentId" => $headerId
        ],
        "fields" => "alignment"
    ]]),
    new Google_Service_Docs_Request([
        "updateParagraphStyle" => [
        "paragraphStyle" => [
            "alignment" => "END"
        ],
        "range" => [
            "startIndex" => 8,
            "endIndex" => 23,
            "segmentId" => $headerId
        ],
        "fields" => "alignment"
    ]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Note:

  • If you use the header ID of the 1st header ID, please check the checkbox for using the 1st header on the Google Document.

Source