c# – 如何将字符样式应用于文字处理文档中的运行?

当我在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>

风格没有适用于我的跑步!

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

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

最佳答案 基于
ECMA specification for OpenXML,为了设置段落中的任何运行样式,您还必须将样式应用于段落标记:

17.3.1.29 rPr (Run Properties for the Paragraph Mark)

This element specifies the set of run properties applied to the glyph used to
represent the physical location of the paragraph mark for this
paragraph. This paragraph mark, being a physical character in the
document, can be formatted, and therefore shall be capable of
representing this formatting like any other character in the
document. If this element is not present, the paragraph mark is
unformatted, as with any other run of text.

所以要在你的代码中解决这个问题..试试这个..

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具有自己的属性部分:

Just as a paragraph can have properties, so too can a run. All of the
elements inside an r element have their properties controlled by a
corresponding optional rPr run properties element (§17.7.9.1;
§17.3.2.27), which shall be the first child of the r element. In
turn, the rPr element is a container for a set of property elements
that are applied to the rest of the children of the r element. [Note:
The elements inside the rPr container element allow the consumer to
control whether the content in the following run content is bold,
underlined, or visible, for example. end note]

希望这可以帮助.

点赞