本文介绍如何使用Spire.Presentation for .NET设置形状中文字的对齐方式。
C#
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Drawing;
namespace TextAlignment
{
class Program
{
static void Main(string[] args)
{
//创建Presentation对象
Presentation presentation = new Presentation();
presentation.SlideSize.Type = SlideSizeType.Screen16x9;
//添加形状
IAutoShape textShape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 400, 200));
textShape.ShapeStyle.LineColor.Color = Color.DarkGray;
textShape.Fill.FillType = FillFormatType.None;
//删除默认段落
textShape.TextFrame.Paragraphs.Clear();
//添加段落和文字
textShape.TextFrame.Paragraphs.Append(new TextParagraph());
textShape.TextFrame.Paragraphs[0].TextRanges.Append(new TextRange("文字对齐方式"));
textShape.TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 20f;
textShape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("黑体");
textShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
textShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
//设置文字水平靠右
textShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Right;
//设置文字垂直靠下
textShape.TextFrame.AnchoringType = TextAnchorType.Bottom;
//保存文档
presentation.SaveToFile("AlignText.pptx", FileFormat.Pptx2013);
}
}
}
VB.NET
Imports Spire.Presentation
Imports System.Drawing
Imports Spire.Presentation.Drawing
Namespace TextAlignment
Class Program
Shared Sub Main(ByVal args() As String)
'创建Presentation对象
Dim presentation As Presentation = New Presentation()
presentation.SlideSize.Type = SlideSizeType.Screen16x9
'添加形状
Dim textShape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(50,50,400,200))
textShape.ShapeStyle.LineColor.Color = Color.DarkGray
textShape.Fill.FillType = FillFormatType.None
'删除默认段落
textShape.TextFrame.Paragraphs.Clear()
'添加段落和文字
textShape.TextFrame.Paragraphs.Append(New TextParagraph())
textShape.TextFrame.Paragraphs(0).TextRanges.Append(New TextRange("文字对齐方式"))
textShape.TextFrame.Paragraphs(0).TextRanges(0).FontHeight = 20f
textShape.TextFrame.Paragraphs(0).TextRanges(0).LatinFont = New TextFont("黑体")
textShape.TextFrame.Paragraphs(0).TextRanges(0).Fill.FillType = FillFormatType.Solid
textShape.TextFrame.Paragraphs(0).TextRanges(0).Fill.SolidColor.Color = Color.Black
'设置文字水平靠右
textShape.TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Right
'设置文字垂直靠下
textShape.TextFrame.AnchoringType = TextAnchorType.Bottom
'保存文档
presentation.SaveToFile("AlignText.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace