【问题标题】:Change text in a textbox in Powerpoint slide在 Powerpoint 幻灯片中更改文本框中的文本
【发布时间】:2012-05-04 05:07:29
【问题描述】:

我有一个包含 3 张幻灯片的 Powerpoint 演示文稿。每张幻灯片都有一个文本框,它是一个占位符。我想替换一张幻灯片上的文本框内容。

我需要知道如何使用 C# 和 OpenXML 来做到这一点

非常感谢

【问题讨论】:

  • 你搞定了吗?请问您可以发布解决方案吗?

标签: openxml presentationml


【解决方案1】:

对每张要更改的幻灯片执行此操作:

ODP.ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
        foreach (ODP.Shape shape in tree.Elements<ODP.Shape>())
        {
            // Run through all the paragraphs in the document
            foreach (ODD.Paragraph paragraph in shape.Descendants().OfType<ODD.Paragraph>())
            {
                foreach (ODD.Run run in paragraph.Elements<ODD.Run>())
                {
                    if (run.Text.InnerText.Contains("PLACEHOLDER"))
                    {
                        run.Text = new ODD.Text("Your new text");
                    }
                }
            }
        }

请记住,如果您的模板的占位符包含空格,这可能会创建两个单独的运行元素。因此,您可能会得到一个 run.text 为 "Place" 的 run 和另一个 run.Text "holder" 的 run 元素,而不是一个 run.Text 为“Place holder”的元素。

【讨论】:

  • 我有一个场景,我只有段落,里面没有子元素。我尝试在其中添加运行和文本元素仍然不起作用。任何帮助表示赞赏
  • 当我这样做并保存文件时,更改的文本不存在。如果我在调试器中检查run.Text,它会显示更新后的文本。
  • 你应该真正定义你的用途。我通过反复试验发现 ODP.Shape 表示 DocumentFormat.OpenXml.Presentation.Shape,而不是 DocumentFormat.OpenXml.Drawing.Shape。
猜你喜欢
  • 1970-01-01
  • 2020-11-10
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 2015-07-27
  • 1970-01-01
相关资源
最近更新 更多