Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So I am working on code that generates word documents, and I really want to be able to make a paragraph not just look like, but be the same style as what you would get if you set it to the Title style that is available in the default set of styles Word's GUI provides.

Everything I find leaves me feeling like I am going to have to programatically define and then add styles to my document for each one. Doing that however will make those styles essentially duplicates of the ones already in Word if someone opens up the file.

I will be very grateful to anyone who has good information on how to do this. It seems like it should be doable, but I am still a newb with this SDK.

thanks,

Danny
Posted

1 solution

Are you complete generating the Word doc in code?

If so, the paragraph style is set pretty easily:

Paragraph paragraph1 = new Paragraph() 
{ 
  RsidParagraphAddition = "00CB5EB1", RsidParagraphProperties = "00020731", RsidRunAdditionDefault = "00020731" 
};
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Title" };
paragraphProperties1.Append(paragraphStyleId1);


The lines above the StyleID lines are just the paragraph identifier, not specific to the style, and will change for each paragraph. I just left them in there for context.

The paragraphStyleId is the "important" part. :)
 
Share this answer
 
v2
Comments
DannyStaten 30-Mar-11 10:14am    
Thanks. I am generating my style completely in code so this should be perfect. I will be sure to give it a try as soon as I get home from work being that this is a personal project I am doing on the side 8).
DannyStaten 30-Mar-11 21:55pm    
So far I have been trying what you are suggesting and it isn't doing anything.

Paragraph chapterHeader = new Paragraph(new Run(new Text(chapter.Name)));
ParagraphProperties props = new ParagraphProperties();
ParagraphStyleId pStyle = new ParagraphStyleId() { Val = "Title" };
props.Append(pStyle);
chapterHeader.PrependChild(props);
buildingBody.Append(chapterHeader);

That is resulting in xml that appears identical, but when I open my word document there is no style applied.

I am not assigning the rsids on my paragraphs. I guess I don't yet understand their purpose, but that wouldn't be part of what the current problem is I assume.
DannyStaten 30-Mar-11 22:45pm    
So I found something that lets me do what I was trying for, and may be something you were assuming I was already doing. A blog post here http://www.labo-dotnet.com/post/Create-Word-document-using-the-OpenXML-20-SDK.aspx shows how to pull a style definition from an existing document, include it in your project and then load that styles.xml in as the styles part for your programatically created docx. It saves me the trouble of having to define my styles programatically, and once I have done that, the styles do work as they are applied. A note to any who do that: You must extract the styles you want from a document that uses them explicitly in it's contents or the styles.xml won't define that style.
GenJerDan 30-Mar-11 23:21pm    
Ah. I guess they need to be embedded somewhere "upstream" of them being applied. Good to know.
Actually, the easiest way to do all this is to design your doc in Word, save it, then use the Document Reflector to deconstruct it into a C# class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900