通过Spire.Presentation,开发人员可以非常方便地操作幻灯片表格中文本的对齐方式。在水平方向,我们可以设置水平居左(Left),水平居中(Center),水平居右(Right),还有水平两端对齐(Justify)和不设置水平对齐方式(None)。在垂直方向,主要分为垂直居顶(Top),垂直居中(Center),垂直居底(Bottom)和不设置垂直对齐方式(None)。本文将详细介绍如何设置PPT 表格中文字的水平对齐方式和垂直对齐方式。
首先,我们准备了一个表格,里面所有的文字都是默认的对齐方式(水平居左对齐)。如下图:

现在通过代码设置第一行的水平对齐方式,设置第二行的垂直对齐方式。
C#
  //实例化一个Presentation对象
  Presentation presentation = new Presentation();
  //加载幻灯片
  presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\测试ppt.pptx");
  ITable table = null;
  //遍历文档获取第一张幻灯片中的Shape
  foreach (IShape shape in presentation.Slides[0].Shapes)
  {
      //如果发现有table
      if (shape is ITable)
      {
          table = (ITable)shape;
          //水平方向:
          //设置表格第一行第一列为水平居左
          table[0, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
          //设置表格第一行第二列为水平居中
          table[1, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
          //设置表格第一行第三列为水平居右
          table[2, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Right;
          //设置表格第一行第四列为水平不设置水平对齐方式
          table[3, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.None;
          //设置表格第一行第五列为水平两端对齐
          table[4, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;
          //垂直方向:
          //设置表格第二行第一列为垂直居上
          table[0, 1].TextAnchorType = TextAnchorType.Top;
          //设置表格第二行第二列为垂直居中
          table[1, 1].TextAnchorType = TextAnchorType.Center;
          //设置表格第二行第三列为垂直居下
          table[2, 1].TextAnchorType = TextAnchorType.Bottom;
          //设置表格第二行第四列为不设置垂直对齐方式
          table[3, 1].TextAnchorType = TextAnchorType.None;
          //设置表格第二行第五列为水平垂直都居中
          table[4, 1].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
          table[4, 1].TextAnchorType = TextAnchorType.Center;
      }
  }
  presentation.SaveToFile("对齐方式.pptx", FileFormat.Pptx2010);VB.NET
'实例化一个Presentation对象 Dim presentation As New Presentation() '加载幻灯片
presentation.LoadFromFile("C:\Users\Administrator\Desktop\测试ppt.pptx")
Dim table As ITable = Nothing
'遍历文档获取第一张幻灯片中的Shape For Each shape As IShape In presentation.Slides(0).Shapes '如果发现有table
	If TypeOf shape Is ITable Then
		table = DirectCast(shape, ITable)
		'水平方向: '设置表格第一行第一列为水平居左
		table(0, 0).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Left
		'设置表格第一行第二列为水平居中 table(1,0).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Center '设置表格第一行第三列为水平居右
		table(2, 0).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Right
		'设置表格第一行第四列为水平不设置水平对齐方式 table(3,0).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.None '设置表格第一行第五列为水平两端对齐
		table(4, 0).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Justify
		'垂直方向: '设置表格第二行第一列为垂直居上
		table(0, 1).TextAnchorType = TextAnchorType.Top
		'设置表格第二行第二列为垂直居中 table(1,1).TextAnchorType = TextAnchorType.Center '设置表格第二行第三列为垂直居下
		table(2, 1).TextAnchorType = TextAnchorType.Bottom
		'设置表格第二行第四列为不设置垂直对齐方式 table(3,1).TextAnchorType = TextAnchorType.None '设置表格第二行第五列为水平垂直都居中
		table(4, 1).TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Center
		table(4, 1).TextAnchorType = TextAnchorType.Center
	End If
Next
presentation.SaveToFile("对齐方式.pptx", FileFormat.Pptx2010)效果如下图:

 



 
					



