Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add style to Docx in Novacode

Tags:

c#

docx

Using Novacode's DocX, I can add a paragraph with Heading 1 style like this:

var p = docX.InsertParagraph("My Heading");
p.StyleName = "Heading1";

But I can't add a "Title" style:

var p = docX.InsertParagraph("My Heading");
p.StyleName = "Title";

I looked in the generated Xml files, and I see the "Title" style is not in the Styles.xml file, whereas if I set it as Title style in Word and save, then the Title style appears in the styles Xml file.

So how do I either get DocX to include the Title style, or how do I go about adding a style to the DocX styles?

like image 409
Sean Avatar asked Mar 27 '15 10:03

Sean


1 Answers

I meet this problem too. By comparing word\document.xml and word\styles.xml files in the docx(zipped)file. I found that the Paragraph.StyleName should be assigned with the style id not the style name !

for example: one style in styles.xml:

<w:style w:type="paragraph" w:customStyle="1" w:styleId="a9">
    <w:name w:val="mystyle" /> 

You should assign "a9" not "mystyle" to Paragraph.StyleName to get the correct style.

Hope helpful.

like image 108
xwing Avatar answered Sep 22 '22 03:09

xwing