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:
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.