You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The API of the library seems a little strange to me. Either the changes were persisted to the template file as well as to the target file or the changes were not persisted at all.
Therefore I make now a copy for my own of the template and then for some reason one has to call document.write(...) otherwise the changes are not persisted to the template file. It would be nice, if the API would be simpler to use.
Here is the code that i used in the end:
public void processDocxTemplate() {
if (!TEMPLATE_FILE.isFile()) {
throw new IllegalArgumentException("The template file does not exist: " + TEMPLATE_FILE);
}
if (OUTPUT_FILE.exists()) {
throw new IllegalArgumentException("The output file does already exist: " + OUTPUT_FILE);
}
copyFile(TEMPLATE_FILE, OUTPUT_FILE);
try (XWPFDocument document = new XWPFDocument(OPCPackage.open(OUTPUT_FILE))) {
WordReplacer replacer = new WordReplacer(document);
placeholderToReplacement.forEach(replacer::replaceWordsInText);
// For some strange reason we need the following write call, otherwise the changes are not persisted.
document.write(new ByteArrayOutputStream());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to processDocxTemplate.", e);
}
}
private void copyFile(File sourceFile, File targetFile) {
try {
FileUtils.copyFile(sourceFile, targetFile);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to copyFile.", e);
}
}
The text was updated successfully, but these errors were encountered:
The API of the library seems a little strange to me. Either the changes were persisted to the template file as well as to the target file or the changes were not persisted at all.
Therefore I make now a copy for my own of the template and then for some reason one has to call
document.write(...)
otherwise the changes are not persisted to the template file. It would be nice, if the API would be simpler to use.Here is the code that i used in the end:
The text was updated successfully, but these errors were encountered: