Search in sources :

Example 1 with InvalidFormatException

use of org.docx4j.openpackaging.exceptions.InvalidFormatException 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

Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 InvalidFormatException (org.docx4j.openpackaging.exceptions.InvalidFormatException)1 Part (org.docx4j.openpackaging.parts.Part)1 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)1 Numbering (org.docx4j.wml.Numbering)1 Styles (org.docx4j.wml.Styles)1