Text Box in PPTX

Hi,

I need to have the text box with vertical alignment as middle centered in PPTX.

How to do it?

Also what is the purpose of Anchoring? I am not getting information from the website.

Hi Htanmo,


Please use the following code snippet to serve the purpose. Moreover, the AnchoringType defines the anchoring position of text inside text frame. You may use different AnchoringType to see the difference.

//Instantiate PresentationEx
PresentationEx pres = new PresentationEx();

//Get the first slide
SlideEx sld = pres.Slides[0];

//Add an AutoShape of Rectangle type
int idx = sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, 150, 75, 150, 250);
AutoShapeEx ashp = (AutoShapeEx)sld.Shapes[idx];

//Add TextFrame to the Rectangle
ashp.AddTextFrame(“Aspose TextBox”);

TextFrameEx tf = ashp.TextFrame;
tf.AnchoringType = TextAnchorTypeEx.Top;

tf.TextVerticalType = TextVerticalTypeEx.Vertical;
//Write the presentation to disk
pres.Write(“d:\TextBox.pptx”);


Many Thanks,

Hi,

I am bit confused. Please clarify me with the below info.

I used the properties with TEXT FRAMEEX as I have used cell values as text frame in the table. But it did not work.

Then I tried with the cell level formatting and so formatting all cells in the table with the below code gave me VERTICAL ALIGNMENT as MIDDLE.

cell.TextVerticalType = TextVerticalTypeEx.Horizontal;

cell.TextAnchorType = TextAnchorTypeEx.Center;

But I want the VERTICAL ALIGNMENT as MIDDLE CENTERED which I get via PPT Class AnchorText. property.

Why the text frame level formatting is not working in PPTX class when it works fine in PPT class.

I have attached PPTX output presentation. and PPT presentation and snapshots too.

Code for PPTX:

PresentationEx pres = new PresentationEx();
SlideEx slide = pres.Slides[0];
TableEx table = null;

int tableHeight = 300;
int tableWidth = 200;
int rowCount = 4;
int columnCount = 3;
double[] rowHeights = new double[rowCount];
double[] columnWidths = new double[columnCount];

double result = (8 / 4) / 8.0;

for (int i = 0; i < columnWidths.Length; i++)
{
columnWidths[i] = (tableWidth / columnCount);
}
for (int i = 0; i < rowHeights.Length; i++)
{
rowHeights[i] = (tableHeight / rowCount);
}


int tableIndex = slide.Shapes.AddTable(20, 40, columnWidths, rowHeights);
TableEx newTable = (TableEx)slide.Shapes[tableIndex];
foreach (RowEx row in newTable.Rows)
{
foreach (CellEx cell in row)
{
cell.BorderTop.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderBottom.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderRight.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderLeft.FillFormat.FillType = FillTypeEx.NoFill;
cell.FillFormat.FillType = FillTypeEx.NoFill;
cell.TextVerticalType = TextVerticalTypeEx.Horizontal;
cell.TextAnchorType = TextAnchorTypeEx.Center;
}
}

TextFrameEx textFrame = null;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{

textFrame = newTable[j, i].TextFrame;
textFrame.Paragraphs[0].Portions[0].Text = "Sample";
textFrame.Paragraphs[0].Portions[0].PortionFormat.FontItalic = NullableBool.True;
}

}

pres.Write("C:\\Aspose Testing\\modifedNew.PPTX");

Code For PPT:

Presentation pptPres = new Presentation();

Slide pptSlide = pptPres.Slides[0];

TextFrame pptTextFrame = null;

Aspose.Slides.Table newTablePPT = pptSlide.Shapes.AddTable(160, 320, 1600, 2400, columnCount, rowCount);

for (int i = 0; i < rowCount; i++)

{

for (int j = 0; j < columnCount; j++)

{

pptTextFrame = newTablePPT.GetCell(j, i).TextFrame;

pptTextFrame.Paragraphs[0].Portions[0].Text = "Sample";

pptTextFrame.AnchorText = AnchorText.MiddleCentered;

pptTextFrame.Paragraphs[0].Portions[0].FontHeight = 18;

pptTextFrame.Paragraphs[0].Portions[0].FontItalic = true;

}

}

pptPres.Write("C:\\Aspose Testing\\modifedNewPPT.PPT");

Hi Htanmo,


I have modified the code snippet shared by you and have been able to observe the issue of vertical middle centered text. Please share, if I may help you further in this regard.

PresentationEx pres = new PresentationEx();
SlideEx slide = pres.Slides[0];
TableEx table = null;

int tableHeight = 300;
int tableWidth = 200;
int rowCount = 4;
int columnCount = 3;
double[] rowHeights = new double[rowCount];
double[] columnWidths = new double[columnCount];

double result = (8 / 4) / 8.0;

for (int i = 0; i < columnWidths.Length; i++)
{
columnWidths[i] = (tableWidth / columnCount);
}
for (int i = 0; i < rowHeights.Length; i++)
{
rowHeights[i] = (tableHeight / rowCount);
}


