Page 1 of 1

Is there is simple way to replace a part of a text/document?

PostPosted: Thu Nov 25, 2010 12:01 am
by mavfunhome
Hey,

i am looking for a simple way to replace a word or part of text in a docx-File.
I have already read the "Getting Started - Tutorial/howto". But I think i don't get it. :?
I was reading the part "Adding a paragraph of text". Is this the right one to solve my problem?

I dont want to append a new part at the bottom of the document. I want to replace a word in the middle of the text.

This is a part of the code to get the element/word/text i want.

Code: Select all
//Auslesen der eizelnen Absätze (w:p)
      String xpath = "//w:p";
      try {
         List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
         System.out.println("\nJaxbNote via Xpath\n\n");
         for(int i=0; i<list.size();i++){
            String p =list.get(i).toString();
            
            
            if(p.equalsIgnoreCase("überschrift")){
               System.out.println("ALT: "+p);
               Object obj = list.get(i);
               System.out.println(list.get(i).toString());
               System.out.println("NEU: "+list.get(i).toString());
            }
         }
         
         System.out.println("\nJaxbNote via Xpath ENDE\n\n");
                        System.out.println("Grüße aus Deutschland! :-)");
      } catch (JAXBException e1) {
         e1.printStackTrace();
      }


best regards :geek:

Re: Is there is simple way to replace a part of a text/document?

PostPosted: Thu Nov 25, 2010 11:43 am
by jason
Assuming you have found the org.docx4j.wml.P object you are looking for, what do you want to do with it exactly?

It contains

Code: Select all
    protected List<Object> paragraphContent;


accessed via:

Code: Select all
   public List<Object> getParagraphContent()


The text of a simple para will be in its w:r/w:t elements.

You can iterate over the paragraphContent and manipulate it. That's a general approach. Note that each run object contains a List of its contents. In fact, many objects have a list containing their contents.

If you have a known string you are trying to replace, a simpler way to do that would be to marshall the entire para to a string of XML, do the replacement, then unmarshall again. See the XmlUtils class.

A better approach (depending on your application, of course) may be to use content control data bindings. See Getting Started for details.

hope this helps .. Jason

Re: Is there is simple way to replace a part of a text/document?

PostPosted: Tue Dec 07, 2010 1:14 am
by mavfunhome
Is there a tutorial how to manipulate a single paragraph?

Can't find anything like:

Code: Select all
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
list.set(i, "newText"); // i = 1,2,3,...


I can interate over the list.
I use
Code: Select all
System.out.println(list.get(i).toString());
to see the content of the paragraph
But I can't replace an object or paragraph.


I want to replace <w:t>Hello world</w:t> with <w:t>NEW Text</w:t>
Code: Select all
<w:p  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:r>
        <w:t>Hello world</w:t>
    </w:r>
</w:p>


For me the "Getting Started" is little bit confusing.



Edit:
I tried:
Code: Select all
ObjectFactory factory = Context.getWmlObjectFactory();

..to create a Object.

this is the result:
Code: Select all
Type mismatch: cannot convert from org.docx4j.wml.ObjectFactory to org.docx4j.customxml.ObjectFactory


I alway get missmatches if i copy&paste code from the "Getting Started".

I want to use:
Code: Select all
list.set(index, element);

to replace a paragraph of text. But I don't know to create the object (element).

Re: Is there is simple way to replace a part of a text/document?

PostPosted: Tue Dec 07, 2010 7:38 am
by jason
Code: Select all
Type mismatch: cannot convert from org.docx4j.wml.ObjectFactory to org.docx4j.customxml.ObjectFactory


You must have an unwanted import statement:

Code: Select all
import org.docx4j.customxml.ObjectFactory


Get rid of that, or use:

Code: Select all
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();