Search code examples
javaopenxmldocx4j

What is RangeFinder("CTBookmark", "CTMarkupRange") in docx4j?


In the BookmarksReplaceWithText.java sample for docx4j, I see the following line. What does RangeFinder do exactly? I could not find a clear description.

RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");

Solution

  • From what I understand, the RangeFinder gathers all of the bookmark start and end points in the xml file. The RangeFinder needs to be put into a TraversalUtil along with the object you are searching in (can be the whole document or a paragraph or whatever):

    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(paragraphs, rt);
    

    Once this is done, you can call rt.getStarts() or rt.getEnds() to get a list of the start and end bookmark locations. What I mean by this is if you look at the XML file of a document you will find bookmark starts and bookmark ends for example:

    <w:bookmarkStart w:id="1" w:name="BookmarkName"/>
        <w:r>
            <w:t>bookmark</w:t>
        </w:r>
    <w:bookmarkEnd w:id="1"/>
    

    With this you can replace the content as described in BookmarksReplaceWithText.java or any number of other things.