source: trunk/docx4j/src/main/java/org/docx4j/samples/AltChunkHtml.java @ 1728

Revision 1728, 1.4 KB checked in by jharrop, 6 months ago (diff)
Line 
1package org.docx4j.samples;
2
3import org.docx4j.jaxb.Context;
4import org.docx4j.openpackaging.contenttype.ContentType;
5import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
6import org.docx4j.openpackaging.parts.PartName;
7import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
8import org.docx4j.relationships.Relationship;
9import org.docx4j.wml.CTAltChunk;
10
11public class AltChunkHtml {
12
13        /**
14         * @param args
15         * @throws Exception
16         */
17        public static void main(String[] args) throws Exception {
18
19                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
20
21                String html = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>"; 
22                AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); 
23                afiPart.setBinaryData(html.getBytes()); 
24                afiPart.setContentType(new ContentType("text/html")); 
25                Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart); 
26                // .. the bit in document body
27                CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); 
28                ac.setId(altChunkRel.getId() ); 
29                wordMLPackage.getMainDocumentPart().addObject(ac); 
30                // .. content type
31                wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html"); 
32                wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/test.docx"));           
33        }
34
35}
Note: See TracBrowser for help on using the repository browser.