We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Resize of table's columns

Hi,

I’m working with Aspose.Slides, adding a table to a slide via C# code.
the text size within the cells is unknown and I’m trying to find the needed size per each column

To calculate the size, I’m trying to add a new table, set the text and font and try to check the width of the table after adding the text, however, the width stays the same…
How can get the needed size per each column based on the text?

I’ve looked at this thread but still no answers:
Resize a table to fit on a slide


this is the sample code I’m trying, for your reference:

<o:p> </o:p>

PresentationEx p = new PresentationEx();
int slideIndex = p.Slides.AddEmptySlide(p.LayoutSlides[0]);
SlideEx slide = p.Slides[slideIndex];
int tIndex = p.Slides[slideIndex].Shapes.AddTable(0, 0, new double[] { 5 }, new double[] { 5 });

string textToAdd = "some text long long text"; TableEx t = p.Slides[slideIndex].Shapes[tIndex] as TableEx;

double cellWidth = t[0, 0].Width;

t[0, 0].TextFrame.Text = textToAdd;

t[0, 0].MarginBottom = 0.0001;

t[0, 0].MarginTop = 0.0001;

// XXX - the value stays as before:

double cellWidthAfter = t[0, 0].Width;

p.Save("C:\\1.pptx", Aspose.Slides.Export.SaveFormat.Pptx);





Hi Eli,


I have worked with the sample code and desired output in your post. I like to clarify that when you add the text inside any table cell the cell width is not changed and only height is changed. So, row height will be altered on adding text and column width remain the same what you have set. You can set the new column width and it will be set accordingly. But the table size will grow from top to down with inclusion of text by adjusting row height and will not increase from left to right by adjusting column width. I hope this will clarify the concept to you.

Please share, if I may help you further in this regard.

Many Thanks,

Hi, thank you for your suggestion, however, something is not working as expected here - the row height isn’t changing as you say, in fact, it remains in a fixed value regardless even when it is manually set in code.
.

here is the code snippet. adding a table with 5 rows, and setting the height to row 0 doesn’t change the height from 2560. adding text doesn’t also change that.
it is important to note that the output does show the row height it was set to be and not 2560, but the property table.GetRowHeight(0) always returns 2560.
Please advise.



private void Form1_Load(object sender, EventArgs e)

{
int fontIndex;
Presentation p = new Presentation();
System.Drawing.Font mainSlideFont = CreateAndAddFont(p, “arial”, 10, out fontIndex, false, false);
Slide slide =
p.AddBodySlide();

string textToAdd = "some long text to place in a table";

Size size = GetNeededSizeForText(textToAdd, mainSlideFont);
Table table = slide.Shapes.AddTable(0, 0, size.Width, size.Height, 1, 4);

table.Height = 5 * size.Height;

TextFrame tf = table.GetCell(0, 0).TextFrame;

table.SetRowHeight(0, size.Height);

tf.Text = textToAdd;

foreach (Paragraph par in tf.Paragraphs)

{

par.HasBullet = false;

par.Alignment = TextAlignment.Left;

foreach (Portion por in par.Portions)

{

por.FontHeight = 10;

por.FontIndex = fontIndex;

}

}

// BUG - the height is not changing at all!

double cellHeight = table.GetCell(0, 0).Height;

p.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "1.ppt"), Aspose.Slides.Export.SaveFormat.Ppt);

}

// get the string measurement

private Size GetNeededSizeForText(string s, System.Drawing.Font font)

{

Size size = System.Windows.Forms.TextRenderer.MeasureText(s, font);

size = new Size(GetSlidePixels(size.Width), GetSlidePixels(size.Height));

return size;

}

private int GetSlidePixels(double screenPixels)

{

// 5760 / 922;

double ratio = 5760 / 922.0;

double value = ratio * screenPixels;

return (int)value;

}

private System.Drawing.Font CreateAndAddFont(Presentation presentation, string fontName, int fontSize, out int foundFontIndex,

bool isBold, bool isUnderline)

{

// Try to find the given font, if needed, adds it to the presentation fonts:

for (int fontIndex = 0; fontIndex < presentation.Fonts.Count; fontIndex++)

{

if (presentation.Fonts[fontIndex].FontName.Equals(fontName, StringComparison.OrdinalIgnoreCase))

{

foundFontIndex = fontIndex;

return new System.Drawing.Font(presentation.Fonts[fontIndex].FontName, fontSize);

}

}

FontEntity fntEntNew = new FontEntity(presentation, presentation.Fonts[0]);

fntEntNew.FontName = fontName;

foundFontIndex = presentation.Fonts.Add(fntEntNew);

FontEntity fontEntity = presentation.Fonts[foundFontIndex];

System.Drawing.FontStyle fontStyle = System.Drawing.FontStyle.Regular;

if (isBold && isUnderline)

fontStyle = System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline;

else if (isBold)

fontStyle = System.Drawing.FontStyle.Bold;

else if (isUnderline)

fontStyle = System.Drawing.FontStyle.Underline;

System.Drawing.Font font = new System.Drawing.Font(

fontEntity.FontName, fontSize, fontStyle);

return font;

}

}






Hi Eli,


I am working with your sample code and will share feedback shortly after investigation.

Many Thanks,

Hi Eli,


I have worked with the sample code shared by you and it has been written as per old legacy API that is no longer supported. I have converted the code w.r.t new API and things seems to work on my end. Please remember the default minimum row height that is also set by PowerPoint as well is 28. Even, if you set the height lesser than this still minimum would be 28. However, if your text exceeds the limit that can be held inside the cell with height 28, the height will automatically get increased based on text. In your example as well when working using Aspose.Slides for .NET 14.9.0, the row height gets changed as you enter the new text in cell. Please try using the following sample code on your end to verify the fact. Please share, if I may help you further in this regard.

