Package org.docx4j.openpackaging.parts.WordprocessingML

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


    // Also create mailmerge data source
    StringBuilder fieldname = new StringBuilder();
    StringBuilder row1 = new StringBuilder();

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

    org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = new DocPropsCustomPart();
    wordMLPackage.addTargetPart(docPropsCustomPart);
    org.docx4j.docProps.custom.ObjectFactory cpfactory = new org.docx4j.docProps.custom.ObjectFactory();
    org.docx4j.docProps.custom.Properties customProps = cpfactory.createProperties();
    docPropsCustomPart.setJaxbElement(customProps);
   

    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();

    int i = 1;
    for (SwitchTestQuad tt : quads) {
      //org.docx4j.wml.P  p = factory.createP();
      org.docx4j.wml.P  p = mdp.addParagraphOfText(tt.val  +" " +  tt.format + " --> ");
     
      p.getContent().add(createSimpleField(tt, "myvar"+i, true));
     
      // Ok, let's add a custom property.
      org.docx4j.docProps.custom.Properties.Property newProp = cpfactory.createPropertiesProperty();
View Full Code Here


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

    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml";
       
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));   
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
       
    String xpath = "//w:t[contains(text(),'scaled')]";
    //String xpath = "//w:r[w:t[contains(text(),'scaled')]]";
   
    List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
   
    System.out.println("got " + list.size() + " matching " + xpath );
   
    for (Object o : list) {
     
View Full Code Here

      for (int i = 0; i<files.length; i++  ) {
        System.out.println("\n" + files[i] + "\n\n");
       
        if (files[i].endsWith("docx")) {
          WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputdir + "/" + files[i]));   
          MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
         
          xpathQuery(documentPart, "//w:fldChar[count(w:ffData)=1]");
        }
      }
     
View Full Code Here

        inputfilepath = System.getProperty("user.dir")
            + "/sample-docs/word/sample-docx.xml";
      }
     
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
     
         // Pretty print the main document part
      System.out.println(
          XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true) );
     
    }
View Full Code Here

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

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(new java.io.File(System.getProperty("user.dir")
            + "/BookmarksDeleter.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();

    fixRange(body.getContent(), "CTBookmark", "CTMarkupRange");
   
    // After
    System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
   
    // or save the docx...
  }
View Full Code Here

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

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .createPackage();
    // Delete the Styles part, since it clutters up our output
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    Relationship styleRel = mdp.getStyleDefinitionsPart().getSourceRelationships().get(0);
    mdp.getRelationshipsPart().removeRelationship(styleRel);   

    // OK, the guts of this sample:
    // The 2 things you need:
    // 1. the Header part
    Relationship relationship = createHeaderPart(wordMLPackage);
View Full Code Here


    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .load(f);
   
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    // Remove from sectPr
    SectPrFinder finder = new SectPrFinder(mdp);
      new TraversalUtil(mdp.getContent(), finder);
      for (SectPr sectPr : finder.getSectPrList()) {
        sectPr.getEGHdrFtrReferences().clear();
      }
     
      // Remove rels
      List<Relationship> hfRels = new ArrayList<Relationship>();
      for (Relationship rel : mdp.getRelationshipsPart().getRelationships().getRelationship() ) {
       
        if (rel.getType().equals(Namespaces.HEADER)
            || rel.getType().equals(Namespaces.FOOTER)) {
          hfRels.add(rel);
        }
      }
      for (Relationship rel : hfRels ) {
        mdp.getRelationshipsPart().removeRelationship(rel);
      }

    wordMLPackage.save(f);
     
   
View Full Code Here

 
  public PropertyResolver(WordprocessingMLPackage wordMLPackage) throws Docx4JException {
   
    this.wordMLPackage = wordMLPackage;
   
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    styleDefinitionsPart = mdp.getStyleDefinitionsPart(true);
    themePart = mdp.getThemePart();
    numberingDefinitionsPart = mdp.getNumberingDefinitionsPart();
    if (wordMLPackage.getMainDocumentPart().getDocumentSettingsPart()!=null
        && wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getContents()!=null) {
      themeFontLang = wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getContents().getThemeFontLang();
    }
    init();   
View Full Code Here

   * @param args
   */
  public static void main(String[] argsthrows Exception {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
   
    mdp.addParagraphOfText("Paragraph 1");

    // Add the XHTML altChunk
    String xhtml = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>";
    mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes());
   
    mdp.addParagraphOfText("Paragraph 3");
   
    // Round trip
    WordprocessingMLPackage pkgOut = mdp.convertAltChunks();
   
    // Display result
    System.out.println(
        XmlUtils.marshaltoString(pkgOut.getMainDocumentPart().getJaxbElement(), true, true));
   
View Full Code Here

    // Load the Package
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
   
    // Change the rels to TargetMode="External"
    // Fetch rels part
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    RelationshipsPart relsPart = documentPart.getRelationshipsPart();
    Relationships rels = relsPart.getRelationships();
    List<Relationship> relsList = rels.getRelationship();
   
    // For each image rel
    for (Relationship r : relsList) {

      System.out.println(r.getTargetMode());
      if ( r.getType().equals( Namespaces.IMAGE )
          && (r.getTargetMode()==null
              || r.getTargetMode().equalsIgnoreCase("internal"))) {
       
        String target = r.getTarget();
        System.out.println("target: " + target);
       
        if (saveImages) {
          // Save the image as a file in the specified location
         
          File f = new File(BASE_DIR + "/" + target);
          if (f.exists()) {
            System.out.println("Overwriting existing object: " + f.getPath() );
          } else if ( !f.getParentFile().exists() ) {
            System.out.println("creating " + f.getParentFile().getAbsolutePath() );
            f.getParentFile().mkdirs();
            //f = new File(BASE_DIR + "/" + target);
          }         
         
          Part p = relsPart.getPart(r);
          FileOutputStream fos = new FileOutputStream( f );
          ((BinaryPart)p).writeDataToOutputStream(fos);
          fos.close();
         
        }
        //   .. externalise - after getPart!
        r.setTargetMode("External");
       
      }     
    }
     
    // Change r:embed to r:link
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
    Body body =  wmlDocumentEl.getBody();
           
    SingleTraversalUtilVisitorCallback imageVisitor
      = new SingleTraversalUtilVisitorCallback(
          new TraversalUtilBlipVisitor());
View Full Code Here

TOP

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

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.