Search in sources :

Example 36 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project flexmark-java by vsch.

the class DocxRenderer method setDefaultStyleAndNumbering.

static void setDefaultStyleAndNumbering(WordprocessingMLPackage out, final DataHolder options) {
    try {
        // (main doc part it if necessary)
        MainDocumentPart documentPart = out.getMainDocumentPart();
        if (documentPart == null) {
            try {
                documentPart = new MainDocumentPart();
                out.addTargetPart(documentPart);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
        }
        if (documentPart.getStyleDefinitionsPart() == null) {
            Part stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart();
            final Styles styles = (Styles) XmlUtils.unmarshalString(STYLES_XML.getFrom(options));
            ((org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart) stylesPart).setJaxbElement(styles);
            // NB - add it to main doc part, not package!
            documentPart.addTargetPart(stylesPart);
            assert documentPart.getStyleDefinitionsPart() != null : "Styles failed to set";
        }
        if (documentPart.getNumberingDefinitionsPart() == null) {
            // add it
            Part numberingPart = new org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart();
            final Numbering numbering = (Numbering) XmlUtils.unmarshalString(NUMBERING_XML.getFrom(options));
            ((org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) numberingPart).setJaxbElement(numbering);
            // NB - add it to main doc part, not package!
            documentPart.addTargetPart(numberingPart);
            assert documentPart.getNumberingDefinitionsPart() != null : "Numbering failed to set";
        }
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
Also used : Numbering(org.docx4j.wml.Numbering) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Part(org.docx4j.openpackaging.parts.Part) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) InvalidFormatException(org.docx4j.openpackaging.exceptions.InvalidFormatException) InvalidFormatException(org.docx4j.openpackaging.exceptions.InvalidFormatException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Styles(org.docx4j.wml.Styles)

Aggregations

MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)36 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)22 File (java.io.File)19 P (org.docx4j.wml.P)13 ObjectFactory (org.docx4j.wml.ObjectFactory)7 Text (org.docx4j.wml.Text)7 Body (org.docx4j.wml.Body)5 Br (org.docx4j.wml.Br)5 R (org.docx4j.wml.R)5 RPr (org.docx4j.wml.RPr)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 Relationship (org.docx4j.relationships.Relationship)4 Tr (org.docx4j.wml.Tr)4 Tc (org.docx4j.wml.Tc)3 Child (org.jvnet.jaxb2_commons.ppp.Child)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2