How to set text vertical alignment in table cell?

I’m not able to set text vertical alignment in table cells. I assumed this was controlled by the TextFrameFormat.AnchoringType property, but it I can’t get it to work as expected.


I asked for assitance through the live chat support, and received the example code below from an Aspose support colleague, which looks like it should work. Unfortunately, when I run this code, the cell text still ends up aligned to the top of the table cell. I have attached an example pptx file that shows both the result of the code, and the (manually adjusted) result that I’m looking for.

What am I doing wrong?

public static void TestTableVertical()
{
<span style="color:#2b91af;">Presentation</span> pres = <span style="color:blue;">new</span> <span style="color:#2b91af;">Presentation</span>(<span style="color:#a31515;">"C:\\Temp\\TestVertical.pptx"</span>);
<span style="color:green;">//Accessing aslide using its slide position</span>
<span style="color:#2b91af;">ISlide</span> slide = pres.Slides[0]; ;
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < slide.Shapes.Count; i++)
{
    <span style="color:blue;">if</span> (slide.Shapes[i] <span style="color:blue;">is</span> <span style="color:#2b91af;">ITable</span>)
    {
        <span style="color:#2b91af;">ITable</span> tbl = (<span style="color:#2b91af;">ITable</span>)slide.Shapes[i]; ;
        <span style="color:green;">//Accessingthe text frame of the first cell of the first row in the table</span>
        <span style="color:#2b91af;">ITextFrame</span> tf = tbl[0, 0].TextFrame;
        tf.Text = <span style="color:#a31515;">"Welcome to Aspose.Slides."</span>;
        <span style="color:green;">//Set textposition to center and vertical</span>
        <span style="color:green;">//  but it doesn't work - text still ends up aligned to the top of the cell</span>
        tf.TextFrameFormat.AnchoringType = <span style="color:#2b91af;">TextAnchorType</span>.Center;
        tf.TextFrameFormat.TextVerticalType = <span style="color:#2b91af;">TextVerticalType</span>.Vertical;

    }
}
pres.Save(<span style="color:#a31515;">"C:\\Temp\\VerticalAlignIllustration.pptx.pptx"</span>, <span style="color:#2b91af;">SaveFormat</span>.Pptx);

}


Hi Steven,

We are sorry for your inconvenience. I request you to please try using the following sample code on your end to serve the purpose. I have verified its working on my end well. Please share, if I may help you further in this regard.

public static void TestTableVertical2()
{

Presentation pres = new Presentation(“D:\Aspose Data\TestVertical.pptx”);
//Accessing aslide using its slide position
ISlide slide = pres.Slides[0]; ;
for (int i = 0; i < slide.Shapes.Count; i++)
{
if (slide.Shapes[i] is ITable)
{
ITable tbl = (ITable)slide.Shapes[i]; ;
//Accessingthe text frame of the first cell of the first row in the table
ITextFrame tf = tbl[0, 0].TextFrame;
ICell cell = tbl[0, 0];
cell.TextAnchorType = TextAnchorType.Center;
cell.TextVerticalType = TextVerticalType.Vertical;
}

}

pres.Save(“D:\Aspose Data\VerticalAlignIllustration.pptx.pptx”, SaveFormat.Pptx);

}



Many Thanks,

Ok, thanks!


cell.TextAnchorType

was what I was looking for.

Hi Steven,

I am glad to hear that things worked on your end. Please share, if I may help you further in this regard.

Many Thanks,