feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
 
/**
 * Create a docx containing an XHTML AltChunk,
 * and then convert that to normal docx content.
 * @author jharrop
 *
 */
public class AltChunkXHTMLRoundTrip {
 
    /**
     * @param args
     */
    public static void main(String[] args)  throws Exception {
 
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
        
        mdp.addParagraphOfText("Paragraph 1");
 
        // Add the XHTML altChunk
        String xhtml = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>"; 
        mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes());
        
        mdp.addParagraphOfText("Paragraph 3");
        
        // Round trip
        WordprocessingMLPackage pkgOut = mdp.convertAltChunks();
        
        // Display result
        System.out.println(
                XmlUtils.marshaltoString(pkgOut.getMainDocumentPart().getJaxbElement(), true, true));
        
    }
 
}