国内的资料很少,有的都只有默认的图片插入,默认为”嵌入型”,没有相应的文档,最近项目需要,将图片设置为文字环绕”四周型”,经过翻看官方论坛和对比XML文件,先看效果。

方法如下,需要使用anchor,由于设置环绕方式后,就像在HTML设置了浮动,需要清除浮动,我找到的方法是在末尾插入一个空的表格,并设置白色,这样就不能看出来有表格,样式也达到要求。
docx4版本 , 3.2.2

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
79
80
81
82
83
84
85
86
87
88
89
//代码片段

//创建段落
P p = factory.createP();
R r = factory.createR();
p.getContent().add(r);

//创建默认图片对象
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, imgBytes);
Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", 1, 2, false);

//将默认的inline图片对象转换为anchor
Anchor anchor = createImageAnchor(inline);

//添加到drawing对象
Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(anchor);

//drawing对象添加到行
r.getContent().add(drawing);

//将段落添加到文档
wordMLPackage.getMainDocumentPart().addObject(p);


//由于有浮动,插入一个表格清除浮动,防止下面内容上移
Tbl table = factory.createTbl();
table.setTblPr(new TblPr());
//addBorders(table);
TblWidth tblW = new TblWidth();
tblW.setW(new BigInteger("10456"));//重要,设置表格宽度,不设置表格将没有宽度,无法达到清除浮动效果,这个宽度是窄边框宽度
table.getTblPr().setTblW(tblW);
Tr tr = factory.createTr();
P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("");//添加空内容
addTableCell(tr, paragraphOfText);
table.getContent().add(tr);

//将表格添加到文档
wordMLPackage.getMainDocumentPart().addObject(table);


/**
 * 给表格添加简单的黑色边框.
 *
 * @param table
 */

private static void addBorders(Tbl table) {
    CTBorder border = new CTBorder();
    border.setColor("#ffffff");
    border.setSz(new BigInteger("4"));
    border.setSpace(new BigInteger("0"));
    border.setVal(STBorder.SINGLE);

    TblBorders borders = new TblBorders();
    borders.setBottom(border);
    borders.setLeft(border);
    borders.setRight(border);
    borders.setTop(border);
    borders.setInsideH(border);
    borders.setInsideV(border);

    table.getTblPr().setTblBorders(borders);
}

 /**
     * 将图片添加到R中
     */

public static Anchor createImageAnchor(Inline inline) throws JAXBException {
    String anchorXml = XmlUtils.marshaltoString(inline, true, false, Context.jc, Namespaces.NS_WORD12, "anchor",
            Inline.class);

    org.docx4j.dml.ObjectFactory dmlFactory = new org.docx4j.dml.ObjectFactory();
    org.docx4j.dml.wordprocessingDrawing.ObjectFactory wordDmlFactory = new org.docx4j.dml.wordprocessingDrawing.ObjectFactory();

    Anchor anchor = (Anchor) XmlUtils.unmarshalString(anchorXml, Context.jc, Anchor.class);
    anchor.setSimplePos(dmlFactory.createCTPoint2D());
    anchor.getSimplePos().setX(0L);
    anchor.getSimplePos().setY(0L);
    anchor.setSimplePosAttr(false);
    anchor.setPositionH(wordDmlFactory.createCTPosH());
    anchor.getPositionH().setPosOffset(0);
    anchor.getPositionH().setRelativeFrom(STRelFromH.COLUMN);
    anchor.setPositionV(wordDmlFactory.createCTPosV());
    anchor.getPositionV().setPosOffset(104652);
    anchor.getPositionV().setRelativeFrom(STRelFromV.PARAGRAPH);
    anchor.setWrapSquare(wordDmlFactory.createCTWrapSquare());
    anchor.getWrapSquare().setWrapText(STWrapText.BOTH_SIDES);
    return anchor;
}
doc4xj 在word中插入图片并设置环绕方式