当我使用OpenXML SDK 2在C#中创建段落样式并将其应用于段落时,一切都会正确无误,并且可以正常运行。
但是,使用下面的代码,当我创建字符样式并将其应用于运行时,它对文档运行不会产生任何改变:
下面的代码将创建并保存样式到文档的样式部分:
以下是我编写的代码,用于将样式应用到运行中:
但是,使用下面的代码,当我创建字符样式并将其应用于运行时,它对文档运行不会产生任何改变:
下面的代码将创建并保存样式到文档的样式部分:
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>
我的运行中没有应用样式!
最后,这是输出文档打开时样式库的屏幕截图,该图片显示样式已成功添加到文档中但未应用于运行:
我如何将字符样式应用于运行?