替换Paragraph Open XML中的文本

时间:2015-05-27 09:00:37

标签: c# powerpoint openxml

我在幻灯片中有多个段落和占位符<<>>,我需要用实际文本替换占位符,但是在替换后如果我用open xml SDK检查文件,我看到下面的行

 A.Run run54 = new A.Run();
 OpenXmlUnknownElement openXmlUnknownElement2 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<p:text xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">Placeholder replaced Text</p:text>");
 run54.Append(openXmlUnknownElement2);

如果我实际打开文件,则会出现修复对话框并且内容消失

下面是我用来替换文本的代码

  if (paragraph.InnerText.Contains(originalText))
                {
                    D.ParagraphProperties paragraphProperties6 = new D.ParagraphProperties() { Alignment = D.TextAlignmentTypeValues.Center };
                    D.DefaultRunProperties defaultRunProperties24 = new D.DefaultRunProperties();

                    paragraphProperties6.Append(defaultRunProperties24);

                    D.Run run6 = new D.Run();
                    D.RunProperties runProperties8 = new D.RunProperties() { Language = "en-US", FontSize = 2400, Bold = true };
                    runProperties8.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
                    D.SolidFill solidFill72 = new D.SolidFill();

                    D.RgbColorModelHex rgbColorModelHex16 = new D.RgbColorModelHex() { Val = "808080" };
                    D.LuminanceModulation luminanceModulation1 = new D.LuminanceModulation() { Val = 75000 };
                    rgbColorModelHex16.Append(luminanceModulation1);
                    solidFill72.Append(rgbColorModelHex16);
                    runProperties8.Append(solidFill72);
                    string modifiedString = Regex.Replace(paragraph.InnerText, originalText, ReplaceText);
                    paragraph.RemoveAllChildren<Run>();
                    paragraph.AppendChild<Run>(new Run(new DocumentFormat.OpenXml.Presentation.Text(modifiedString)));
                    //D.Text text15 = new D.Text();
                    //text15.Text = new DocumentFormat.OpenXml.Presentation.Text(modifiedString).ToString();
                    run6.Append(runProperties8);
                    //run6.Append(text15);
                    D.EndParagraphRunProperties endParagraphRunProperties7 = new D.EndParagraphRunProperties() { Language = "en-US", FontSize = 2400, Bold = true, Dirty = false };
                    endParagraphRunProperties7.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
                    D.SolidFill solidFill75 = new D.SolidFill();

                    D.RgbColorModelHex rgbColorModelHex19 = new D.RgbColorModelHex() { Val = "808080" };
                    D.LuminanceModulation luminanceModulation4 = new D.LuminanceModulation() { Val = 75000 };

                    rgbColorModelHex19.Append(luminanceModulation4);

                    solidFill75.Append(rgbColorModelHex19);
                    endParagraphRunProperties7.Append(solidFill75);
                    paragraph.Append(paragraphProperties6);
                    paragraph.Append(run6);
                    //paragraph11.Append(break1);
                    paragraph.Append(endParagraphRunProperties7);
                    break;
                }

任何帮助将不胜感激,

1 个答案:

答案 0 :(得分:0)

使用openxml sdk来反映代码是很好的检查丢失的东西,但代码很难读,如果你犯了一个错误,复制它可能导致大量的空闲时间损失。

所以基本上你必须知道openxml元素的结构,对于word / powerpoint来说都是一样的。这里是段落|文本我忽略了大部分时间都是TextBody的段落的父母。

首先,你必须在Run元素中找到你的搜索模式。假设文本没有拆分成多个Run元素。

IEnumerable<Run> runs = Paragraph.Descendants<Run>()
    .Where(el => el.InnerText.Contains("<<>>"));
if(runs != null) {
    foreach(Run run in runs) { 
        // Use this if using powerpoint openxml
        run.Text = new Text(run.InnerText.Replace("<<>>", "your text");

        // Use this if using wordprocessing openxml
        string innerText = run.InnerText.Replace("<<>>", "your text");
        run.RemoveAllChildren<Text>();
        run.AppendChild(new Text(innerText));
    }
}

如果将模式拆分为多个运行,则必须迭代所有段落运行,并在找到可替换这些运行文本的整个模式后将每个运行文本添加到局部变量。我想你会设法做到这一点。