简体   繁体   English

来自其他.docx问题的docx4j导入样式

[英]docx4j import style from other .docx problem

I get code from here: Docx4j export style from one .docx and use it in another .docx 我从这里获取代码: 从一个.docx导出docx4j样式,并在另一个.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. 因此,我从template.docx导入了样式,并在其中创建了自己的Heading1 ant Title样式。 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. 如果我添加此法则,则在创建的.docx文件中,标题样式将为默认的docx4j样式,但Heading1将与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. 如果我将“标题”更改为“ Heading1”,则会遇到相同的问题,但是Heading1样式将是默认样式,而Title将是导入的样式。 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. 因此,如果我在代码中添加样式化的段落,则样式将更改为默认样式,但是如果我不添加样式化的段落,而是在Word中打开创建的docx并更改文本样式,则它将是导入的样式。

I have the solution. 我有解决方案。 I use Word in hungarian language, and the "Heading1" in hungarian is "Címsor1." 我在匈牙利语中使用Word,匈牙利语中的“ Heading1”是“Címsor1”。

So instead of this: 所以代替这个:

wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1", "Test");

I have to use this: 我必须使用这个:

wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Cmsor1", "Test");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM