I’m encountering an issue with Apache POI where it fails to detect certain templates within a Word document, while it detects others fine.
Undetected templates remain unchanged in the output document.
This is my code:
@Override
public byte[] generateSingleScale(SingleScale singleScale) {
// Getting file
try {
XWPFDocument doc = new XWPFDocument(new FileInputStream(wordFile));
for (XWPFTable table : doc.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph paragraph : cell.getParagraphs()) {
String text = paragraph.getText();
if (text != null) {
// Conditional replacements
if (text.contains("[controlType]")) {replaceText(paragraph, "[controlType]", singleScale.getControlType());
}
if (text.contains("[owner]")) {replaceText(paragraph, "[owner]", singleScale.getOwner());
}
if (text.contains("[serialNumber]")) {replaceText(paragraph, "[serialNumber]", jednodelnoMerilo.getSerialNumber());
}
// More conditional replacements
}
}
}
}
}
// Rest of the code
return workingDocumentBytes;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private void replaceText(XWPFParagraph paragraph, String placeholder, String replacement) {
for (XWPFRun run : paragraph.getRuns()) {
String text = run.getText(0);
if (text != null && text.contains(placeholder)) {
run.setText(text.replace(placeholder, replacement), 0);
}
}
}
For example, it will nicely replace the owner and serial number, but not the type of control.
I checked that there are no errors in word by doing CTRL + F and copy-paste template and it finds everything without problems.
These are the dependencies I used:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
I’m using Word 2010 if that makes any difference.
I tried to implement the code differently, to change the template to something else and checked the size and font of the template in the document.