当我在 c# 中使用 openxml sdk 2 创建段落样式并将其应用到段落时,一切都是正确的,并且它将毫无问题地运行。
但是使用下面的代码,当我创建字符样式并将其应用于运行时,它不会更改文档的运行:
下面的代码将创建样式并将其保存到文档的样式部分:
StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();
Style style = new Style()
{
Type = StyleValues.Character,
CustomStyle = true,
StyleId = "CharacterStyle1"
};
LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "LinkedStyle" };
style.Append(linkedStyle1);
style.Append(new Name() { Val = "CharacterStyle1" });
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
Color color = new Color() { Val = "FF0000" };
RunFonts font1 = new RunFonts() { ComplexScript = "Tahoma" };
styleRunProperties1.Append(color);
styleRunProperties1.Append(font1);
styleRunProperties1.Append(new Bold());
styleRunProperties1.Append(new FontSize() { Val = "48" });
style.Append(styleRunProperties1);
stylePart.Styles = new Styles();
stylePart.Styles.Append(style);
下面的代码是我编写的将样式应用于运行的代码:
Paragraph heading = new Paragraph();
ParagraphProperties headingPPr = new ParagraphProperties();
heading.Append(headingPPr);
Run run1 = new Run();
Text textRun1 = new Text("THIS IS TEST RUN 1");
run1.Append(textRun1);
RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};
heading.Append(run1);
body.Append(heading);
这些是document.xml的输出代码:
<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr />
<w:r w:rsidRPr="009531B2">
<w:t>THIS IS TEST RUN 1</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
这种风格不适用于我的跑步!
最后,这是我打开输出文档时样式库的屏幕截图,这张图显示样式已成功添加到文档中,但未申请运行:
如何将字符样式应用于跑步?