I'm trying to get the text entered in an activex TextBox but cannot figure how to get deeper than the ActiveXControlXmlPart object I identify with that code :
- Code: Select all
import java.io.File;
import java.util.List;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.TraversalUtil.Callback;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.ActiveXControlXmlPart;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.CTObject;
public class TestDocx4j {
/**
* @param args
*/
public static void main(String[] args) {
TestDocx4j testDocx4j = new TestDocx4j();
try {
// load package
WordprocessingMLPackage wordMLPackage = Docx4J.load(new File("test.docm"));
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
testDocx4j.traverseDocument(mdp);
} catch (Docx4JException e) {
e.printStackTrace();
}
}
private void traverseDocument(final MainDocumentPart mdp) {
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) mdp.getJaxbElement();
Body body = wmlDocumentEl.getBody();
new TraversalUtil(body,
new Callback() {
String indent = "";
@Override
public List<Object> apply(Object o) {
String text = o.toString();
if (o instanceof org.docx4j.wml.Text)
text = ((org.docx4j.wml.Text) o).getValue();
else if (o instanceof CTObject) {
CTObject ctObj = (CTObject) o;
String r = ctObj.getControl().getId();
Part relPart = mdp.getRelationshipsPart().getPart(r);
if (relPart instanceof ActiveXControlXmlPart) {
ActiveXControlXmlPart actXPart = (ActiveXControlXmlPart) relPart;
text = "ActiveXPart named: " + actXPart.getPartName();
text += " typed: " + actXPart.getContentType();
}
}
System.out.println(indent + o.getClass().getName() + " \"" + text + "\"");
return null;
}
@Override
public boolean shouldTraverse(Object o) {
return true;
}
// Depth first
@Override
public void walkJAXBElements(Object parent) {
indent += " ";
List<Object> children = getChildren(parent);
if (children != null) {
for (Object o : children) {
// if its wrapped in javax.xml.bind.JAXBElement, get its
// value
o = XmlUtils.unwrap(o);
this.apply(o);
if (this.shouldTraverse(o)) {
walkJAXBElements(o);
}
}
}
indent = indent.substring(0, indent.length() - 4);
}
@Override
public List<Object> getChildren(Object o) {
return TraversalUtil.getChildrenImpl(o);
}
}
);
}
}
Can someone help me (see attached file)?
Fred