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

标签 c# ms-word openxml openxml-sdk wordprocessingml

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

风格不适用于我的运行!

最后,这是我打开输出文档时样式库的截图,这张图显示样式已经成功添加到文档中,但没有应用到运行:

picture of style in documnet

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

最佳答案

基于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]

希望这对您有所帮助。

关于c# - 如何将字符样式应用于字处理文档中的运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917747/

相关文章:

c# - 如何从列表中获取随机项目

c# - 发送给最终用户的报告的文档布局

c# - 如何限制对 MVC5 中某些操作的访问?

Java XWPF,设置段落悬挂缩进

c# - 你能在 ClosedXML (C#) 中填充一个范围吗

c# - 是否有任何通用的 Parse() 函数可以使用解析将字符串转换为任何类型?

c# - VSTO Word 加载项 user.config 每次更新都丢失

c# - 段落列表不包含完整内容

.net - 在哪里可以找到 Office open xml 完整引用?

c# - 使用 Open XML 创建 word 文档