示例#1
0
  private void init() throws Docx4JException {

    // Make sure we have a styles definitions part
    try {
      if (styleDefinitionsPart == null) {
        styleDefinitionsPart = new StyleDefinitionsPart();
        styleDefinitionsPart.unmarshalDefaultStyles();

        // For this case, should we provide a way
        // to add this new part to the package?

      }
    } catch (Exception e) {
      throw new Docx4JException("Couldn't create default StyleDefinitionsPart", e);
    }

    defaultParagraphStyleId = this.styleDefinitionsPart.getDefaultParagraphStyle().getStyleId();
    defaultCharacterStyleId = this.styleDefinitionsPart.getDefaultCharacterStyle().getStyleId();

    // Initialise styles
    styles = (org.docx4j.wml.Styles) styleDefinitionsPart.getJaxbElement();
    initialiseLiveStyles();

    // Initialise docDefaults
    docDefaults = styles.getDocDefaults();

    if (docDefaults == null) {
      // The only way this can happen is if the
      // styles definition part is missing the docDefaults element
      // (these are present in docs created from Word, and
      // in our default styles, so maybe the user created it using
      // some 3rd party program?)
      try {
        docDefaults =
            (DocDefaults) XmlUtils.unmarshalString(StyleDefinitionsPart.docDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.docDefaultsString, e);
      }
    }

    // Setup documentDefaultPPr
    if (docDefaults.getPPrDefault() == null) {
      try {
        documentDefaultPPr = (PPr) XmlUtils.unmarshalString(StyleDefinitionsPart.pPrDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.pPrDefaultsString, e);
      }
    } else {
      documentDefaultPPr = docDefaults.getPPrDefault().getPPr();
    }

    // Setup documentDefaultRPr
    if (docDefaults.getRPrDefault() == null) {
      try {
        documentDefaultRPr = (RPr) XmlUtils.unmarshalString(StyleDefinitionsPart.rPrDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.rPrDefaultsString, e);
      }
    } else {
      documentDefaultRPr = docDefaults.getRPrDefault().getRPr();
    }

    addNormalToResolvedStylePPrComponent();
    addDefaultParagraphFontToResolvedStyleRPrComponent();
  }