Apache POI - manipulating .doc lose images

38 views Asked by At

Good morning everyone!
The intention was to do a simple text replace on a .doc file that was provided to me. I had already tried this procedure on plain .docx and it worked, but on this .doc it isn't working.

HWPFDocument docCandidato = null;

try {
    String filePath = getClass().getClassLoader().getResource("/templates/cv_template.doc").getPath();
    InputStream inputStream = new FileInputStream(filePath);
    POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
    docCandidato = new HWPFDocument(fileSystem);        
} catch (Exception e) {
    logger.error("--------> ERROR: ", e);
}
        
docCandidato = replaceText(docCandidato, "xxx", candidato.getNome().concat(" ").concat(candidato.getCognome()));

ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
    docCandidato.write(out);
    out.close();
    docCandidato.close();
} catch (IOException e) {
    logger.error("--------> ERROR: ", e);
}
private HWPFDocument replaceText(HWPFDocument doc, String originalText, String updatedText) {
    Range range = doc.getRange();
    range.replaceText(originalText, updatedText);
    return doc;
}

Beyond this, the most serious problem is that when I go to save the file flow without having manipulated anything, the images inside the document are lost.

Before: enter image description here

After: enter image description here

enter image description here

Has anyone ever had this problem?
How he solved it?

Thanks!

Save the template of a doc file without losing information.

0

There are 0 answers