mattsmith5
mattsmith5

Reputation: 1073

Google Slide API : Add Shape and Center Text Align

How do I set Text Alignment Center in a Shape InsertTextRequest using Google Slides API?

    requests.add(new Request()
            .setCreateShape(new CreateShapeRequest()
                    .setObjectId(elementRandomString)
                    .setShapeType("RECTANGLE")
                    .setElementProperties(new PageElementProperties()
                            .setPageObjectId(this.pageObjectId)
                            .setSize(new Size()
                                    .setHeight(ptHeight)
                                    .setWidth(ptWidth))
                            .setTransform(new AffineTransform()
                                    .setScaleX(1.0)
                                    .setScaleY(1.0)
                                    .setTranslateX(xLocation)
                                    .setTranslateY(yLocation)
                                    .setUnit("PT")))));

    if (shapeModel.textModel != null && shapeModel.textModel.textValue != null) {
        requests.add(new Request()
                .setInsertText(new InsertTextRequest()
                        .setObjectId(elementRandomString)
                        .setText("BOOK")));
    }

The last line, we set the text; want to center Text in the middle.

Center align in rectangle

It was not specified in resources below.

Resources:

https://developers.google.com/slides/how-tos/add-shape

Java Insert Text Request

Upvotes: 1

Views: 383

Answers (1)

Kristkun
Kristkun

Reputation: 5953

It is not possible to set the alignment using InsertTextRequest. You can update the alignment under paragraph styles using the UpdateParagraphStyleRequest message in a call to batchUpdate.

Sample Request Body:

{
  "requests": [
    {
      "updateParagraphStyle": {
        "objectId": "gc9072fb39b_0_5",
        "style": {
          "alignment": "CENTER"
        },
        "fields": "alignment"
      }
    }
  ]
}

Applicable Java Code:

requests.add(new Request()
        .setUpdateParagraphStyle(new UpdateParagraphStyleRequest()
                .setObjectId(elementRandomString)
                .setFields("*")
                .setStyle(new ParagraphStyle()
                        .setAlignment("CENTER"))));

Output:

Before:

enter image description here

After:

enter image description here

References:

Upvotes: 1

Related Questions