本文介绍如何使用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 {
//创建Presentation对象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//添加形状
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);
//删除默认段落
textShape.getTextFrame().getParagraphs().clear();
//添加段落和文字
textShape.getTextFrame().getParagraphs().append(new ParagraphEx());
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().append(new PortionEx("文字居右靠下"));
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("宋体"));
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);
//设置文字水平靠右
textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
//设置文字垂直靠下
textShape.getTextFrame().setAnchoringType(TextAnchorType.BOTTOM);
//保存文档
presentation.saveToFile("TextAlignment.pptx",FileFormat.PPTX_2013);
}
}