Adding Rows To A Table - Continue onto Next Slide

I have a table on a template that is dynamic in size. I add to the table using

table.getRows().addClone(tblRow , false);

The issue I am trying to solve is that if the table has too many rows then it can truncate the rows and will not display on the next slide.

Is there a way to have the table continue onto the next slide?

I have one slide in the template that is cloned over and over for each object in a master list. There is a table on that one slide in the template that I am dealing with.

Here is a snippet of source code…

Presentation target = new Presentation(customTemplate.getIs());

ISlideCollection slds = target.getSlides();

//add a blank cloned slide for each item in the list
for(int total = 1; total<items.size(); total++)
slds.addClone(target.getSlides().get_Item(0));

IShape shape = null;

shape = slide.findShapeByAltText(“StepTable”);

ITable table = (ITable)shape;
IRow tblRow = table.getRows().get_Item(0);

int numColumns = tblRow.size();

List mitiSteps = risk.getSet().getStepsAsArrayList();
Iterator k = mitiSteps.iterator();

int mitiNumber = 1;

while (k.hasNext()) {

mitiNumber++;
if(mitiSteps.size() >= mitiNumber)
//AddClone adds a row in the end of the table
table.getRows().addClone(tblRow , false);
}

Hi John,

Thanks for inquiring Aspose.Slides.

I have observed your requirements and like to share that there is no automatic option available to move the rows exceeding slide height to next slide. However, you can check for your self if the table has overflowed slide height by checking the table height + table Y value not exceeding slide height, whenever, you add data to table or clone rows. If the slide height is exceeded, you can add new slide and table on that to fill rest of table. I hope this will be helpful.

Many Thanks,

ok - yes I have figured out when the dynamic table reaches the bottom of the slide. And then I add a slide.

Maybe you can help me figure out how to then add a table to this new slide where the table has the same column configuration as the original table. So far I have no luck in figuring that out.

Here is some code that does not work where ‘shape’ is the original table. I get this by using the alternative text of the table…

if(table.getHeight() + table.getY() > target.getSlideSize().getSize().getHeight())
{
// go to next slide
// make another table

ISlide slide222 = target.getSlides().addEmptySlide(target.getLayoutSlides().getByType(SlideLayoutType.Blank));

ITable table1 = (ITable)shape;

table1.getRows().addClone(tblRow , false);

slide222.getShapes().addClone((IShape)table1);

//index, with attached rows
table1.getRows().removeAt(1, true);

//try to move the table to the top of the slide

table1.setY(15);

//I use the columns in the table later to fill in the data - so I need to make the //new table reference or point to the old table variable ‘table’

table = null;

table = table1;
slideNumber++;
}

Hi John,

I like to share that instead of adding new slide and then adding a table on reaching the bottom of the existing slide when populating table data, you can clone the existing slide with table that you are populating. This way you will have the same table with similar formatting. All you need to do in this case is to traverse the table cells and clear the table data and fill new one. I hope this will be understandable.

Many Thanks,

That would certainly work - except that there ar many other shapes on the slide - not just this table. It just happens that this table is the last shape and when expanded it can creep off the slide and into never never land.

Any other suggestions?

I tried to create a new table on the new slide and then add a cloned row to that new table but i get an error - ‘cloning rows between different tables isn’t supported’.

Here is my source code for that…

//table is the original table
//table1 is the new table - but I need to reference the original table because before the slide runs out of room I have to clone rows to it

if(mitiSteps.size() >= mitiNumber)
{

if(table.getHeight() + table.getY() > target.getSlideSize().getSize().getHeight())
{
// go to next slide
// make another table

ISlide slide222 = target.getSlides().addEmptySlide(target.getLayoutSlides().getByType(SlideLayoutType.Blank));

double[] dblCols = new double[table.getColumns().size()];
double[] dblRows = { table.getRows().get_Item(0).getHeight()};

for (int zzz = 0; zzz < table.getColumns().size(); zzz++)
dblCols[zzz] = table.getColumns().get_Item(zzz).getWidth();

ITable table1 = slide222.getShapes().addTable(200, 250, dblCols, dblRows);

table1.setY(15);

table = null;

table = table1;
slideNumber++;
mitiRowNumber = 0;

}
else
//AddClone adds a row in the end of the table
table.getRows().addClone(tblRow , false);
}

Hi John,

I have observed the information shared by you and like to share that it is not possible to copy the row from one table to another. If you don’t desire to clone the whole slide, you can clone the table only to next slide and then fill that accordingly. Please visit this documentation link to serve the purpose on your end.

Many Thanks,

I need to create a new slide that only contains a table with the same row dimensions as the table on the previous slide.

Do you have a solution to help me out?

So I need a table with a row that is the same dimensions as another table’s first row.

Hi John,

I have observed your requirements and request you to please try using the following sample code on your end to serve the purpose. I have the example will be helpful in serving the purpose on your end.

