Question

I do a search and replace on a word document by using docx4j and the following code:

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(pathFinder.findUploadPath() + filename));

        //Get all text elements out of the doc
        List texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);

        // Loop through all "text" elements
        for (Object obj : texts) {
            Text text = (Text) ((JAXBElement) obj).getValue(); //get the value of the object

            // Get the string value
            String textValueBefore = text.getValue();

            text.setValue(string_afterwards);
        }

The "string_afterwards" is a generated string by some other code lines.

My problem is, to format this string, so that it will appear in bold style.
Is there any chance to do it, without changing the search&replace lines?
Something like to add a tag < b > to the string?

Was it helpful?

Solution

You'll need to change XPATH_TO_SELECT_TEXT_NODES so it selects the parent w:r element of the text nodes of interest.

Then make sure the w:r has a w:rPr (run properties element), and set that to bold.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top