int tableIndex = slide.Shapes.AddTable(20, 40, columnWidths, rowHeights);
TableEx newTable = (TableEx)slide.Shapes[tableIndex];
foreach (RowEx row in newTable.Rows)
{
foreach (CellEx cell in row)
{
cell.BorderTop.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderBottom.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderRight.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderLeft.FillFormat.FillType = FillTypeEx.NoFill;
cell.FillFormat.FillType = FillTypeEx.NoFill;
cell.TextVerticalType = TextVerticalTypeEx.Vertical;
cell.TextAnchorType = TextAnchorTypeEx.Center;
}
}

TextFrameEx textFrame = null;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{

textFrame = newTable[j, i].TextFrame;
textFrame.Paragraphs[0].Portions[0].Text = “Sample”;
textFrame.Paragraphs[0].Portions[0].PortionFormat.FontItalic = NullableBool.True;
}

}

pres.Write(“D:\Aspose Data\modifedNew.PPTX”);

Many Thanks,

Thanks for your response.

With your code the text appears vertically with again the HORIZONTAL ALIGNMENT as center but not Center Middle. As I told earlier in sample code and attachements my requirement is to have HORIZONTALLY ALIGNED text which is obtained via TextVerticalTypeEx.Horizontal property.

But the VERTICAL ALIGNMENT property is still MIDDLE as I stated in my code and CENTER as in your code. I want that to be MIDDLE CENTERED.

I am using Aspose.Slides v5.7.0. Please help me out if it is an issue with the Aspose and if so please send me the issue and fix details

I would like to remind u about one more clarification regarding the usage of TextFrame properties in PPTX similar to that in PPT which was asked in the second post.

Let me know if more information is required.

Hi Htanmo,


I have worked over the requirements shared by you. I have modified the code snippet shared earlier and it satisfy your requirements. For your kind reference, I have also shared the generated presentation. Please share, if I may help you further in this regard. I have used Aspose.Slides for .NET 5.8.0.

PresentationEx pres = new PresentationEx();
SlideEx slide = pres.Slides[0];
TableEx table = null;

int tableHeight = 300;
int tableWidth = 200;
int rowCount = 4;
int columnCount = 3;
double[] rowHeights = new double[rowCount];
double[] columnWidths = new double[columnCount];

double result = (8 / 4) / 8.0;

for (int i = 0; i < columnWidths.Length; i++)
{
columnWidths[i] = (tableWidth / columnCount);
}
for (int i = 0; i < rowHeights.Length; i++)
{
rowHeights[i] = (tableHeight / rowCount);
}


int tableIndex = slide.Shapes.AddTable(20, 40, columnWidths, rowHeights);
TableEx newTable = (TableEx)slide.Shapes[tableIndex];
foreach (RowEx row in newTable.Rows)
{
foreach (CellEx cell in row)
{
cell.BorderTop.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderBottom.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderRight.FillFormat.FillType = FillTypeEx.NoFill;
cell.BorderLeft.FillFormat.FillType = FillTypeEx.NoFill;
cell.FillFormat.FillType = FillTypeEx.NoFill;
cell.TextVerticalType = TextVerticalTypeEx.Vertical;
cell.TextAnchorType = TextAnchorTypeEx.Center;
TextFrameEx tf=cell.TextFrame;
ParagraphEx para = tf.Paragraphs[0];
para.Alignment = TextAlignmentEx.Center;
}
}

TextFrameEx textFrame = null;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{

textFrame = newTable[j, i].TextFrame;
textFrame.Paragraphs[0].Portions[0].Text = “Sample”;
textFrame.Paragraphs[0].Portions[0].PortionFormat.FontItalic = NullableBool.True;
}

}

pres.Write(“D:\Aspose Data\modifedNew.PPTX”);

Many Thanks,

Sorry for again asking the clarification as I am not clear with the answers.

I have attached the screen cap from the attachement you have given which shows that the HORIZONTAL ALIGNMENT is MIDDLE as in "Center alignment.jpg". This is because of TextVerticalEx.Vertical property in code. Else if TextVerticalEx.Horizontal is used, the text must have been with VERTICAL ALIGNMENT as MIDDLE and not as MIDDLE CENTERED which is requirement.

But there is an arrangement in MS POWEPOINT as MIDDLE CENTERED for VERTICAL ALIGNMENT. (Attached as "My Requirement.JPG")

Also the recent code which you have given is converting the CellEx to a TextFrameEx.

But why the cell level properties in Aspose.Slides.PPTX.are not being set when used with TextFrameEx (obtained via table[].TextFrame. Becasuse in Aspose.Slides.PPT class there was no need to set the properties of cell using Cell properties.

Thanks for ur support.

Hi Htanmo,


I agree with your point. I like to share that Middle Center alignment is unavailable in Aspose.Slides. However, the code snippet that I have shared yields the results that can be achieved through Middle Centered vertical alignment.Also the result that I have shared using my code is identical to those required by you. If there is still issue please share with us.

Many Thanks,