Can anybody guide me how to add predefined styles on paragraph using open XML Word Processing? I have tried various solutions available on forums but nothing works for me. Here is what i want to accomplish:
// Create a document by supplying the filepath.
WordprocessingDocument wordDocument = WordprocessingDocument.Create("E:/Test/Executive.Docx", WordprocessingDocumentType.Document);
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Executive Summary"));
if (para.Elements<ParagraphProperties>().Count() == 0)
para.PrependChild<ParagraphProperties>(new ParagraphProperties());
// Get the ParagraphProperties element of the paragraph.
ParagraphProperties pPr = para.Elements<ParagraphProperties>().First();
// Set the value of ParagraphStyleId to "Heading3".
pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" };
Your technique would totally work if you were editing an existing document. The problem is that a fresh document doesn't have a "Heading 1" predefined. You'd have to add it. So you have two choices:
1. Work with an existing template document
Create a template document (TemplatePath) to use as a base. In the code, copy it to the final destination (FinalPath) and add text/whatever to it, applying styles. Heading 1 will already be in the template.
2. Create your new document from scratch
If you do this, it will have no built-in styles. So create a style, call it "Heading 1" and apply it to your paragraph.
<sarcasm>See? OpenXML is a piece of cake!</sarcasm> I swear, my eyes are rolling so hard I'm getting a headache.
(Sry, my english)
I think the style names is depends your language, what use your word.
Heading 1 in english style id: "Heading 1" in Hungarien: "Címsor 1" -> stlye id: "Cmsor1"
I saw that, the docx xml style file.
How i solove this:
The style hierarchy is very important!
It's work for me table style also.