I need help on how to use content control and altChunk together to insert a docx into another docx. I find a useful link talking about this using Open XML SDK. http://devmeat.com/show/172347. Basically it define a content control in template and at run time try to locate the content control and replace it with altChunk.
I tried to do the same thing using docx4j API, I managed to locate the content control, but can not delete it and insert an altChunk at the content control's place. Is there anyone know how to do this? The following is my code.
- Code: Select all
private static void insertDocument(WordprocessingMLPackage wordMLPackage) throws Exception {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String xpathSdt = "//w:sdt";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpathSdt, false);
for(Iterator<Object> it = list.iterator(); it.hasNext();) {
Object o = XmlUtils.unwrap(it.next());
if (o instanceof SdtBlock ) {
SdtBlock sdt = (SdtBlock)o;
Tag tag = sdt.getSdtPr().getTag();
String val = tag.getVal();
if (val != null && val.startsWith("insertdoc=")) { // my content control has tag start with "insertdoc="
//find the name of the file to be inserted.
String fileName = val.substring(val.indexOf("=") + 1);
//insert altChunk. how to do this?
//try to remove the content control, but after save, the content control still exists
it.remove();
}
}
}
}