Page 1 of 1

how can i modify the text in the head?

PostPosted: Tue Aug 16, 2011 3:05 pm
by tosswang
hi,all:

i want to modify the text in the head,below is my code
Code: Select all

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.docx4j.XmlUtils;
import org.docx4j.model.structure.HeaderFooterPolicy;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;

public class TestMidfyHeader
{
   public static void main(String[] str)throws Exception
   {
       String inputfilepath = "C:\\workspace\\Doc1.docx";
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
       
        List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
       
        for (SectionWrapper sw : sectionWrappers)
        {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         HeaderPart header= hfp.getDefaultHeader();
         String xml=XmlUtils.marshaltoString(header.getJaxbElement(), true);          
           HashMap<String, String> mappings = new HashMap<String, String>();            
           mappings.put("[[label0]]", "Mi titulo :)");
           XmlUtils.unmarshallFromTemplate(xml, mappings);
        }
               
        wordMLPackage.save(new File("C:\\", "headerFooter.docx"));

   }
}

but The results did not change...
the attachement is my sample doc.

how can i modify codes to achieve my goal?

thanks !

tosswang

Re: how can i modify the text in the head?

PostPosted: Tue Aug 16, 2011 3:18 pm
by tosswang
a ha:

I solved this problem :) ,below is my code
Code: Select all
package org;

import java.io.File;
import java.util.List;
import org.docx4j.model.structure.HeaderFooterPolicy;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;

public class TestMidfyHeader
{
   public static void main(String[] str)throws Exception
   {
       String inputfilepath = "C:\\workspace\\Doc1.docx";
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
       
        List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
        org.docx4j.wml.ObjectFactory factory=new org.docx4j.wml.ObjectFactory();
        for (SectionWrapper sw : sectionWrappers)
        {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         HeaderPart header= hfp.getDefaultHeader();
         String xpath = "//w:r[w:t[contains(text(),'"+"[["+"label0"+"]]"+"')]]";
         List<Object> list = header.getJAXBNodesViaXPath(xpath, false);
         for(int i=0;i<list.size();i++)
         {         
            org.docx4j.wml.R r = (org.docx4j.wml.R) list.get(i);         
            org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();      
            org.docx4j.wml.RPr rpr=r.getRPr();
            int index = parent.getContent().indexOf(r);         
            parent.getContent().remove(r);         
            
            org.docx4j.wml.Text addedTmpText =factory.createText();
            org.docx4j.wml.R mainR = factory.createR();         
            mainR.setRPr(rpr);//保持原有文本属性
            addedTmpText.setValue("AAAA");
            mainR.getContent().add(addedTmpText);
            parent.getContent().add(index, mainR);                                          
         }
        }
               
        wordMLPackage.save(new File("C:\\", "headerFooter.docx"));


   }
}


i hope it's helpful to people in need :)

tosswang