我在幻灯片中有多个段落带有占位符 <<>>,我需要用实际文本替换占位符,但是替换后如果我使用打开的 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 来反射(reflect)代码非常适合检查丢失的东西,但代码很难阅读,如果你在复制它时犯了一个错误,可能会导致大量空闲时间损失。
所以基本上你必须知道 openxml 元素的结构,这对于 word/powerpoint 都是一样的。这里是 paragraph|run|text 我忽略了段落的父级,在大多数情况下,它是 TextBody。
首先,您必须在运行元素中找到您的搜索模式。假设文本未拆分为多个 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));
}
}
如果模式被分成多个运行,您将必须迭代所有段落运行,并在找到整个模式后将每个运行文本添加到局部变量,您可以替换那些运行文本。我认为你会设法做到这一点。
关于c# - 替换段落 Open XML 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30477742/