当内容从当前幻灯片流出时,如何使用 Open XML SDK 2.0 将表格拆分为新的 PowerPoint 幻灯片

发布于 2024-08-29 05:24:47 字数 2452 浏览 3 评论 0原文

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

在其中一张幻灯片上,我有一个表格,要求是将数据添加到该表格,并在该表格超出幻灯片底部时将该表格跨到多张幻灯片上。我采取的方法是确定表格的高度,如果它超过幻灯片的高度,则将新内容移动到下一张幻灯片中。我已阅读 布莱恩和琼斯 博客介绍了向 PowerPoint 幻灯片添加重复数据,但我的情况略有不同。他们使用以下代码:

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 设置为默认值。这是我计算表格高度的尝试:

   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 中打开时高度才会重新调整。

我还尝试访问行中最大表格单元格的高度,但似乎找不到执行该任务的正确属性。

我的问题是,如何确定动态添加的表格行的高度,因为在 PowerPoint 中打开行之前,它似乎不会更新行的高度?使用 Open XML SDK 2.0 时,还有其他方法可以确定何时将内容拆分到另一张幻灯片吗?我愿意接受任何关于某人可能采取的更好方法的建议,因为关于这个主题的文档并不多。

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.

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

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

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 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-09-05 05:24:47

这是一个非常好的问题。您可以做的一件事是测量 System.Drawing.Text 中字体的高度和宽度,并在代码中创建一种预渲染器,以确定文本是否会导致表格流动-屏幕。需要跟踪一些信息,例如字体将以什么宽度换行并创建新行,然后在行和单元格边缘之间留出空间。这将是一个运行总计,通过表格可以包含的行数、字体、大小以及插入的文本来跟踪表格高度,并且仍然保持在幻灯片画布的范围内。但是,一旦您掌握了所有这些,您就可以很好地了解是否需要新幻灯片。

这是一篇学习如何测量 .NET 中渲染文本的好文章:http://www.devsource.com/c/a/Languages/Text-Metrics-in-the-Net-Framework-Part-I/

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.

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文