以编程方式在单元格中居中文本:使用单行文本但不能更长时间

问题描述 投票:2回答:1

我在PowerPoint中创建了一个表,我想在每个单元格中水平居中文本。我使用了MsoHorizo​​ntalAnchor.msoAnchorCenter。

如果文本很短且在一行中,它可以很好地工作但如果文本很长,它会显示在多行中并且不会居中。代码:

myShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "shortOK";
myShape.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "Long text is not displayed centered";

myShape.Table.Cell(1, 1).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;                 
myShape.Table.Cell(1, 2).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;

myShape.Table.Cell(1, 1).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;
myShape.Table.Cell(1, 2).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;

如果以1个以上的方式显示,您知道如何保持文本居中吗?

c# powerpoint office-interop
1个回答
4
投票

我会使用这个文档,“https://docs.microsoft.com/en-us/office/vba/api/powerpoint.paragraphformat.alignment”。 interop中的TextRange有一个名为“PPParagraphAlignment.ppAlignCenter”的东西,例如:

 textrange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;
© www.soinside.com 2019 - 2024. All rights reserved.