// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); worksheet.getRange("B1").setValue("Config TextRange alignment"); worksheet.getRange("G1").setValue("Config each paragraph's alignment"); IShape shapeOfLeftTopAlignment = worksheet.getShapes().addShape(AutoShapeType.Rectangle, worksheet.getRange("B3:E6")); shapeOfLeftTopAlignment.getTextFrame().getTextRange().add("First shape."); shapeOfLeftTopAlignment.getTextFrame().getTextRange().add("Left and top alignment."); // set first shape text alignment to left and top shapeOfLeftTopAlignment.getTextFrame().getTextRange().setTextAlignment(TextAlignmentAnchor.Left); shapeOfLeftTopAlignment.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorTop); IShape shapeOfCenterMiddleAlignment = worksheet.getShapes().addShape(AutoShapeType.Rectangle, worksheet.getRange("B8:E11")); shapeOfCenterMiddleAlignment.getTextFrame().getTextRange().add("Second shape."); shapeOfCenterMiddleAlignment.getTextFrame().getTextRange().add("Center and middle alignment."); // set second shape text alignment to center and middle shapeOfCenterMiddleAlignment.getTextFrame().getTextRange().setTextAlignment(TextAlignmentAnchor.Center); shapeOfCenterMiddleAlignment.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorMiddle); IShape shapeOfRightBottomAlignment = worksheet.getShapes().addShape(AutoShapeType.Rectangle, worksheet.getRange("B13:E16")); shapeOfRightBottomAlignment.getTextFrame().getTextRange().add("Third shape."); shapeOfRightBottomAlignment.getTextFrame().getTextRange().add("Right and bottom alignment."); // set third shape text alignment to right and bottom shapeOfRightBottomAlignment.getTextFrame().getTextRange().setTextAlignment(TextAlignmentAnchor.Right); shapeOfRightBottomAlignment.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorBottom); IShape shapeOfThereAlignmentParagraphs = worksheet.getShapes().addShape(AutoShapeType.Rectangle, worksheet.getRange("G3:J6")); shapeOfThereAlignmentParagraphs.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorMiddle); // set different alignments for the three paragraphs of the last shape shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().add("Left."); shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().add("Center."); shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().add("Right."); shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().getParagraphs().get(0).setTextAlignment(TextAlignmentAnchor.Left); shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().getParagraphs().get(1).setTextAlignment(TextAlignmentAnchor.Center); shapeOfThereAlignmentParagraphs.getTextFrame().getTextRange().getParagraphs().get(2).setTextAlignment(TextAlignmentAnchor.Right); // Save to an excel file workbook.save("SetShapeTextAlignment.xlsx");