Bug 62138 - Undefined results for insertNewRun() method when XWPFParagraph contains SDT runs
Summary: Undefined results for insertNewRun() method when XWPFParagraph contains SDT runs
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 3.14-FINAL
Hardware: PC All
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2018-02-27 11:22 UTC by radu.c.stefanescu@gmail.com
Modified: 2018-03-17 23:29 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description radu.c.stefanescu@gmail.com 2018-02-27 11:22:18 UTC
I have a paragraph that contains one SDT element (Checkbox) on position 0 and some text on the next runs. I want to insert a text run BEFORE the SDT element in the paragraph, but calling the following method does not yield the expected results:


The insertNewRun method, when doing the synchronization between the Run list and the IRun list, will make no assumptions about the ordering of XWPFSDT and XWPFRun types of elements in the IRun list, which in this case is not yielding the desired result, as a client of the API will not be able to order the sequence of IRunElements as desired.

In the example before, the new text run (XWPFRun) is inserted on position 1, rather then before the XWPFSDT element.
This is happening due to this logic:

// To update the iruns, find where we're going
            // in the normal runs, and go in there
            int iPos = iruns.size();
            if (pos < runs.size()) {
                XWPFRun oldAtPos = runs.get(pos);
                int oldAt = iruns.indexOf(oldAtPos);
                if (oldAt != -1) {
                    iPos = oldAt;

Possible solution: enrich the API for XWPFParagraph with a method that inserts directly in the IRun list in order to be able to control the correct order of runs.