private static double getNumber(FldSimpleModel model, String value)
      throws FieldResultIsNotANumberException {

    WordprocessingMLPackage pkg = model.getWordMLPackage();
    String decimalSymbol = null;
    if (pkg != null && pkg.getMainDocumentPart().getDocumentSettingsPart() != null) {

      DocumentSettingsPart docSettingsPart = pkg.getMainDocumentPart().getDocumentSettingsPart();
      if (docSettingsPart.getJaxbElement().getDecimalSymbol() != null) {
        decimalSymbol = docSettingsPart.getJaxbElement().getDecimalSymbol().getVal();
      }
    }

    // For DOCPROPERTY field, and possibly some others, but not "=",
    // Word will parse "€180,000.00 EUR" as a number
    if (model.fldName.equals("DOCPROPERTY") || model.fldName.equals("MERGEFIELD")) {

      // First, parse the value
      NumberExtractor nex = new NumberExtractor(decimalSymbol);
      try {
        value = nex.extractNumber(value);
      } catch (java.lang.IllegalStateException noMatch) {
        // There is no number in this string.
        // In this case Word just inserts the non-numeric text,
        // without attempting to format the number
        throw new FieldResultIsNotANumberException("No number in " + value);
      }
    }

    try {
      return Double.parseDouble(value);
    } catch (Exception e) {
      // TODO: is it a bookmark?

      throw new FieldResultIsNotANumberException();
    }
  }
Пример #2
0
  public static void main(String[] args) throws Exception {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    CTSettings ct = new CTSettings();
    DocumentSettingsPart dsp = documentPart.getDocumentSettingsPart();
    if (dsp == null) {
      dsp = new DocumentSettingsPart();
      CTView ctView = Context.getWmlObjectFactory().createCTView();
      ctView.setVal(STView.PRINT);
      ct.setView(ctView);
      BooleanDefaultTrue b = new BooleanDefaultTrue();
      b.setVal(true);
      ct.setUpdateFields(b);
      dsp.setJaxbElement(ct);
      documentPart.addTargetPart(dsp);
    }

    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
    Body body = wmlDocumentEl.getBody();

    ObjectFactory factory = Context.getWmlObjectFactory();

    /*
     * Create the following:
     *
     * <w:p> <w:r> <w:fldChar w:dirty="true" w:fldCharType="begin"/>
     * <w:instrText xml:space="preserve">TOC \o &quot;1-3&quot; \h \z \ u
     * \h</w:instrText> </w:r> <w:r/> <w:r> <w:fldChar w:fldCharType="end"/>
     * </w:r> </w:p>
     */
    P paragraphForTOC = factory.createP();
    R r = factory.createR();

    FldChar fldchar = factory.createFldChar();
    fldchar.setFldCharType(STFldCharType.BEGIN);
    fldchar.setDirty(true);
    r.getContent().add(getWrappedFldChar(fldchar));
    paragraphForTOC.getContent().add(r);

    R r1 = factory.createR();
    Text txt = new Text();
    txt.setSpace("preserve");
    txt.setValue("TOC \\o \"1-3\" \\h \\z \\u ");
    r.getContent().add(factory.createRInstrText(txt));
    paragraphForTOC.getContent().add(r1);

    FldChar fldcharend = factory.createFldChar();
    fldcharend.setFldCharType(STFldCharType.END);
    R r2 = factory.createR();
    r2.getContent().add(getWrappedFldChar(fldcharend));
    paragraphForTOC.getContent().add(r2);

    body.getContent().add(paragraphForTOC);

    documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
    documentPart.addStyledParagraphOfText("Heading2", "Hello 2");
    documentPart.addStyledParagraphOfText("Heading3", "Hello 3");
    documentPart.addStyledParagraphOfText("Heading1", "Hello 1");

    wordMLPackage.save(
        new java.io.File(System.getProperty("user.dir") + "/OUT_TableOfContentsAdd.docx"));
  }