3

I have an array list that I want to use to create a new bullet list inside a document.

I already have numbering (with numbers) and I want to have both (number and bullet) on different lists.

My document is pre-populated with some data and I have some tokens who determine where go my data. For my list, I have token who is like this one and I able to reach it.

  • {{tokenlist1}}

I want to :

first option : reach my token, create a new bullet list and delete my token

second option : replace my token by my first element and continue my bullet list.

It would be really appreciated if the bullet form (square, round, check, ....) can stay the same as they are with the token.

centic
  • 15,565
  • 9
  • 68
  • 125
WEGSY85
  • 291
  • 1
  • 3
  • 21

1 Answers1

2

EDIT

for those who want an answer here's my solution.

Action

Map<String, Object> replacements = new HashMap<String, Object>();
        replacements.put("{{token1}}", "texte changé 1");
        replacements.put("{{token2}}", "ici est le texte du token numéro 2");
        replacements.put("{{tokenList1}}", tokenList1);
        replacements.put("{{tokenList2}}", tokenList1);         
        
        templateWithToken = reportService.findAndReplaceToken(replacements, templateWithToken);

Service

public XWPFDocument findAndReplaceToken (Map<String, Object> replacements,
        XWPFDocument document) {
    List<XWPFParagraph> paragraphs = document.getParagraphs();
    for (int i = 0; i < paragraphs.size(); i++) {
        XWPFParagraph paragraph = paragraphs.get(i);

        List<XWPFRun> runs = paragraph.getRuns();
        for (Map.Entry<String, Object> replPair : replacements
                .entrySet()) {
            String find = replPair.getKey();
            Object repl = replPair.getValue();
            TextSegment found =
                    paragraph.searchText(find, new PositionInParagraph());
            if (found != null) {
                if (repl instanceof String) {
                    replaceText(found, runs, find, repl);
                } else if (repl instanceof ArrayList<?>) {

                    Iterator<?> iterArrayList =
                            ((ArrayList) repl).iterator();

                    boolean isPassed = false;

                    while (iterArrayList.hasNext()) {
                        Object object = (Object) iterArrayList.next();

                        if (isPassed == false) {
                            replaceText(found, runs, find,
                                    object.toString());
                        } else {
                            XWPFRun run = paragraph.createRun();
                            run.addCarriageReturn();
                            run.setText(object.toString());
                        }

                        isPassed = true;
                    }
                }
            }
        }
    }
    return document;
}

private void replaceText(TextSegment found, List<XWPFRun> runs,
        String find, Object repl) {
    int biginRun = found.getBeginRun();
    int biginRun2 = found.getEndRun();
    if (found.getBeginRun() == found.getEndRun()) {
        // whole search string is in one Run
        XWPFRun run = runs.get(found.getBeginRun());
        String runText = run.getText(run.getTextPosition());
        String replaced = runText.replace(find, repl.toString());
        run.setText(replaced, 0);
    } else {
        // The search string spans over more than one Run
        // Put the Strings together
        StringBuilder b = new StringBuilder();
        for (int runPos = found.getBeginRun(); runPos <= found
                .getEndRun(); runPos++) {
            XWPFRun run = runs.get(runPos);
            b.append(run.getText(run.getTextPosition()));
        }
        String connectedRuns = b.toString();
        String replaced = connectedRuns.replace(find, repl.toString());

        // The first Run receives the replaced String of all
        // connected Runs
        XWPFRun partOne = runs.get(found.getBeginRun());
        partOne.setText(replaced, 0);
        // Removing the text in the other Runs.
        for (int runPos = found.getBeginRun() + 1; runPos <= found
                .getEndRun(); runPos++) {
            XWPFRun partNext = runs.get(runPos);
            partNext.setText("", 0);
        }
    }
}
Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
WEGSY85
  • 291
  • 1
  • 3
  • 21
  • What about the bulleting then? – Priidu Neemre Aug 11 '21 at 09:14
  • It's been a while since I did that code, but the bullet point is added in the template and that code adds the following bullet points automatically. I could validate to be sure, but I'm pretty sure it's the case. – WEGSY85 Aug 11 '21 at 13:13