当内容从当前幻灯片流出时如何使用Open XML SDK 2.0将表拆分为新的PowerPoint幻灯片 [英] How to split table to new PowerPoint slide when content flows off current slide using Open XML SDK 2.0

查看:114
本文介绍了当内容从当前幻灯片流出时如何使用Open XML SDK 2.0将表拆分为新的PowerPoint幻灯片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量数据需要从网站导出到PowerPoint演示文稿,并且一直在使用Open XML SDK 2.0执行此任务.我有一个PowerPoint演示文稿,正在通过Open XML SDK 2.0 Productivity Tool进行演示,以生成可用于重新创建导出的模板代码.

I have a bunch of data that I need to export from a website to a PowerPoint presentation and have been using Open XML SDK 2.0 to perform this task. I have a PowerPoint presentation that I am putting through Open XML SDK 2.0 Productivity Tool to generate the template code that I can use to recreate the export.

在其中一张幻灯片上,我有一张桌子,要求是将数据添加到该桌子上,如果桌子超过了桌子的底部,请将该桌子分成多张桌子.我采用的方法是确定桌子的高度,如果它超过了幻灯片的高度,请将新内容移动到下一张幻灯片中.我已阅读 Bryan和Jones 关于在PowerPoint幻灯片上添加重复数据的博客,但是我的情况有所不同.他们使用以下代码:

On one of those slides I have a table and the requirement is to add data to that table and break that table across multiple slides if the table exceeds the bottom of the slide. The approach I have taken is to determine the height of the table and if it exceeds the height of the slide, move that new content into the next slide. I have read Bryan and Jones blog on adding repeating data to a PowerPoint slide, but my scenario 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++;

这对我不起作用,因为他们知道将表格行设置为什么高度,因为它将是图像的高度,但是当添加不同数量的文本时,我不知道提前的高度,所以我只需将tr.Height设置为默认值即可.这是我尝试确定桌子高度的尝试:

This won't work for me since they know what height to set the table row to since it will be the height of the image, but when adding in different amounts of text I do not know the height ahead of time so I just set tr.Heightto a default value. Here is my attempt at figuring at the table height:

   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);

以下是辅助方法:

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;
}

这可以将新表行正确地添加到现有表中,但是当我尝试获取表的高度时,它将返回原始高度,而不是新高度.新高度表示我最初设置的默认高度,而不是插入大量文本后的高度.似乎只有在PowerPoint中打开高度时,高度才会重新调整.

This correctly adds the new table row to the existing table, but when I try and get the height of the table, it returns the original height and not the new height. The new height meaning the default height I initially set and not the height after a large amount of text has been inserted. It seems the height only gets readjusted when it is opened in PowerPoint.

我还尝试访问该行中最大的表格单元的高度,但是似乎找不到合适的属性来执行该任务.

I have also tried accessing the height of the largest table cell in the row, but can't seem to find the right property to perform that task.

我的问题是,如何确定动态添加的表行的高度,因为在PowerPoint中打开该行之前,它似乎不会更新该行的高度吗?确定使用Open XML SDK 2.0时何时将内容分割到另一张幻灯片的其他方法吗?由于有关此主题的文档不多,因此我对任何人可能采取的更好方法的建议都持开放态度.

My question is how do you determine the height of a dynamically added table row since it doesn't seem to update the height of the row until it is opened in PowerPoint? Any other ways to determine when to split content to another slide while using Open XML SDK 2.0? I'm open to any suggestion on a better approach someone might have taken since there isn't much documentation on this subject.

推荐答案

这是一个非常好的问题.您可以做的一件事是测量System.Drawing.Text中字体的高度和宽度,并在代码中创建一种预渲染器,以弄清文本是否会使表格流出屏幕.会有一些需要跟踪的地方,例如字体将以多大的宽度换行并创建新的行,然后在行与单元格的边缘之间留出间隔.跟踪表格的高度将是一个连续的总和,它可以根据字体,字体的大小和插入的文本的总行数来跟踪表格的高度-并且仍在幻灯片画布的范围之内.但是,一旦您拥有了所有这些功能,它应该可以让您很好地了解是否需要一张新的幻灯片.

This is a really great question. One thing you can do is measure the height and width of the fonts in System.Drawing.Text and create a sort of pre-renderer in code to figure out if the text will cause the table to flow off-screen. There would be a bit to keep track of, like at what width will the fonts wrap and create a new line and then space between the lines and the margin of the cells. It would be a running total to keep track of the table height by the total number of lines it could contain with your font and it's size and your text plugged-in - and still stay within the bounds of the slide canvas. But once you have all of this, it should give you very good insight into whether or not you need a new slide.

这是一篇很好的文章,旨在学习如何在.NET中测量渲染的文本:

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

这篇关于当内容从当前幻灯片流出时如何使用Open XML SDK 2.0将表拆分为新的PowerPoint幻灯片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