import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;
import org.docx4j.wml.Numbering;
import org.docx4j.XmlUtils;
public class ListDefTest {
private final static String filename = "C:/temp/ListDefTestOut.docx";
static org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
public static void main(String[] args) throws Exception {
int altChunkCount = 0;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainPart = wordMLPackage.getMainDocumentPart();
String l_content =
"
" +
" - Introduction
" +
" - Major players " +
"
" +
" - Google
" +
" - Apple
" +
" - Microsoft
" +
" - Motorola
" +
"
" +
" " +
" - Locations " +
"
" +
" - New York
" +
" - Chicago
" +
" - Atlanta
" +
" - Phoenix
" +
"
" +
" " +
"
";
String BulletListXML =
"" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" ";
/*
* Add numbering part
*/
System.out.println("Adding custom NumberingDefinitionsPart");
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
Numbering num = (Numbering) XmlUtils.unmarshalString(BulletListXML);
ndp.setJaxbElement( num );
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
System.out.println("Finished adding custom NumberingDefinitionsPart");
/*
* Now add the altChunk
*/
String _body = "" + l_content + "";
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/test" + altChunkCount++ + ".html"));
afiPart.setContentType(new ContentType("text/html"));
afiPart.setBinaryData(_body.getBytes());
Relationship altChunkRel = mainPart.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
mainPart.addObject(chunk);
wordMLPackage.save(new java.io.File(filename));
System.out.println("Saved " + filename);
}
}