1

I need to add a style to a paragraph using OpenXML. I have the following code and it's running but, the style is not the one I am trying to add.

var file = WordprocessingDocument.Open(fileName, true); 

var text = new Text("Hello world");
var run = new Run(text);
var paragraph = new Paragraph(run);
paragraph.ParagraphProperties = new ParagraphProperties(
new ParagraphStyleId() { Val = "Body Text" });            

file.MainDocumentPart.Document.Body.AppendChild(paragraph);
file.MainDocumentPart.Document.Save();

Is there something that I am doing wrong??? How to add some of the existent styles to a paragraph using OpenXML.

Yanet Francisco
  • 147
  • 4
  • 19
  • How do you define `"Body Text"` template? – FortyTwo Jun 05 '17 at 10:05
  • I don't have a definition for Body Text, I was thinking that the styles was like the Font that you just put the name of the font that you need. I have a design in Word and I need to create the code that has the same format, so I was trying to use in some paragraph the style that the paragraph has in the original document. – Yanet Francisco Jun 05 '17 at 12:50
  • Check this [post](https://stackoverflow.com/a/20197959/3110695) – FortyTwo Jun 05 '17 at 14:12
  • [This post](https://stackoverflow.com/questions/25056927/unable-to-use-existing-paragraph-styles-in-open-xml/25058393#25058393) might be of some use to you as well. – petelids Jun 05 '17 at 16:19

1 Answers1

1

As illustrated here

private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, 
    string styleid, string stylename)
{
    // Get access to the root element of the styles part.
    Styles styles = styleDefinitionsPart.Styles;

    // Create a new paragraph style and specify some of the properties.
    Style style = new Style() { Type = StyleValues.Paragraph, 
        StyleId = styleid, 
        CustomStyle = true };
    StyleName styleName1 = new StyleName() { Val = stylename };
    BasedOn basedOn1 = new BasedOn() { Val = "Normal" };
    NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" };
    style.Append(styleName1);
    style.Append(basedOn1);
    style.Append(nextParagraphStyle1);

    // Create the StyleRunProperties object and specify some of the run properties.
    StyleRunProperties styleRunProperties1 = new StyleRunProperties();
    Bold bold1 = new Bold();
    Color color1 = new Color() { ThemeColor = ThemeColorValues.Accent2 };
    RunFonts font1 = new RunFonts() { Ascii = "Lucida Console" };
    Italic italic1 = new Italic();
    // Specify a 12 point size.
    FontSize fontSize1 = new FontSize() { Val = "24" };
    styleRunProperties1.Append(bold1);
    styleRunProperties1.Append(color1);
    styleRunProperties1.Append(font1);
    styleRunProperties1.Append(fontSize1);
    styleRunProperties1.Append(italic1);

    // Add the run properties to the style.
    style.Append(styleRunProperties1);

    // Add the style to the styles part.
    styles.Append(style);
}
Tacy Nathan
  • 344
  • 1
  • 6
  • 15