Пример #1
0
  public void updateState(NodeIterator fldCharNodeIt) {
    org.docx4j.wml.FldChar field = null;

    Node node = fldCharNodeIt.nextNode();

    try {
      field =
          (org.docx4j.wml.FldChar)
              XmlUtils.unmarshal(node, Context.jc, org.docx4j.wml.FldChar.class);
    } catch (JAXBException e1) {
      e1.printStackTrace();
    }

    STFldCharType fieldCharType = field.getFldCharType();

    if (fieldCharType == null) {

      log.debug("Ignoring unrecognised: " + XmlUtils.w3CDomNodeToString(node));

    } else {

      if (fieldCharType == STFldCharType.BEGIN) {
        inField = true;
      } else if (fieldCharType == STFldCharType.END) {
        inField = false;
      }
      // else ignore STFldCharType.SEPARATE
    }
  }
  @Override
  public List<Object> apply(Object o) {

    //		System.out.println(o.getClass().getName());

    //		if (o instanceof P) {
    //			currentP = (P)o;
    //		}

    if (o instanceof org.docx4j.wml.FldChar) {
      FldChar fldChar = (FldChar) o;
      if (fldChar.getFldCharType().equals(STFldCharType.BEGIN)) {
        // System.out.println("Found a BEGIN");
        depth++;
        P currentP = pStack.peek();
        if (depth == 1 && !starts.contains(currentP)) {
          starts.add(currentP);
          //					System.out.println("Adding " + XmlUtils.marshaltoString(currentP));
        }
      }
      if (fldChar.getFldCharType().equals(STFldCharType.END)) {
        depth--;
      }
    }

    return null;
  }
