在Powerpoint幻灯片中更改文本框中的文本

Mat*_*raj 6 openxml presentationml

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

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

万分感谢

小智 3

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

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 Code Online (Sandbox Code Playgroud)

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