C# 如何将字符样式应用于字处理文档中的运行?
当我用c#中的OpenXMLSDK2创建一个段落样式并将其应用于段落时,所有内容都将是正确的,它将毫无问题地运行 但使用下面的代码,当我创建字符样式并将其应用于运行时,不会更改文档的运行: 下面的代码将创建样式并保存到文档的样式部分:C# 如何将字符样式应用于字处理文档中的运行?,c#,ms-word,openxml,openxml-sdk,wordprocessingml,C#,Ms Word,Openxml,Openxml Sdk,Wordprocessingml,当我用c#中的OpenXMLSDK2创建一个段落样式并将其应用于段落时,所有内容都将是正确的,它将毫无问题地运行 但使用下面的代码,当我创建字符样式并将其应用于运行时,不会更改文档的运行: 下面的代码将创建样式并保存到文档的样式部分: StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>(); Style style = new Style(
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);
<?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>
这是document.xml的输出代码:
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);
<?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>
这是测试运行1
这种风格不适用于我的跑步
最后,这是我打开输出文档时样式库的屏幕截图,这张图片显示样式已成功添加到文档中,但未应用于运行:
如何将字符样式应用于段落?基于,要设置段落中任何段落的样式,您还必须将样式应用于段落标记:
17.3.1.29 rPr(段落标记的运行属性)
此元素指定应用于用于显示的图示符的运行属性集
表示此段落标记的物理位置
段落本段标记,是本段中的一个物理字符
文件,可以格式化,因此应能够
将此格式表示为中的任何其他字符
文件。如果此元素不存在,则段落标记为
未格式化,与任何其他文本运行一样
因此要在代码中修复此问题。。试试这个..
Paragraph heading = new Paragraph();
ParagraphProperties headingPPr = new ParagraphProperties();
heading.Append(headingPPr);
ParagraphMarkRunProperties headingParagraphMarkRunProperties = new ParagraphMarkRunProperties();
RunStyle runStyle1 = new RunStyle(){ Val = "CharacterStyle1" };
headingParagraphMarkRunProperties.Append(runStyle1);
headingPPr.Append(headingParagraphMarkRunProperties);
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"}};
run1.Append(rprRun1);
heading.Append(run2);
body.Append(heading);
更新:
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);
<?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>
根据注释中打开的xml片段,您忘记了包括
RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};
run1.Append(rprRun1); //Adding run properties to the run
在你的代码中。由于run
有自己的属性部分,还需要将属性分别应用于run,以便将格式应用于run
元素:
正如一段文字可以有属性一样,一段文字也可以有属性。所有的
r元素中的元素的属性由
相应的可选rPr运行特性元素(§17.7.9.1;
§17.3.2.27),应为r元素的第一个子元素。在里面
反过来,rPr元素是一组属性元素的容器
应用于r元素其余子元素的。[注:
rPr容器元素内的元素允许使用者
控制以下运行内容中的内容是否为粗体,
例如,下划线或可见。尾注]
希望这能有所帮助。谢谢@Flowerking,这是我按照您所说的更改代码时的输出:
这是测试运行1
,但它没有更改运行1的样式