How to split a table into a new PowerPoint slide when content flows from the current slide using the Open XML SDK 2.0

I have a ton of data that I need to export from a website to a PowerPoint presentation and use the Open XML SDK 2.0 to complete this task. I have a PowerPoint presentation that I use in the Open XML SDK 2.0 Performance Tool to generate template code that I can use to recreate the export.

I have a table on one of these slides, and the requirement is to add data to this table and split this table into several slides if the table exceeds the bottom of the slide. The approach I took is to determine the height of the table, and if it exceeds the height of the slide, move this new content to the next slide. I read Brian and Jones 's blog about adding duplicate data to a PowerPoint slide, but my script is a little different. They use the following code:

A.Table tbl = current.Slide.Descendants<A.Table>().First(); A.TableRow tr = new A.TableRow(); tr.Height = heightInEmu; tr.Append(CreateDrawingCell(imageRel + imageRelId)); tr.Append(CreateTextCell(category)); tr.Append(CreateTextCell(subcategory)); tr.Append(CreateTextCell(model)); tr.Append(CreateTextCell(price.ToString())); tbl.Append(tr); imageRelId++; 

This will not work for me, because they know what height to set for the table row, since it will be the height of the image, but when I add different amounts of text, I don’t know the height ahead of time, so I just set tr.Height to the default value. Here is my attempt to figure out the height of the table:

  A.Table tbl = tableSlide.Slide.Descendants<A.Table>().First(); A.TableRow tr = new A.TableRow(); tr.Height = 370840L; tr.Append(PowerPointUtilities.CreateTextCell("This"); tr.Append(PowerPointUtilities.CreateTextCell("is")); tr.Append(PowerPointUtilities.CreateTextCell("a")); tr.Append(PowerPointUtilities.CreateTextCell("test")); tr.Append(PowerPointUtilities.CreateTextCell("Test")); tbl.Append(tr); tableSlide.Slide.Save(); long tableHeight = PowerPointUtilities.TableHeight(tbl); 

Here are the helper methods:

 public static A.TableCell CreateTextCell(string text) { A.TableCell tableCell = new A.TableCell( new A.TextBody(new A.BodyProperties(), new A.Paragraph(new A.Run(new A.Text(text)))), new A.TableCellProperties()); return tableCell; } public static Int64Value TableHeight(A.Table table) { long height = 0; foreach (var row in table.Descendants<A.TableRow>() .Where(h => h.Height.HasValue)) { height += row.Height.Value; } return height; } 

This correctly adds the new table to the existing table, but when I try to get the table height, it returns the original height, not the new height. The new height means the default initial height that I originally set, and not the height after a large amount of text has been inserted. It seems that the height is only adjusted when it opens in PowerPoint.

I also tried to access the height of the largest table cell in the row, but could not find a suitable property to perform this task.

My question is how to determine the height of a dynamically added row in a table, since it does not seem to update the row height before it opens in PowerPoint? Any other ways to determine when to split content into another slide when using the Open XML SDK 2.0? I am open to any suggestions on the best approach that anyone could take, since the documentation on this issue is not enough.

+5
source share
1 answer

This is a really big question. One thing you can do is measure the height and width of the fonts in System.Drawing.Text and create some kind of preliminary rendering in the code to find out if the text will bring to the table screen. It would be a little to track, for example, how wide the fonts would fit and create a new line, and then a space between the lines and cell fields. This would be the total amount to track the height of the table by the total number of lines it could contain with your font, as well as the size and text connected to it, and still remain within the slide canvas. But once you have all this, it should give you a very good idea of ​​whether you need a new slide.

This is a good article to learn how to measure text in .NET: http://www.devsource.com/c/a/Languages/Text-Metrics-in-the-Net-Framework-Part-I/

+2
source

All Articles