Examples of MainDocumentPart


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

  @Test
  public void testNumbering() throws Exception {
   
    WordprocessingMLPackage wordMLPackage = createPkg();
   
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

    P p = createNumberedP("ChapterLevel1", 12, 0);
    mdp.getContent().add(p);
    Assert.assertEquals("1", Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());
   
    //System.out.println(Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());
   
    p = createNumberedP("ChapterLevel2", 6, 1);
    mdp.getContent().add(p);
    Assert.assertEquals("1.1", Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());
   
    p = createNumberedP("ChapterLevel2", 6, 1);
    mdp.getContent().add(p);
    Assert.assertEquals("1.2", Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());

    p = createNumberedP("ChapterLevel3", 6, 2);
    mdp.getContent().add(p);
    Assert.assertEquals("1.2.1", Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());
   
    p = createNumberedP("ChapterLevel1", -1, -1); // no numPr
    mdp.getContent().add(p);
    Assert.assertEquals("2", Emulator.getNumber(wordMLPackage, p.getPPr()).getNumString());
   
  }
View Full Code Here

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

   
       
    boolean save = true;

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Example 1: add text in Title style
    mdp.addStyledParagraphOfText("Title", "Example 1");

    // Example 2: add normal paragraph (no explicit style)
    mdp.addParagraphOfText("Example 2");
   
    // Example 3a: bold text
    // To get bold text, you must set the run's rPr@w:b,
      // so you can't use the addParagraphOfText convenience method
   
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    org.docx4j.wml.P  p = factory.createP();

    org.docx4j.wml.Text  t = factory.createText();
    t.setValue("Example 3a (bold)");

    org.docx4j.wml.R  run = factory.createR();
    run.getContent().add(t);   
   
    p.getContent().add(run);
   
   
    org.docx4j.wml.RPr rpr = factory.createRPr();   
    org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
      b.setVal(true);     
      rpr.setB(b);
     
    run.setRPr(rpr);
   
    // Optionally, set pPr/rPr@w:b   
      org.docx4j.wml.PPr ppr = factory.createPPr();     
      p.setPPr( ppr );
      org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr();
      ppr.setRPr(paraRpr);     
      rpr.setB(b);
     
      mdp.getJaxbElement().getBody().getContent().add(p);
      // or just:
      // mdp.getContent().add(p);
      // but:
      // mdp.addObject(p);
      // is a better alternative if you are using a new style, since it
      // will ensure that the style is activated 

    // Example 3b: bold text
      // Well, actually you can use addParagraphOfText:
    P p3b = mdp.addParagraphOfText("Example 3b (bold)");
    R r3b = (R)p3b.getContent().get(0);
      // .. now set rPr (I'll just reuse the above object)
    r3b.setRPr(rpr);

     
      // Example 4: Here is an easier way:
      String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Example 4</w:t></w:r></w:p>";
     
      mdp.addObject(
            org.docx4j.XmlUtils.unmarshalString(str) );

     
      // Example 5: Let's add a table
      int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
      int cols = 3;
      int cellWidthTwips = new Double(
                    Math.floor( (writableWidthTwips/cols ))
                      ).intValue();
     
      Tbl tbl = TblFactory.createTable(3, 3, cellWidthTwips);
      mdp.addObject(tbl);
     
   
       // 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_CreateWordprocessingMLDocument.docx";
      wordMLPackage.save(new java.io.File(filename) );
