News Category

Set Text Alignment within a Shape in Java

2021-07-01 06:31:09 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to set text horizontal and vertical alignment within a shape using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetTextAlignment {
    public static void main(String[] args) throws Exception {

        //Create a Presentation object 
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Add a shape 
        IAutoShape textShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 200));
        textShape.getShapeStyle().getLineColor().setColor(Color.DARK_GRAY);
        textShape.getFill().setFillType(FillFormatType.NONE);

        //Remove the default paragraphs 
        textShape.getTextFrame().getParagraphs().clear();

        //Add a paragraph and append some text to it 
        textShape.getTextFrame().getParagraphs().append(new ParagraphEx());
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().append(new PortionEx("Text Alignment"));
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial"));
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(Color.BLACK);

        //Set text horizontal alignment to right 
        textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);

        //Set text vertical alignment to bottom 
        textShape.getTextFrame().setAnchoringType(TextAnchorType.BOTTOM);

        //Save to file
        presentation.saveToFile("output/TextAlignment.pptx",FileFormat.PPTX_2013);
    }
}

Set Text Alignment within a Shape in Java

Additional Info

  • tutorial_title:
Last modified on Thursday, 02 September 2021 06:00