In a previous article I have shown how to use an existing heading style. But this requires an existing template document with the according style definition. Maybe you don’t have such a document or don’t want to use one. In this case you can define and use you own heading styles.
The following source code shows how to define a simple style. Of course you may set additional parameters to adapt the style to your needs. As described in the previous article each style has an ID and a name. So you can create a new style and set the according ID and a name. At next the style can be used to define the StyleRunProperties. In this example I have set a bold text with a defined size and font.
var style = new Style() { Type = StyleValues.Paragraph, StyleId = "Heading1", CustomStyle = true, Default = false }; style.Append(new StyleName() { Val = "Heading 1" }); var styleRunProperties = new StyleRunProperties(); styleRunProperties.Append(new Bold()); styleRunProperties.Append(new RunFonts() { Ascii = "Arial" }); styleRunProperties.Append(new FontSize() { Val = "24" }); style.Append(styleRunProperties);
After you have created the style, like shown above, you may use it for your heading text. To do so you have to set the style ID in the ParagraphStyleId element and assign it to the ParagraphProperties.
paragraph.ParagraphProperties = new ParagraphProperties( new ParagraphStyleId() { Val = "Heading1" });
The following source code shows the complete example. As you can see you have to create a StyleDefinitionsPart within you main document. This part is used as container for all style definition. Furthermore you have to create a Styles element and assign it to the styles definition part.
//file name string folder = @"<my folder>"; string fileName = folder + @"\Test.docx"; //create document using (var file = WordprocessingDocument.Create( fileName, WordprocessingDocumentType.Document)) { //create document part file.AddMainDocumentPart(); //create styles definitions part StyleDefinitionsPart styleDefinitions = file.MainDocumentPart.AddNewPart<StyleDefinitionsPart>(); //create styles var styles = new Styles(); styles.Save(styleDefinitions); styles = styleDefinitions.Styles; //create style var style = new Style() { Type = StyleValues.Paragraph, StyleId = "Heading1", CustomStyle = true, Default = false }; style.Append(new StyleName() { Val = "Heading 1" }); var styleRunProperties = new StyleRunProperties(); styleRunProperties.Append(new Bold()); styleRunProperties.Append(new RunFonts() { Ascii = "Arial" }); styleRunProperties.Append(new FontSize() { Val = "24" }); style.Append(styleRunProperties); styles.Append(style); //set content var text = new Text("Hello world"); var run = new Run(text); var paragraph = new Paragraph(run); paragraph.ParagraphProperties = new ParagraphProperties( new ParagraphStyleId() { Val = "Heading1" }); var body = new Body(paragraph); var document = new Document(body); //save content file.MainDocumentPart.Document = document; file.MainDocumentPart.Document.Save(); }