convert content control's content to html
Posted: Thu Aug 23, 2012 5:39 pm
Hi, all! I want to convert content control's content to html. i guess the method is as follows:
1. traverse docx as 'sample\ContentControlsInfoStructure.java', find every content control
2. insert every content control in a new docx respectively
3. convert every docx to html as 'sample\ConvertOutHtml.java'.
this my code snippet:
however, there will be a 'java.lang.NullPointerException' when calling 'wordMLPackage.getMainDocumentPart().addObject(element)'. please help!
1. traverse docx as 'sample\ContentControlsInfoStructure.java', find every content control
2. insert every content control in a new docx respectively
3. convert every docx to html as 'sample\ConvertOutHtml.java'.
this my code snippet:
- Code: Select all
public class ParseDocx {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String input_DOCX = "resource/new.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(input_DOCX));
TraversalUtilContentControlVisitor visitor = new TraversalUtilContentControlVisitor();
IndentingVisitorCallback contentControlCallback = new IndentingVisitorCallback(visitor);
visitor.callback = contentControlCallback;
contentControlCallback.walkJAXBElements(
wordMLPackage.getMainDocumentPart().getJaxbElement() );
}
public static class TraversalUtilContentControlVisitor extends TraversalUtilVisitor<SdtElement> {
IndentingVisitorCallback callback; // so we can get indentation
@Override
public void apply(SdtElement element, Object parent, List<Object> siblings) {
WordprocessingMLPackage wordMLPackage = new WordprocessingMLPackage();
if (element.getSdtContent() != null)
wordMLPackage.getMainDocumentPart().addObject(element);
try {
wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/xxx.docx") );
} catch (Docx4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static class IndentingVisitorCallback extends SingleTraversalUtilVisitorCallback {
public IndentingVisitorCallback(TraversalUtilVisitor visitor) {
super(visitor);
}
String indent = "";
@Override
public void walkJAXBElements(Object parent) {
List children = getChildren(parent);
if (children != null) {
String oldIndent = indent;
indent += " ";
for (Object o : children) {
o = XmlUtils.unwrap(o);
this.apply(o, parent, children);
if (this.shouldTraverse(o)) {
walkJAXBElements(o);
}
}
indent = oldIndent;
}
}
}
}
however, there will be a 'java.lang.NullPointerException' when calling 'wordMLPackage.getMainDocumentPart().addObject(element)'. please help!