Page 1 of 1

Best Approach to Search & Replace in a Template + Merge

PostPosted: Sat Mar 31, 2012 3:57 am
by CesarDRK
Hi to all !
I have the following scenario, and need some advice:

The user will input a word document as a template, and provide some parameters in runtime so i can query my database and get data to fill the document.
So, there are two basic things i need to do:

1- Replace every key in the document with it´s respective result from the current query line.
2- "Merge" (copy? duplicate?) the existing document unchanged into itself (append) depending on how many rows i got from the query, and replacing the keys from this new copy with the next row values.

What´s is the best aprroach to do this? I´ve managed to do the replace part for now, by using the unmarshallfromtemplate providing it a hashmap.
But this way is a little bit tricky, because i need to add "${variable_name}" in the document, and sometimes word separates "${" and "}" in different tags, causing issues.

I´ve read about the custom xml binding, but didn´t understand it completely. I need to generate a custom XML, inject it in the document (all of this un runtime) and call the applybindings?? If this is true, how would i bind the fields in the document to the xml ? By name?

Thanks in advance.

Re: Best Approach to Search & Replace in a Template + Merge

PostPosted: Mon Apr 02, 2012 10:31 pm
by jason
CesarDRK wrote:I´ve read about the custom xml binding, but didn´t understand it completely. I need to generate a custom XML, inject it in the document (all of this un runtime) and call the applybindings?? If this is true, how would i bind the fields in the document to the xml ? By name?


It is true :-)

The fields (ie content controls) are bound using an XPath expression. You can add these using a suitable Word Add-In. See http://www.opendope.org for further info.

There is a bit of a learning curve to get started, but the benefits include:
- repeat of table rows etc
- conditional inclusion/exclusion of content
- inclusion of images
- import of HTML snippets

Re: Best Approach to Search & Replace in a Template + Merge

PostPosted: Mon Apr 09, 2012 9:36 pm
by sureshbabubv
Hi,

Try this code to replace a static text in a document to any object

Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
         .load(new java.io.File(existingfileName));


MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();


   String xpath = "//w:r[w:t[contains(text(),'codetobereplaced')]]";

   List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);

   if (list.isEmpty()) {
      System.out.println("Couldn't find the run");
      return;
   }

   R r = (R) list.get(0);
   P parent = (P) r.getParent();



   Text tmp = objFactory.createText();
      tmp.setValue("sureshbabu");

   R  run = factory.createR();
   
     run.getContent().add(tmp );

    parent.getContent().remove(r);
    
   parent.getContent().add(run);


wordMLPackage.save(new File(newfilename));




Thanks & Regards,
B.V.Suresh Babu.