Reza M.A
Reza M.A

Reputation: 1207

How to apply a character style to a run in a wordprocessing document?

When I create a paragraph style with openxml sdk 2 in c# and apply it to a paragraph every thing will be correct and it will run without any problem.

But with the codes below, when I create a character style and apply it to a run it make no change to the runs of document:

Codes below will create and save the style to style part of document:

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

And below codes are something I wrote to apply the style to a run:

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

And these are the output code of 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>

The style didn't applied to my run!

And at the end, this is the screen-shot of the style gallery when I open the outputted document, this picture show that the style has been added successfully to the document but it didn't apply to run:

picture of style in documnet

How can I apply a character style to a run?

Upvotes: 4

Views: 4453

Answers (1)

Flowerking
Flowerking

Reputation: 2581

Based on the ECMA specification for OpenXML, in order to style any runs in the paragraph you have to apply style to the paragraph mark as well :

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.

So to fix this in your code.. try this..

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

Update:

Based on your open xml snippet in the comment, you forgot to include

RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};
    
 run1.Append(rprRun1); //Adding run properties to the run

in your code. It is also required to apply properties to the run seperately in order to get the formatting applied to the run element as run has its own properties section:

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]

Hope this helps.

Upvotes: 3

Related Questions