I created a simple docx file consisting of 4 lines with simple text. I tried to manipulate some parts of the text by traversing over all org.docx4j.wml.Text elements:
- Code: Select all
WordprocessingMLPackage wpml = WordprocessingMLPackage.load(pathToDocX.toFile());
MainDocumentPart mdp = wpml.getMainDocumentPart();
Finder finder = new Finder(Text.class);
new TraversalUtil(mdp.getContent(), finder);
public static class Finder extends CallbackImpl {
protected Class<?> typeToFind;
public List<Object> results = new ArrayList<>();
protected Finder(Class<?> typeToFind) {
this.typeToFind = typeToFind;
}
public List<Object> apply(Object o) {
// Adapt as required
if (o.getClass().equals(typeToFind)) {
results.add(o);
}
return null;
}
}
I got all Text-Elements but not in one piece. I believe its because of my word document:
- Code: Select all
<w:document mc:Ignorable="w14 wp14">
<w:body>
<w:p>
<w:r>
<w:t>Das ist Text 1!</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Das ist Text 2!</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Das ist Text3!</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Das ist Text 4!</w:t>
</w:r>
</w:p>
<w:p/>
<w:p>
<w:r>
<w:t xml:space="preserve">Hier ein ganz langer </w:t>
</w:r>
<w:r>
<w:t>Text!</w:t>
</w:r>
<w:bookmarkStart w:name="_GoBack" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:sectPr>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
How can i handle such docx files with traversing? Should i traverse over paragraphs?
Thanks for help.