Docx4j export style from one .docx and use it in another .docx

纵饮孤独 提交于 2020-01-04 05:38:06

问题


I am creating new Word document with the code below:

Tidy tidy = new Tidy();
            tidy.setShowWarnings(true);
            tidy.setInputEncoding("UTF-8");
            tidy.setOutputEncoding("UTF-8");
            tidy.setXHTML(true);
            tidy.setMakeClean(true);
            tidy.setQuoteNbsp(false);

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
            XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

for (Value v : res.getRules()) {
                System.out.println(v.toString());
                ByteArrayOutputStream ou = new ByteArrayOutputStream();
                tidy.parse(new ByteArrayInputStream(v.toString().getBytes(StandardCharsets.UTF_8)), ou);
                wordMLPackage.getMainDocumentPart().getContent().clear();
                wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(new String(ou.toByteArray()), null));
            }
            wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/report.docx"));

What I want to do, is to use style from other .docx and append it to specific part of output that I save. Any ideas ? I've spent a lot of time on finding solution, but I haven't found anything useful.


回答1:


First document. We want to import styles from it.

WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage
.load(new java.io.File(System.getProperty("user.dir") + "/template.docx"));

Second document

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

We import styles from first document and append them to the second document

MainDocumentPart tempDocPart = wordMLPackage2.getMainDocumentPart();
StyleDefinitionsPart sdp = tempDocPart.getStyleDefinitionsPart();
Styles tempStyle = sdp.getJaxbElement();
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setJaxbElement(tempStyle);

And we can use specific style using its id

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



回答2:


Ordinarily, you'd import your XHTML directly into the target docx (rather than creating a new docx, as you do in your question). That way, the imported content can use the target styles (if that's what you want).

If however you want to copy styles from one docx to another, that can be trivial or more complex (but has nothing to do with XHTML import).

To start with, you'll need references to the styles in each docx:

Styles styles = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();

Styles are usually "basedOn" another style. If the style you are importing is basedOn a style in your target docx with the same definition, you can just copy the source style.

If the basedOn style is not present in the target docx or defined differently, you'll need to handle that case, by copying/renaming. And so on up the hierarchy.

Also, styles can use numbering definitions, so you might need to import those as well (from the NumberingDefinitionsPart).



来源:https://stackoverflow.com/questions/35135679/docx4j-export-style-from-one-docx-and-use-it-in-another-docx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!