Search in sources :

Example 6 with Part

use of org.docx4j.openpackaging.parts.Part 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

Part (org.docx4j.openpackaging.parts.Part)6 PartName (org.docx4j.openpackaging.parts.PartName)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 Parts (org.docx4j.openpackaging.parts.Parts)3 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 BinaryPart (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart)2 CommentsPart (org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)2 Comments (org.docx4j.wml.Comments)2 Comment (org.docx4j.wml.Comments.Comment)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 TraversalUtil (org.docx4j.TraversalUtil)1 InvalidFormatException (org.docx4j.openpackaging.exceptions.InvalidFormatException)1