I get code from here: Docx4j export style from one .docx and use it in another .docx
WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage.load(new java.io.File(System.getProperty("user.dir") + "/template.docx"));
MainDocumentPart tempDocPart = wordMLPackage2.getMainDocumentPart();
StyleDefinitionsPart sdp = tempDocPart.getStyleDefinitionsPart();
Styles tempStyle = sdp.getJaxbElement();
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setJaxbElement(tempStyle);
So I import the style from template.docx, where I created my own Heading1 ant Title style. Its working, I have problem in the following code:
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Title test");
If I add this pharagraph, in the created .docx file the Title style will be the default docx4j style, but the Heading1 will be the same as in the template.docx. If I change the "Title" to "Heading1", I have the same problem, but the Heading1 style will be the default and Title will be the imported style. So If I add styled paragraph in the code, the style will change to default style, but if I dont add styled paragraph and I open the created docx in Word and change the text style, it will be the imported style.
I have the solution. I use Word in hungarian language, and the "Heading1" in hungarian is "Címsor1."
So instead of this:
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1", "Test");
I have to use this:
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Cmsor1", "Test");