wordpress hit counter
How to substitute text in docx. - WordprocessingML - Formats - OpenXML Developer

How to substitute text in docx.

Formats

Discussions about working with different Open XML Formats

How to substitute text in docx.

  • rated by 0 users
  • This post has 3 Replies |
  • 0 Followers
  • Hi,
     
    I would like to change documents docx in Groovy or JAVA language. I can replace image or graph, but i don't know how i have to do to substitute keyword ($$$textToSubstitute) by new text (String).
     
    I manage actually no problem to browse each paragraph (xwpfDoc.getParagraphs(i)), however in the next step I cannot browse the Run (I cannot find a method such paragraph.getRuns ()). This is a problem to replace the keyword with the replacement text in the right place, because I cannot use the specific Run containing the keyword.
     
    I cannot find method in Apache POI (XWPF) to browse Run one by one. despite the fact that this method exist in (HWPF : Apache POI for .doc file)
    ...
    int numCharRuns = paragraph.numCharacterRuns();
    for (int j = 0; j < numCharRuns; j++) {
    CharacterRun charRun = paragraph.getCharacterRun(j);
    text = charRun.text();
    if (text.contains(key)) {
    int start = text.indexOf(key);
    charRun.replaceText(key, value, start);
    }
    }

    ...
    How can i do to browse each  Run in a docx file, with the help of Apache POI APIs or other?
     
    Thank you for everyhelps!
  • You can use docx4j.

    The docx4j equivalent of paragraph.getRuns() is paragraph.getParagraphContent()

    There is also a method XmlUtils.unmarshallFromTemplate(String wmlTemplateString,
    java.util.HashMap mappings)
  • Hi,

    I'm working with docx4j. I'm trying to change the main title of the document and I do this:

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


            //MARSHAL
            String xml = XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true);
           
             //CHANGE (Replace a string)
            xml = xml.replaceAll("<w:t>Title of document</w:t>", "<w:t>New Title</w:t>");


            //UNMARSHAL
          wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(xml));
           
            // Now save it
              if (save) {
                    System.out.println("Saved.");
                    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
                    saver.save(outPutFileName);
                   
                }


    But... when I compile it, I get some exceptions like this:


    org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
        at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:319)
        at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:130)
        at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:80)
        at org.docx4j.samples.ChangeTitle.main(ChangeTitle.java:90)


    Could someone help me, please?
    Thank you!

  • I read in plutext forums that this problem has a solution: download the last release of docx4j 2.3.0, and with that, my program works perfectly. But finally, when i didn't know it, I changed my program, and now I do a text replace traversing the document, using org.docx4j.wml.Text objects. There is an example of it in docx4j samples.

    Thanks!
Page 1 of 1 (4 items)