Package org.docx4j.openpackaging.parts.WordprocessingML

Examples of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart$FontAndStyleFinder


    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Table.docx";
    //String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Word2007-fonts.docx";
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();   
   
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
   
    Writer out = new OutputStreamWriter(System.out);
   
    extractText(wmlDocumentEl, out);
   
View Full Code Here


public class TableOfContentsAdd {

  public static void main(String[] args) throws Exception {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
   
    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 \\h");
        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") );
   
  }
View Full Code Here

    String inputfilepath = System.getProperty("user.dir")
        + "/sample-docs/sample-docx.xml";

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

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

    new TraversalUtil(body,
View Full Code Here

  }

  public static void visit(WordprocessingMLPackage wmlPackage,
      boolean bodyOnly, Callback callback) {
   
    MainDocumentPart mainDocument = null;
    RelationshipsPart relPart = null;
    List<Relationship> relList = null;
    List<Object> elementList = null;
   
    if ((wmlPackage != null) && (callback != null)) {
      mainDocument = wmlPackage.getMainDocumentPart();
      callback.walkJAXBElements(mainDocument.getJaxbElement().getBody());
      if (!bodyOnly) {
        relPart = mainDocument.getRelationshipsPart();
        relList = relPart.getRelationships().getRelationship();
        for (Relationship rs : relList) {
          elementList = null;
          if (Namespaces.HEADER.equals(rs.getType())) {
            elementList = ((HeaderPart) relPart.getPart(rs))
View Full Code Here

    }
   
     
    WordprocessingMLPackage wordMLPackage  = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
   
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    if (wordMLPackage.getMainDocumentPart().getXPathsPart() == null) {
      throw new Docx4JException("OpenDoPE XPaths part missing");
    } else {
      xPaths = wordMLPackage.getMainDocumentPart().getXPathsPart()
          .getJaxbElement();
View Full Code Here

   
       
    boolean save = true;

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Example 1: add text in Title style
    mdp.addStyledParagraphOfText("Title", "Select all, then hit F9 in Word to see your pictures, or programmatically add them first");
   
    mdp.createParagraphOfText("simple field:");
   
    P p = new P();
    p.getContent().add(
        createSimpleField( " INCLUDEPICTURE  \"file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\"  \\* MERGEFORMAT ")
        );
    mdp.getContent().add(p);

    mdp.createParagraphOfText("complex field:");
   
    p = new P();
    addComplexField(p, " INCLUDEPICTURE  \"file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\"  \\* MERGEFORMAT ");
    mdp.getContent().add(p);
     
   
       // Pretty print the main document part
    System.out.println(
        XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true) );
   
    // Optionally save it
    if (save) {
      String filename = System.getProperty("user.dir") + "/OUT_FieldINCLUDEPICTURE.docx";
      wordMLPackage.save(new java.io.File(filename) );
View Full Code Here

    return new DefaultXmlPart(new PartName(partName));
  }
 
  public Part CreateMainDocumentPartObject(String partName)
      throws InvalidFormatException {
    return new MainDocumentPart(new PartName(partName));
  }
View Full Code Here

    String outputfilepath = System.getProperty("user.dir")
        + "/OUT_VariableReplace.docx";

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

    HashMap<String, String> mappings = new HashMap<String, String>();
    mappings.put("colour", "green");
    mappings.put("icecream", "chocolate");
   
    long start = System.currentTimeMillis();

    // Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
   
      documentPart.variableReplace(mappings);
   
/*    // Approach 2 (original)
   
      // unmarshallFromTemplate requires string input
      String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
      // Do it...
      Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
      // Inject result into docx
      documentPart.setJaxbElement((Document) obj);
*/
     
    long end = System.currentTimeMillis();
    long total = end - start;
    System.out.println("Time: " + total);

    // Save it
    if (save) {
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
      saver.save(outputfilepath);
    } else {
      System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true,
          true));
    }
  }
View Full Code Here

   

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(System.getProperty("user.dir")
            + "/bm1.docx"));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
   
    // Before..
    // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));

    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
        .getJaxbElement();
    Body body = wmlDocumentEl.getBody();
   
    BookmarksReplaceWithText bti = new BookmarksReplaceWithText();
View Full Code Here

    // Create the master doc, and specify
    // the subdoc
    String subdocx = ".\\sample-docs\\word\\sample-docx.xml";
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Link to subdoc
    JAXBElement<CTRel> subdoc = createSubdoc(mdp, subdocx);
   
    // Add it to a paragraph
    org.docx4j.wml.ObjectFactory wmlFactory = Context.getWmlObjectFactory();
    org.docx4j.wml.P paragraph = wmlFactory.createP();
   
    paragraph.getContent().add( subdoc );
    mdp.addObject(paragraph);
   
    // Now save it
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_SubDocumentMASTER.docx") );
   
  }
View Full Code Here

TOP

Related Classes of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart$FontAndStyleFinder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.