If somebody like the post and its helpful in your work then, add comments.

Thursday, July 16, 2009

Open XML : Styles in the Word document

Styles are the important part of the word document. When we create paragraph or link or other component in the word document we have to set some styles on that components like Hyperlink,Heading1,23.. or Title etc.

For this we have to do some exercise like as follows :

1. Create a word documents
2. Insert all the styles like in image.


3. Save the document.
4. Rename the document(DocName.docx to DocName.zip)
5. Extract the document.
6. Go to DocName\Word folder


7. Copy the styles.xml and paste in the solution.
8. Write the below code to use in our solution

StyleDefinitionsPart styleDefinitionsPart = mainPart.AddNewPart();

//File Styles Uploading : Location of style c:\\styles.xml
FileStream stylesTemplate = new FileStream("c:\\styles.xml", FileMode.Open, FileAccess.Read);
styleDefinitionsPart.FeedData(stylesTemplate);
styleDefinitionsPart.Styles.Save();
9. When we want to append Heading 1 with our para then we can use like :

Paragraph AuthorPara = new Paragraph();
Run run = new Run();
Text TextLine = new Text("Created by: " + System.Environment.UserName);
run4.Append(TextLine);
AuthorPara.Append(new ParagraphProperties (new ParagraphStyleId (){Val="Heading1"}), run);

This is the way we can append styles to our created document.

4 comments:

  1. Does it work if i change my fonts in Word?

    ReplyDelete
  2. Well for the change in the font, you just have to update the styles.xml file.

    style.xml file have all the current styles of the document.

    ReplyDelete
  3. how to export info path form in excel on button click when it is upload in share point 2010

    ReplyDelete