Free Support Forum - aspose.com

Setting table width and position dynamically based on data

Hello,

I’m new to aspose slide. I’m able to generate pptx file using aspose slide product. I’m facing an issue since 1 month of setting table width and position on slide dynamically by code(.NET). I couldn’t found syntax for setting width and positioning through code can you please help me to solve out.
If anyone can provide syntax for setting width of table and table cells than that would be great.

Thanks.

Hi Jonathan,


Thank you for your interest in Apose.Slides.

I have observed your comments and like to share with you that following code sets the height, width of rows and position of table.

double[] dblCols = { 30, 30, 30, 30, 30, 30, 30 };
double[] dblRows = { 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 };

//Add table shape to slide
ITable tbl = slide.Shapes.AddTable(100, 50, dblCols, dblRows);

Where 30 is the width of each column, 20 is the height of each row; 100 and 50 are the x,y coordinates of top left corner of the table.

Also, I request you to please visit this link for your kind reference.

I hope this clarifies the concept. Please share if I may help you further in this regard.

Best Regards,

Thanks for the reply Adnan,

As you suggested, I did the same and I am able to achieve my goal. but I don’t want to add table from code. I just want to fetch table from slide and want to change height, width of cells as per the text that I want to add in cell and position of existing table in slide.

e.g. If my cell text is “New” then want to set height,width of cell according to it and if my cell text is “New Accomodation” then want to set height,width of cell accordingly.

Note: I am using Aspose.slides version(v. 15.1.0.0)

Thanks.

Hi Jonathan,


I have observed your comments and like to request you to please share with us a sample presentation with your desired results so that I may try to help you out. Also, I would like to request you to please try using Aspose.Slides for .NET 15.6.0 on your end as it is quite improved version as compared to the version which you are using on your side.

Best Regards,

Thanks Adnan for the quick response.


I have attached a .pptx file for your reference. Please find attachment.

Attachment will help you to understand my issue with more clarification.

Thanks.

Hi Jonathan,


Thank you for sharing the presentation file.

I have observed your requirements and worked with the presentation file shared by you. I would like to request you to please try using following sample code with the file I have attached as an input file. The only difference between your file and this attached one is that, to differentiate between two tables, I have added AlternativeText to one table. I have also attached the generated output file for your kind reference.

Presentation pres = new Presentation(@“D:\PPT_Issuetbl.pptx”);
ISlide sld = pres.Slides[0];
for (int j = 0; j < sld.Shapes.Count; j++)
{
IShape shape2 = sld.Shapes[j];
if (shape2.AlternativeText == “now”)
//this if condition is to apply this technique on some specific table in slide
//the table with AlternativeText as “now”
{
if (shape2 is ITable)
{
ITable table = ((ITable)sld.Shapes[j]);
{
IColumn col = table.Columns[0];
table[0, 1].TextFrame.Text = “abcdefghijklmopqrstuv”;
int rows = table.Rows.Count;
int max = 0;
ICell cl = table[0, 1];

for (int k = 0; k< table.Columns.Count; k++ )
{
//Iterate through each row of particular column to find rowl with longest string
for (int i = 0; i < rows; i++)
{
if (table[k, i].TextFrame.Text.Length > max)
{
max = table[k, i].TextFrame.Text.Length;
}
}
//10 is multiplication factor which could be changed if you want to
table.Columns[k].Width = max * 10;
}
}
}
}
pres.Save(@“D:\PPT_IssuetblModified.pptx”, Aspose.Slides.Export.SaveFormat.Pptx);

I hope this will be helpful. Please share if I may help you further in this regard.

Best Regards,

Hi Adnan,


Thanks for the sample code, it works for me. Now I am able to set width of a cell based on text.

Adnan, I have one another query about ppt notes.

I am able to add text in notes of a slide. But when I try to apply style(Italic) to text, It does not take affect. I dont want to apply Italic style to whole notes text. I want partial text of notes should be in italic style and remaining should be in regular style.
I have attached a .pptx file which will give you more clear idea about my issue. Please find attachment.

Thanks.

Hi Jonathan,


I have observed that you have made a similar inquiry concerning to notes slide text formatting in a separate thread as well. I have shared my feedback over this thread link. Please share further details with us in shared thread link if there is still an issue incurring on your end.

Many Thanks,