public static void Form1_Load()
{
int fontIndex;
Presentation p = new Presentation();
ISlide slide = p.Slides.AddEmptySlide(p.LayoutSlides[0]);
string textToAdd = “some long text to place in a table”;
Size size = new Size();
//Define columns with widths and rows with heights
double[] dblCols = { 50, 50, 50, 50 };//4 columns in your data
double[] dblRows = { 5 };//start with one row

//Add table shape to slide
ITable table = slide.Shapes.AddTable(100, 50, dblCols, dblRows);
table.Height = 5 * size.Height;
ITextFrame tf = table[0, 0].TextFrame;
table.Rows[0].MinimalHeight =size.Height;

tf.Text = textToAdd;
foreach (IParagraph par in tf.Paragraphs)
{
par.ParagraphFormat.Bullet.Type=BulletType.None;
par.ParagraphFormat.Alignment= TextAlignment.Left;
foreach (IPortion por in par.Portions)
{
por.PortionFormat.FontHeight= 10f;
por.PortionFormat.LatinFont= new FontData(“Arial”);

}
}

// BUG - the height is not changing at all!
double cellHeight = table[0, 0].Height;
p.Save(“C:\Presentations\1.ppt”, Aspose.Slides.Export.SaveFormat.Ppt);
}


Many Thanks,

Hi, thank you, I will try to execute the code in the new Dlls. regardless of this, is there a ‘measurestring’ method that can be used on this Presentation object, to get the right width that is needed per each string in a table?

Hi Eli,


I have observed the comments shared and regret to share that there is no such method or property available in Aspose.Slides to get the rightly suggested width for the text. One need to devise his own mechanism or technique to serve the purpose on his end. Please share, if I may help you further in this regard.

Many Thanks,

Hi,

I’ve executed the code using the latest Aspose Dlls (14.10), the height is changing when text is added, however, when changing the width, the height remains the same as before, for example,
setting:
table.Columns[0].Width = 10;
should result in a change in height (the height would increase the be able to hold all the text with this smaller width). the height remains the same.
so the idea of increasing the width of a cell until the height changes (indicating that it fit into one row using this width) is not working.

again, I reminding you the original need - to calculate the needed width per each cell in the table. if the solution requires upgrading to aspose 14.10 this is what I’ll do, but I need to be sure this works first.

Please advise.


Hi Eli,


Thank you for sharing the feedback. I request you to please share the working sample code that is demonstrating your requirement and results achieved. Please also share the generated presentation and desired output presentation. Please also make sure that what you are requiring from Aspose.Slides to offer you is possible in PowerPoint as well via automatic means as per your requirement.

Many Thanks,

the example is the same as above posts - you suggested to run it using the latest version and this is what I’ve done, changing the width of table’s cell doesn’t change the height

This is a very simple matter, and not related to the output PPT in anyway - I need to set the width of a table’s cell based on the text size. when I change the width of the column (here is the code example: table.Columns[0].Width = 50;) , I would expect the table height to change, because the text doesn’t fit anymore and it needs more lines to show all the text.
This doesn’t happen, please advise.

Hi Eli,


I have worked over the requirements shared by you and like to share that I have tried changing the column width and it also affect the row height. Please try using the following sample code to see the effect. I have also attached the two presentation files in this regard to see the comparison. If there is still an issue then please share in the form of snapshot, generated and desired presentation that what you would like Aspose.Slides to offer you in your situation.

public static void Form1_Load()
{
int fontIndex;
Presentation p = new Presentation();
ISlide slide = p.Slides.AddEmptySlide(p.LayoutSlides[0]);
string textToAdd = “some long text to place in a table”;
Size size = new Size();
//Define columns with widths and rows with heights
double[] dblCols = { 50, 50, 50, 50 };//4 columns in your data
double[] dblRows = { 5 };//start with one row

//Add table shape to slide
ITable table = slide.Shapes.AddTable(100, 50, dblCols, dblRows);
table.Height = 5 * size.Height;
ITextFrame tf = table[0, 0].TextFrame;
table.Rows[0].MinimalHeight =size.Height;

tf.Text = textToAdd;
foreach (IParagraph par in tf.Paragraphs)
{
par.ParagraphFormat.Bullet.Type=BulletType.None;
par.ParagraphFormat.Alignment= TextAlignment.Left;
foreach (IPortion por in par.Portions)
{
por.PortionFormat.FontHeight= 10f;
por.PortionFormat.LatinFont= new FontData(“Arial”);

}
}


double cellHeight = table[0, 0].Height;

p.Save(“C:\Presentations\1.ppt”, Aspose.Slides.Export.SaveFormat.Ppt);

//Now lets change the width of column and see if the row height is changed or not
table.Columns[0].Width = table.Columns[0].Width * 2;

//Yes the row height is changed
cellHeight = table[0, 0].Height;
p.Save(“C:\Presentations\2.ppt”, Aspose.Slides.Export.SaveFormat.Ppt);

}

Many Thanks,

Hi,

The output presentation is indeeded with the new width, however, the problem is before that - the cell height (last line before the second save) remains the same as before changing the width. I would expect the property to change, not just the final output in PPT.

Hi Eli,

I have used the above sample code using Aspose.Slides for .NET 14.10.0 on my end and have attached the snapshot that specify the changed height for the row when column width is changed. You can observe in the attached image that height is changed. Please share, if I may help you further in this regard.

Many Thanks,

The change in height only occurs because you perform a save. if you’ll remove the first ‘save’ line, the height will remain 79 even after width change (see attachment)



Hi Eli,

Thanks for your clarification. This seems to be an issue and I have created an issue with ID SLIDESNET-35938 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be resolved.

We are sorry for your inconvenience,