View Full Code Here

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

   * @param wordMLPackage
   * @return
   */
  public static CustomXmlDataStoragePart getCustomXmlDataStoragePart(WordprocessingMLPackage wordMLPackage) throws Docx4JException {
   
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart()
   
    if (wordMLPackage.getMainDocumentPart().getXPathsPart()==null) {
     
      // Can't do it the easy way, so inspect content controls

View Full Code Here

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

          + "/sample-docs/word/sample-docx.docx";
    }

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

    // Uncomment to see the raw XML
    //System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));

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

    new TraversalUtil(body,
View Full Code Here

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

   
    String inputfilepath = System.getProperty("user.dir") + "/test.docx";
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
   
    Marshaller marshaller=Context.jc.createMarshaller();
    NamespacePrefixMapperUtils.setProperty(marshaller,
        NamespacePrefixMapperUtils.getPrefixMapper());
   
    Writer out = new OutputStreamWriter(System.out);   
    marshaller.marshal(documentPart.getJaxbElement(), new SAXHandlerToCodeString(out));
    out.close();
  }
View Full Code Here

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

   
    String inputfilepath = System.getProperty("user.dir") + "/test.docx";
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
   
    Marshaller marshaller=Context.jc.createMarshaller();
    NamespacePrefixMapperUtils.setProperty(marshaller,
        NamespacePrefixMapperUtils.getPrefixMapper());
   
    Writer out = new OutputStreamWriter(System.out);   
    marshaller.marshal(documentPart.getJaxbElement(), new SAXIdentityHandler(out));
   
    out.close();
  }
View Full Code Here

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

//    String regex = ".*(calibri|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
    String regex = ".*(calibri|cour|arial).*";
    PhysicalFonts.setRegex(regex);   

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
      P p = new P();
      mdp.getContent().add(p);
     
      R r = Context.getWmlObjectFactory().createR();
     
      r.getContent().add(
         
View Full Code Here

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

  public  void testFont() throws Exception {
   
    boolean save = false;
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Styles part
    StyleDefinitionsPart sdp = mdp.getStyleDefinitionsPart();
    Styles styles = (Styles)XmlUtils.unmarshalString(stylesXML)
    sdp.setJaxbElement(styles);

    // Theme part
    ThemePart themePart = new ThemePart();   
    Theme theme = (Theme)XmlUtils.unmarshalString(themeXML);     
    themePart.setJaxbElement(theme);
    mdp.addTargetPart(themePart);
   
    // Settings
    DocumentSettingsPart dsp = new DocumentSettingsPart();
    dsp.setJaxbElement(createSettings());
    mdp.addTargetPart(dsp);
   
   
    Document document = (Document)XmlUtils.unmarshalString(documentXML);   
    wordMLPackage.getMainDocumentPart().setJaxbElement(document);   
View Full Code Here

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

  public  void testFont() throws Exception {
   
    boolean save = false;
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Styles part
    StyleDefinitionsPart sdp = mdp.getStyleDefinitionsPart();
    Styles styles = (Styles)XmlUtils.unmarshalString(stylesXML)
    sdp.setJaxbElement(styles);

    // Theme part
    ThemePart themePart = new ThemePart();   
    Theme theme = (Theme)XmlUtils.unmarshalString(themeXML);     
    themePart.setJaxbElement(theme);
    mdp.addTargetPart(themePart);
   
    // Settings
    DocumentSettingsPart dsp = new DocumentSettingsPart();
    dsp.setJaxbElement(createSettings());
    mdp.addTargetPart(dsp);
   
   
    Document document = (Document)XmlUtils.unmarshalString(documentXML);   
    wordMLPackage.getMainDocumentPart().setJaxbElement(document);   
View Full Code Here

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

  public  void testFont() throws Exception {
   
    boolean save = false;
   
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Styles part
    StyleDefinitionsPart sdp = mdp.getStyleDefinitionsPart();
    Styles styles = (Styles)XmlUtils.unmarshalString(stylesXML)
    sdp.setJaxbElement(styles);

    // Theme part
    ThemePart themePart = new ThemePart();   
    Theme theme = (Theme)XmlUtils.unmarshalString(themeXML);     
    themePart.setJaxbElement(theme);
    mdp.addTargetPart(themePart);
   
    // Settings
    DocumentSettingsPart dsp = new DocumentSettingsPart();
    dsp.setJaxbElement(createSettings());
    mdp.addTargetPart(dsp);
   
   
    Document document = (Document)XmlUtils.unmarshalString(documentXML);   
    wordMLPackage.getMainDocumentPart().setJaxbElement(document);   
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.