| Line | |
|---|
| 1 | package org.docx4j.samples; |
|---|
| 2 | |
|---|
| 3 | import org.docx4j.jaxb.Context; |
|---|
| 4 | import org.docx4j.openpackaging.contenttype.ContentType; |
|---|
| 5 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 6 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 7 | import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; |
|---|
| 8 | import org.docx4j.relationships.Relationship; |
|---|
| 9 | import org.docx4j.wml.CTAltChunk; |
|---|
| 10 | |
|---|
| 11 | public 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.