替换段落打开XML中的文本
本文关键字:文本 XML 段落打 替换 | 更新日期: 2023-09-27 17:50:49
我在幻灯片中有多个段落占位符<<>>,我需要用实际文本替换占位符,但是替换后,如果我用打开的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;
}
使用openxml sdk来反映代码对于检查丢失的东西是相当好的,但是代码很难阅读,如果你犯了一个错误复制它可能会导致大量的空闲时间损失。
所以基本上你必须知道openxml元素的结构对于word/powerpoint来说是一样的。它是paragraph|run|text这里我忽略了段落的父元素,在word中大多数时候是TextBody。
首先,您必须在Run elements中找到您的搜索模式。假设文本没有被分割成多个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));
}
}
如果模式被分割成多个运行,您将不得不迭代所有段落运行,并在找到整个模式后将每个运行文本添加到局部变量中,您可以替换那些运行文本。我认为你会成功的。