Пример #3
0
  /**
   * Creates document footer including page number
   *
   * @param doc WordprocessingMLPackage for the document.
   * @throws InvalidFormatException
   */
  private void createFooter(WordprocessingMLPackage doc) throws InvalidFormatException {
    MainDocumentPart content = doc.getMainDocumentPart();

    // Create footer
    FooterPart footer = new FooterPart();

    Ftr ftr = objectFactory.createFtr();
    P footerParagraph = objectFactory.createP();

    setStyle(footerParagraph, "footer");

    PPr parProps = objectFactory.createPPr();
    Jc al = objectFactory.createJc();
    al.setVal(JcEnumeration.RIGHT);
    parProps.setJc(al);
    footerParagraph.setPPr(parProps);

    // Add field start
    R run = objectFactory.createR();
    FldChar fldChar = objectFactory.createFldChar();
    fldChar.setFldCharType(STFldCharType.BEGIN);
    run.getContent().add(fldChar);
    footerParagraph.getContent().add(run);

    // Add pageNumber field
    run = objectFactory.createR();
    Text txt = objectFactory.createText();
    txt.setSpace("preserve");
    txt.setValue(" PAGE   \\* MERGEFORMAT ");
    run.getContent().add(objectFactory.createRInstrText(txt));
    footerParagraph.getContent().add(run);

    // Add field end
    run = objectFactory.createR();
    fldChar = objectFactory.createFldChar();
    fldChar.setFldCharType(STFldCharType.END);
    run.getContent().add(fldChar);
    footerParagraph.getContent().add(run);

    ftr.getContent().add(footerParagraph);
    footer.setJaxbElement(ftr);

    Relationship rel = content.addTargetPart(footer);

    // Relate footer to document
    List<SectionWrapper> sections = doc.getDocumentModel().getSections();

    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();

    if (null == sectPr) {
      sectPr = objectFactory.createSectPr();
      content.addObject(sectPr);
      sections.get(sections.size() - 1).setSectPr(sectPr);
    }

    FooterReference footerReference = objectFactory.createFooterReference();
    footerReference.setId(rel.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
  }
Пример #4
0
  public static List<Object> getChildrenImpl(Object o) {

    if (o == null) {
      log.warn("null passed to getChildrenImpl");
      return null;
    }

    log.debug("getting children of " + o.getClass().getName());
    if (o instanceof org.docx4j.wml.Text) return null;

    // Short circuit for common elements
    if (o instanceof List) {
      // Handy if you have your own list of objects you wish to process
      return (List<Object>) o;
    } else if (o instanceof org.docx4j.wml.ContentAccessor) {
      return ((org.docx4j.wml.ContentAccessor) o).getContent();
    } else if (o instanceof org.docx4j.wml.SdtElement) {
      return ((org.docx4j.wml.SdtElement) o).getSdtContent().getContent();
    } else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Anchor) {
      org.docx4j.dml.wordprocessingDrawing.Anchor anchor =
          (org.docx4j.dml.wordprocessingDrawing.Anchor) o;
      List<Object> artificialList = new ArrayList<Object>();
      CTNonVisualDrawingProps drawingProps = anchor.getDocPr();
      if (drawingProps != null) {
        handleCTNonVisualDrawingProps(drawingProps, artificialList);
      }
      if (anchor.getGraphic() != null) {
        log.debug("found a:graphic");
        org.docx4j.dml.Graphic graphic = anchor.getGraphic();
        if (graphic.getGraphicData() != null) {
          artificialList.addAll(handleGraphicData(graphic.getGraphicData()));
        }
      }
      if (!artificialList.isEmpty()) return artificialList;
    } else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Inline) {
      org.docx4j.dml.wordprocessingDrawing.Inline inline =
          (org.docx4j.dml.wordprocessingDrawing.Inline) o;
      List<Object> artificialList = new ArrayList<Object>();
      CTNonVisualDrawingProps drawingProps = inline.getDocPr();
      if (drawingProps != null) {
        handleCTNonVisualDrawingProps(drawingProps, artificialList);
      }
      if (inline.getGraphic() != null) {
        log.debug("found a:graphic");
        org.docx4j.dml.Graphic graphic = inline.getGraphic();
        if (graphic.getGraphicData() != null) {
          artificialList.addAll(handleGraphicData(graphic.getGraphicData()));
        }
        return artificialList;
      }
      if (!artificialList.isEmpty()) return artificialList;

    } else if (o instanceof Pict) {
      return ((Pict) o).getAnyAndAny(); // (why didn't the reflection below find this?)

    } else if (o instanceof org.docx4j.dml.picture.Pic) { // Post 2.7.1; untested

      org.docx4j.dml.picture.Pic dmlPic = ((org.docx4j.dml.picture.Pic) o);
      if (dmlPic.getBlipFill() != null && dmlPic.getBlipFill().getBlip() != null) {
        log.debug("found DML Blip");
        List<Object> artificialList = new ArrayList<Object>();
        artificialList.add(dmlPic.getBlipFill().getBlip());
        return artificialList;
      } else {
        return null;
      }
    } else if (o instanceof org.docx4j.dml.CTGvmlPicture) { // Post 2.7.1

      org.docx4j.dml.CTGvmlPicture dmlPic = ((org.docx4j.dml.CTGvmlPicture) o);
      if (dmlPic.getBlipFill() != null && dmlPic.getBlipFill().getBlip() != null) {
        log.debug("found DML Blip");
        List<Object> artificialList = new ArrayList<Object>();
        artificialList.add(dmlPic.getBlipFill().getBlip());
        return artificialList;
      } else {
        return null;
      }

    } else if (o instanceof org.docx4j.vml.CTShape) {
      //			return ((org.docx4j.vml.CTShape)o).getAny();
      List<Object> artificialList = new ArrayList<Object>();
      for (JAXBElement<?> j : ((org.docx4j.vml.CTShape) o).getPathOrFormulasOrHandles()) {
        //				System.out.println(XmlUtils.unwrap(j).getClass().getName() );
        artificialList.add(j);
      }
      return artificialList;
    } else if (o instanceof CTDataModel) {
      CTDataModel dataModel = (CTDataModel) o;
      List<Object> artificialList = new ArrayList<Object>();
      // We're going to create a list merging two children ..
      artificialList.addAll(dataModel.getPtLst().getPt());
      artificialList.addAll(dataModel.getCxnLst().getCxn());
      return artificialList;
    } else if (o instanceof org.docx4j.dml.diagram2008.CTDrawing) {
      return ((org.docx4j.dml.diagram2008.CTDrawing) o).getSpTree().getSpOrGrpSp();
    } else if (o instanceof org.docx4j.vml.CTTextbox) {
      //			return ((org.docx4j.vml.CTTextbox)o).getAny();
      org.docx4j.vml.CTTextbox textBox = ((org.docx4j.vml.CTTextbox) o);
      if (textBox.getTxbxContent() == null) {
        return null;
      } else {
        return textBox.getTxbxContent().getEGBlockLevelElts();
        // grandchildren
      }
      //		} else if (o instanceof org.docx4j.wml.CTTxbxContent) {
      //			return ((org.docx4j.wml.CTTxbxContent)o).getEGBlockLevelElts();
    } else if (o instanceof CTObject) {

      CTObject ctObject = (CTObject) o;
      List<Object> artificialList = new ArrayList<Object>();
      artificialList.addAll(ctObject.getAnyAndAny());
      if (ctObject.getControl() != null) {
        artificialList.add(ctObject.getControl()); // CTControl
      }
      return artificialList;
    } else if (o instanceof org.docx4j.dml.CTGvmlGroupShape) {
      return ((org.docx4j.dml.CTGvmlGroupShape) o).getTxSpOrSpOrCxnSp();
    } else if (o instanceof FldChar) {
      FldChar fldChar = ((FldChar) o);
      List<Object> artificialList = new ArrayList<Object>();
      artificialList.add(fldChar.getFldCharType());
      if (fldChar.getFfData() != null) {
        artificialList.add(fldChar.getFfData());
      }
      if (fldChar.getFldData() != null) {
        artificialList.add(fldChar.getFldData());
      }
      if (fldChar.getNumberingChange() != null) {
        artificialList.add(fldChar.getNumberingChange());
      }
      return artificialList;
    }

    // OK, what is this? Use reflection ..
    // This should work for things including w:drawing
    log.debug(".. looking for method which returns list ");
    try {
      Method[] methods = o.getClass().getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        if (m.getReturnType().getName().equals("java.util.List")) {
          return (List<Object>) m.invoke(o);
        }
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    log.debug(".. no list member");
    return null;
  }
Пример #5
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"));
  }