public static void TestTableClone()
{
String dataDir = “C:\Aspose Data\”;
using (Presentation presentation = new Presentation(dataDir + “TestTable.pptx”))
{
// Get Presentation Shapes & LayoutSlides
IShapeCollection sourceShapes = presentation.Slides[0].Shapes;
ITable srcTable=(ITable)sourceShapes[0];
if (srcTable.Height + srcTable.Y > presentation.SlideSize.Size.Height)
{
ILayoutSlide blankLayout = presentation.Masters[0].LayoutSlides.GetByType(SlideLayoutType.Blank);

// Add Empty Slide and InsertClone
ISlide destSlide = presentation.Slides.AddEmptySlide(blankLayout);

IShapeCollection destShapes = destSlide.Shapes;

// destShapes.AddClone(sourceShapes[1], 50, 150 + sourceShapes[0].Height);
IShape newShape = destShapes.AddClone(sourceShapes[0]);
// destShapes.InsertClone(0, sourceShapes[0], 50, 150);

ITable table = (ITable)newShape;

foreach (IRow row in table.Rows)
{
for (int i = 0; i < row.Count; i++)
{
ICell cell = row[i];
cell.TextFrame.Paragraphs.Clear();
}
}
}
// Write the PPTX file to disk
presentation.Save(dataDir + “CloneShape_out.pptx”, SaveFormat.Pptx);
}
}


Many Thanks,

Ok - I am able to create a new slide with a new table using the code below. But the new table has a blue background with white text. I need to have the same font, font size, font color and background color as the original table. How can I get that information from a table? How can I set that information in the new table?

//are we too far off the page?
if( table.getHeight() + table.getY() + tblRow.getHeight() >= target.getSlideSize().getSize().getHeight())
{
newSlide = true;

// make another table
ISlide slide222 = target.getSlides().addEmptySlide(target.getLayoutSlides().getByType(SlideLayoutType.Blank));

double[] dblCols = new double[table.getColumns().size()];
double[] dblRows = { table.getRows().get_Item(0).getHeight()};

for (int zzz = 0; zzz < table.getColumns().size(); zzz++)
dblCols[zzz] = table.getColumns().get_Item(zzz).getWidth();

tableExtra = slide222.getShapes().addTable(200, 250, dblCols, dblRows);

tblRowExtra = tableExtra.getRows().get_Item(0);

// slide222.getShapes().addClone((IShape)tableExtra);

//the row number is zero for this new table because we do not have the header row
mitiRowNumber = 0;

slideNumber++;

//put first solution here
}

As far as using the code you provided - I have too many ‘shapes’ on my template slide to know which one to clone like that. So cloning will not work. I have to go ahead and create a new table and use that. See my code in my last posting.

I do, however, need to mimic the old table as far as font and background color. The new table is blue for some reason.

Hi John,

I have observed your comments and like to share that in order to avoid the issue of many shapes on a single slide and deciding which one is a table that is to be cloned on next slide, you can use Alternative Text property for the desired shape and then find that shape and cloning that to next slide. Please visit this documentation link for your reference in this regard.

As far as retaining the cloned table textual properties as that of actual source table, I like to share that when you clone the table to next slide, it will have same textual properties as that of source table. The example that I shared with you with blue header row and white text was an example only. So, when this slide was cloned it had same textual properties. You can try cloning your own table in this regard. Please visit this documentation link for more about table manipulations.


Many Thanks,

Thanks - I have this code and it almost works. It creates a new table on the new slide - but the new table does not have the same font - it is bigger. And the table has just as many rows as the original table - but each row and cell are clear. I need to delete all of the rows in the new table - not just clear them.

And I’d like the font to be the same.

//target is the presentation
//shape is the original table


ISlide slide222 = target.getSlides().addEmptySlide(target.getLayoutSlides().getByType(SlideLayoutType.Blank));

IShapeCollection destShapes = slide222.getShapes();

IShape newShape = destShapes.addClone(shape);

tableExtra = (ITable)newShape;

for (int zzz = 0; zzz < tableExtra.getRows().size(); zzz++)
{
IRow row = tableExtra.getRows().get_Item(zzz);
for (int kk = 0; kk < row.size(); kk++)
{
ICell cell2 = row.get_Item(kk);
cell2.getTextFrame().getParagraphs().clear();
}
}

tblRowExtra = tableExtra.getRows().get_Item(0);

I now have the table cloned and in place but I need to remove all the orws and start from scratch. This source code cashes when it gets to the 7th row - I think it might be trying to remove the cells? The talbe has 6 columns and that seems like a cooincidence…

//table is the original ITable

//If I use the cloned table iterator and try to remove thr rows inside a loop I get //an error that says the collection has been modified…

Iterator bbb = table.getRows().iterator();
int numRows = 0;
while (bbb.hasNext())
numRows++;

for (int zzz = 0; zzz <numRows; zzz++)
tableExtra.getRows().removeAt(zzz, false);



Hi John,

I have observed the issue. Actually, you are one end removing the rows and thus decreasing the rows count actually in collection. Where as on other hand you have set the counter numRows to actual value of rows before removing any. For this reason you are getting exception. Please try using following alternate on your end to serve the purpose.

for (int zzz = 0; zzz < numRows; zzz++)
{
table.Rows.RemoveAt(0, false);

}


Many Thanks,