I found that people use docx4j to modify docx's. I went through 'getting started' and I believe I have basic knowledge of this lib works.
What I want to achieve is to add basic text to the beginning of document (before any other text). I managed to add text to the end of file. Here is the code:
for(File file: folder.listFiles())
{
if(file.getName().contains("docx"))
{
try
{
WordprocessingMLPackage docx = WordprocessingMLPackage.load(file);
docx.getMainDocumentPart().addParagraphOfText(toAppend);
docx.save(new File(file.getAbsolutePath()));
}
catch (Docx4JException e)
{
e.printStackTrace();
}
}
}
but It doesn't behave in the way that I expected. It appends text to the eof. How can I add text before MainDocumentPart, not after? Also I would like to keep code clean and simple to read.