feiyu02
2022-11-15 23bd719cebe5feeff4e48fde925b0b39755eea93
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Pict;
 
import java.io.File;
 
public class ImageAddWMF {
 
    public static void main(String[] args) throws Exception {
        
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        
        // The image to add
        File file = new File(System.getProperty("user.dir") 
                + "/image1.wmf" );
        
        java.io.InputStream is = new java.io.FileInputStream(file );
 
        MetafileWmfPart imagePart = new MetafileWmfPart(new PartName("/word/media/image1.wmf"));
        imagePart.setBinaryData(is);
        Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(imagePart);
 
        org.docx4j.wml.P p = newImage( rel, "width:33pt;height:15pt", "image1" ); // for best results, supply the size
        wordMLPackage.getMainDocumentPart().addObject(p);
 
        
        
        // Now save it 
        wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_ImageAddWMF.docx") );
        
    }
 
    /**
     * Create image, without specifying width
     */
    public static org.docx4j.wml.P newImage(Relationship rel, String style, String title) throws Exception {        
        
        Pict pict = createImageInline( rel, style, title);
        
        // Now add the inline in w:p/w:r/w:drawing
        org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
        org.docx4j.wml.P  p = factory.createP();
        org.docx4j.wml.R  run = factory.createR();        
        p.getContent().add(run);        
        run.getContent().add(pict);        
        
        return p;
        
    }    
    
    public static Pict createImageInline(Relationship rel, String style, String title) throws Exception {
        
        
        String ml =
                "<w:pict " + namespaces + ">"
        +"<v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"" + style + "\">"
        +"  <v:imagedata r:id=\""+rel.getId()+"\" o:title=\""+title+"\"/>"
        +"</v:shape>"
      +"</w:pict>";                
                
        Pict pict = (Pict) org.docx4j.XmlUtils.unmarshalString(ml);
        
        return pict;        
    }
 
    final static String namespaces = " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" "
            + "xmlns:v=\"urn:schemas-microsoft-com:vml\" "
            + "xmlns:o=\"urn:schemas-microsoft-com:office:office\" "
            + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" "
            + "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\""
            ;
    
}