I have a project that need to read all content of a docx file, but i dont know how to get it. All of thing i can get is just list of paragraphs. I wanna get data inside Textbox too Here is my code:
List<Object> texts = getAllElementFromObject(document.getMainDocumentPart(), P.class);
I tried to use method getAllElementFromObject(document.getMainDocumentPart(), CTTextbox.class);
but still cant get Textbox data.
My method getAllElementFromObject()
:
public static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
A text box create in Word looks something like:
Here the relevant objects are:
Your code isn't going to work since Pict doesn't implement ContentAccessor.
So instead, please try https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/finders/ClassFinder.java