如何在文字处理文档中将字符样式应用于一段文字?

4
当我使用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>

我的运行中没有应用样式!

最后,这是输出文档打开时样式库的屏幕截图,该图片显示样式已成功添加到文档中但未应用于运行:

文档中样式的图片

我如何将字符样式应用于运行?

1个回答

3
基于ECMA的OpenXML规范,要为段落中的任何运行应用样式,必须同时将样式应用于段落标记:

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);

更新:

根据您在评论中提供的开放式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 容器元素内的元素允许使用者控制以下运行内容是否加粗、下划线或可见等。结束注意]

希望这能帮到你。


1
感谢@Flowerking,这是我按照您的建议更改代码后的输出:`<?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="" rel = "nofollow noreferrer">http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:body> <w:p> <w:pPr> <w:rPr> <w:rStyle w:val="CharacterStyle1" /> </w:rPr> </w:pPr> <w:r> <w:t>THIS IS TEST RUN 1</w:t> </w:r> </w:p> </w:body> </w:document>`但它没有改变run1的样式。 - Reza M.A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接