0

I am trying to do a placeholders replacement in .DOCX with Apache POI using the MergeField method. I am aware of the problem with splitting the text into different XWPFRun, so I chose the mentioned method.

I used this answer as the basis of the solution.

    public static void replace(Path input, Path output) throws Exception {
        try (var open = OPCPackage.open(Files.newInputStream(input))) {
            try (var document = new XWPFDocument(open)) {
                replace(document, Map.of("word", "WORD"));
                document.write(Files.newOutputStream(output, 
                      StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
            }
        }
    }

    public static void replace(XWPFDocument document, Map<String, String> placeholders) {
        for (var paragraph : document.getParagraphs()) {
            for (var run : paragraph.getRuns()) {
                var text = run.getText(0);
                if (text != null) {
                    // Pattern.compile("\\$\\{(?<name>[A-z0-9_]+?)\\}");
                    var matcher = FIND_PLACEHOLDERS_PATTERN.matcher(text); 
                    if (matcher.find() && matcher.groupCount() > 0) {
                        var groupName = matcher.group("name");
                        var placeholder = placeholders.get(groupName);
                        if (placeholder != null) {
                            String replaced = matcher.replaceAll(placeholder);
                            run.setText(replaced, 0);
                        }
                    }
                }
            }
        }
    }

Input:

input

Output:

output

It would seem that everything works fine, but quotes remain, how can I get rid of them or what am I doing wrong? Quotes are added automatically to the placeholder, I can't get rid of them.

  • 1
    Apache POI does not provide methods to fill mail merge fields properly. To replace text placeholders, it provides [TextSegment](https://poi.apache.org/apidocs/dev/org/apache/poi/xwpf/usermodel/TextSegment.html). Examples see in my answers: [containing TextSegment](https://stackoverflow.com/search?tab=newest&q=user%3a3915431%20TextSegment&searchOn=3). – Axel Richter May 27 '23 at 05:34

0 Answers0