I have example document.docx
Example:
Task: to detect a text field, analyze the text and replace it if necessary. Thanks to other answers on stackoverflow, I managed to find the text, but I have no idea how to replace it. Can someone help me?
public class POITest {
public static void main(String[] args) throws Exception {
final var directory = Paths.get("test");
final var input = directory.resolve("new_debug_input.docx");
final var output = directory.resolve("new_debug_output.docx");
try (var open = OPCPackage.open(Files.newInputStream(input))) {
try (var document = new XWPFDocument(open)) {
for (XWPFParagraph paragraph : document.getParagraphs()) {
printContentsOfTextBox(paragraph, builder);
}
document.write(Files.newOutputStream(output,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING));
}
}
}
private static void printContentsOfTextBox(XWPFParagraph paragraph, StringBuilder builder) {
XmlObject[] objects = paragraph.getCTP().selectPath(
"declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'" +
"declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape'" +
"declare namespace v='urn:schemas-microsoft-com:vml'" +
".//*/wps:txbx/w:txbxContent | .//*/v:textbox/w:txbxContent"
);
{
System.out.println("objects.length = " + objects.length);
for (XmlObject object : objects) {
try {
XmlObject[] children = object.selectChildren(
new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "p")
);
System.out.println("children.length = " + children.length);
for (final XmlObject child : children) {
final var childParagraph = new XWPFParagraph(CTP.Factory.parse(child.xmlText()), paragraph.getBody());
final var text = childParagraph.getText();
if (!text.isEmpty()) {
System.out.println(text);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
output:
objects.length = 2
children.length = 2
TEXT IN TEXT BOX
TEXT IN TEXT BOX
children.length = 2
TEXT IN TEXT BOX
TEXT IN TEXT BOX
Why duplicated ?
UPD #1:
As I understand it, to get the existing text, we parse an xml object, creating a new paragraph based on it, from which we then get the text, but if we replace the text in it, it will have no effect on the document.
UPD #2:
changeText(paragraph, "REPLACED TEXt");
Result: