I'm working on a spring boot project using thymeleaf I need to create a Word file (.docx) based on an existing one (as a template) that contains styles, tables, images... , and also I need to modify and customize some lines at the same time. I checked some codes and answers about the Apache POI Library but nothing works for my case.
Any ideas PLEASE! Thanks and regards
I solved my problem using this code :
public void wordFileCreator() throws IOException
{
String filename = "Template.docx";
try {
XWPFDocument doc = new XWPFDocument(OPCPackage.open(new FileInputStream(filename)));
List<XWPFParagraph> paragraphList = doc.getParagraphs();
for (XWPFParagraph para : paragraphList) {
for (XWPFRun run : para.getRuns()) {
String text = run.text();
if(text.contains("StringToReplace")){
text = text.replace("StringToReplace", "newVal");
run.setText(text, 0);
}
}
}
doc.write(new FileOutputStream("newFile.docx"));
} catch (Exception e) {
e.printStackTrace();
}
}