none
Applying pre-defined styles like Heading1, Title, etc to an Open XML Document ... RRS feed

  • Question

  • I cannot get a style associated with a Paragraph to be applied. Code runs fine, but the style is not applied to my text. I've tried opening an existing but empty word document (docx), creating a new one and building it from the ground up, and a few others. Here is the code snippet for using an exiting but empty word (docx) document.

     

             string tempFilename = @"c:\temp\template.docx";

             WordprocessingDocument doc = WordprocessingDocument.Open(tempFilename, true);
             MainDocumentPart mainDocPart = doc.MainDocumentPart;

             Body mainDocBody = mainDocPart.Document.Body; //new Body(); //Crates the Body tag

             // Paragraph consists of a Paragraph, Run, and Text
             Paragraph newParagraph = new Paragraph(
                        new ParagraphProperties(
                            new ParagraphStyleId() { Val = "Title" }),
                        new Run(
                            new Text("Title Text Is Here")));

             doc.MainDocumentPart.Document.Body.Append(newParagraph);
             doc.MainDocumentPart.Document.Save();

    Tuesday, June 15, 2010 6:32 PM

Answers

  • Hi CobbDev,

    Thanks for your question.

    Agree with AbeML, you need to add a definition for style "Title" first. Here is a simple example on how to add a custom style definition:

    private

     

    static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart1) 

     

    Styles styles1 = styleDefinitionsPart1.Styles;

     

    Style style1 = new Style() { Type = StyleValues.Paragraph, StyleId = "MyStyle", CustomStyle = true };

     

    StyleName styleName1 = new StyleName() { Val = "MyStyle" };

     

    BasedOn basedOn1 = new BasedOn() { Val = "Normal" };

     

    StyleRunProperties styleRunProperties1 = new StyleRunProperties();

     

    Bold bold1 = new Bold();

     

    Color color1 = new Color() { Val = "EEECE1", ThemeColor = ThemeColorValues.Background2 };

     

    RunFonts font1 = new RunFonts() { Ascii = "Times New Roman" };

     

    You could use the following code to get the StyleDefinitionPart:

    StyleDefinitionsPart

     

    styleDefPart = document.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>().FirstOrDefault();

    Hope this helps.

    Thanks,

    Lu

     

    styleRunProperties1.Append(bold1);

    styleRunProperties1.Append(color1);

    styleRunProperties1.Append(font1);

    style1.Append(styleName1);

    style1.Append(basedOn1);

     

    style1.Append(styleRunProperties1);

    styles1.Append(style1);

    }

     

     

     

     

     

     

     

     

     

    {

     

    Friday, June 18, 2010 6:26 AM

All replies

  • Hi CobbDev,

     

    The problem here is that when starting from a blank word document the styles are not added. In open xml format for word, the styles are stored in a separate place /word/styles.xml All of the styles are stored here and then as you correctly point out in the code the Paragraph.ParagraphProperty.ParagraphStyleID.val references the style that is there in the styles.xml file.

     

    In your case, when starting from a blank document, the style "Title" is not present in the Styles.xml file. If you want to use the "Title" style then you first need to create this in the styles.xml file.

    Following is defined as the Word app "Title" style - 

     

     <w:style w:type="paragraph" w:styleId="Title">
     <w:name w:val="Title" />
     <w:basedOn w:val="Normal" />
     <w:next w:val="Normal" />
     <w:link w:val="TitleChar" />
     <w:uiPriority w:val="10" />
     <w:qFormat />
     <w:rsid w:val="006A1FA3" />
     <w:pPr>
      <w:pBdr>
      <w:bottom w:val="single" w:color="4F81BD" w:themeColor="accent1" w:sz="8" w:space="4" />
      </w:pBdr>
      <w:spacing w:after="300" w:line="240" w:lineRule="auto" />
      <w:contextualSpacing />
     </w:pPr>
     <w:rPr>
      <w:rFonts w:asciiTheme="majorHAnsi" w:hAnsiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:cstheme="majorBidi" />
      <w:color w:val="17365D" w:themeColor="text2" w:themeShade="BF" />
      <w:spacing w:val="5" />
      <w:kern w:val="28" />
      <w:sz w:val="52" />
      <w:szCs w:val="52" />
     </w:rPr>
     </w:style>
    In the word application, when you start from a blank document and then add some text and then apply the "Title" style, word application, behind the scenes, creates this styles definition in the styles.xml file. You will have to do the same too...

     

    Another simpler approach to solve your problem is to use bookmarks. So, you use the word application and create a bookmark. Then apply the required style on the bookmark.

    Then while adding text to the word document using the Open Office XML SDK, just find the bookmark and then replace it with the text you want. This way the style is applied as it is already defined...

    Hope this helps...

    -abe

     

     

     

     

    Tuesday, June 15, 2010 10:03 PM
  • Hi CobbDev,

    Thanks for your question.

    Agree with AbeML, you need to add a definition for style "Title" first. Here is a simple example on how to add a custom style definition:

    private

     

    static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart1) 

     

    Styles styles1 = styleDefinitionsPart1.Styles;

     

    Style style1 = new Style() { Type = StyleValues.Paragraph, StyleId = "MyStyle", CustomStyle = true };

     

    StyleName styleName1 = new StyleName() { Val = "MyStyle" };

     

    BasedOn basedOn1 = new BasedOn() { Val = "Normal" };

     

    StyleRunProperties styleRunProperties1 = new StyleRunProperties();

     

    Bold bold1 = new Bold();

     

    Color color1 = new Color() { Val = "EEECE1", ThemeColor = ThemeColorValues.Background2 };

     

    RunFonts font1 = new RunFonts() { Ascii = "Times New Roman" };

     

    You could use the following code to get the StyleDefinitionPart:

    StyleDefinitionsPart

     

    styleDefPart = document.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>().FirstOrDefault();

    Hope this helps.

    Thanks,

    Lu

     

    styleRunProperties1.Append(bold1);

    styleRunProperties1.Append(color1);

    styleRunProperties1.Append(font1);

    style1.Append(styleName1);

    style1.Append(basedOn1);

     

    style1.Append(styleRunProperties1);

    styles1.Append(style1);

    }

     

     

     

     

     

     

     

     

     

    {

     

    Friday, June 18, 2010 6:26 AM