I'm working on an export of an HTML file to a Open XML wordfile. If in the HTML <h1>
is used, I want to add a Heading1 style to that part. But somehow when I open the document in Microsoft Word 2010, the Style isn't applied.
If I open the created document in Libre Office, there is some style applied.
I also defined some styles myself, and if I use one of those styles, everything went well in Word and Libre Office.
I opened the Open XML SDK 2.5 Productivity Tool for Microsoft Office and when I look in the example code it provides, it suggests:
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Kop1" };
Kop1 (instead of Heading1) is because my Word is in Dutch so my template is in Dutch, but this didn't solve the problem.
In this code sample I create the paragraph and add style and text to it:
using (wordDocument = WordprocessingDocument.Open(documentStream, true))
{
MainDocumentPart mainPart = wordDocument.MainDocumentPart;
WP.Body body = wordDocument.MainDocumentPart.Document.Body;
WP.Paragraph para = body.AppendChild(new WP.Paragraph());
StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
if (part != null)
{
WP.ParagraphProperties pPr = new WP.ParagraphProperties();
WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Heading1" };
pPr.Append(paragraphStyleId1);
para.Append(pPr);
}
WP.Run run = para.AppendChild(new WP.Run());
run.AppendChild(new WP.Text(value));
}
It has styles defined, because I work with a template, so the code will come inside the if-statement.
I also tried it with the style title (in Dutch titel), tried both but both didn't work...
I really don't understand what's wrong and why I can use my own created styles but can't use one of the predefined styles of Word, which I didn't remove in the template.
It somehow doesn't recognize the predefined styles?
EDIT/ADDITION
I got it working, but not the way I want, I have to click on the predefined styles in the template-document and then save the document. Somehow now after clicking them and saving these styles are added to the document, because if I now generate my final document using the template, I can use the styles. But without clicking the styles in the document first, I can't use them becuase they aren't saved?
Heading1
style name asKop1
and you've tried using this in your actual code i.e.WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Kop1" };
? Can you post the reflection of your actual document (generated, not template) with just a heading in along with actually using word to open your template file and saving out the same type of doc (with just a heading in) and posting the reflection of that too? – Paul Aldred-BannWP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Kop1" };
I also reflected the files what you suggest, and somehow the style: ` <w:style w:type="paragraph" w:styleId="Kop1"> ` is added in the document I created with Word, and this is missing in the generated document. But I don't understand, because it's a default style I can select in Word. – Vincent HogendoorndocumentStream
is that aFile.Open
of your template docx? Also, inStyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
what doespart
contain? Edit - I've just tried your code with one of my templates, and it's working fine, so we're definitely on a style name / doc type issue here. – Paul Aldred-BannstyleDefinitionsPart
part contains all the styles of the document. Because if I don't use a template but an empty document, there isn't a StyleDefinitionsPart in the document and it should be created. I'll try again with a new Template, maybe something is wrong in the templatefile.. – Vincent Hogendoorn