Docx4j开发手册-5|Docx4j的图片信息

一、通过Docx4j向文档添加照片

本章节代码来自docx4j项目github开源代码,有所改动。

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
90
91
92
93
94
public class AddImage {
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File("D:/XXXX/XXXX.docx"));
// 要添加的图像
File file = new File("D:/XXXX/1.png");

InputStream is = new FileInputStream(file);
long length = file.length();
// 不能创建long型数组。
// 必须是int类型。
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// 确保已读入所有字节
if (offset < bytes.length) {
System.out.println("无法完全读取文件" + file.getName());
}
is.close();

String filenameHint = null;
String altText = null;
int id2 = 1;


// 图像1:未指定宽度
P p = newImage(wordMLPackage, bytes, filenameHint, altText, id2);
wordMLPackage.getMainDocumentPart().addObject(p);

// 图2:宽度3000
P p2 = newImage(wordMLPackage, bytes, filenameHint, altText, id2, 3000);
wordMLPackage.getMainDocumentPart().addObject(p2);

// 图3:宽度6000
P p3 = newImage(wordMLPackage, bytes, filenameHint, altText, id2, 6000);
wordMLPackage.getMainDocumentPart().addObject(p3);

// Now save it
wordMLPackage.save(new File("D:/Desktop/image.docx"));

}

/**
* 创建图像,不指定宽度
*/
public static P newImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint, String altText, int id2) throws Exception {

BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

Inline inline = imagePart.createImageInline(filenameHint, altText,
wordMLPackage.getDrawingPropsIdTracker().generateId(), id2, false);

// 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);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);

return p;

}

/**
* 创建图像,以斜纹为单位指定宽度
*/
public static P newImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint, String altText, int id2, long cx) throws Exception {

BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

Inline inline = imagePart.createImageInline(filenameHint, altText,
wordMLPackage.getDrawingPropsIdTracker().generateId(), id2, cx, false);

// 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);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);

return p;

}
}

二、通过Docx4j从文档获取照片保存到硬盘

以下代码仅未测试Demo,未对属性类型做严格检查,使用时请注意。

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
public static void main(String[] args) throws Exception {
// 加载 DOCX 文件
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("D:/XXXX/XXXX.docx"));

MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();

List<Object> content = mainDocumentPart.getContent();

for (Object o : content) {
P p = (P) o;
List<Object> content1 = p.getContent();
for (Object object : content1) {
if (object instanceof R) {
R r = (R) object;
List<Object> rContent = r.getContent();
for (Object o1 : rContent) {
if (o1 instanceof JAXBElement) {
JAXBElement jaxbElement = (JAXBElement) o1;
Object value = jaxbElement.getValue();
if (value instanceof Drawing) {
Drawing drawing = (Drawing) value;
List<Object> anchorOrInline = drawing.getAnchorOrInline();
for (Object aoi : anchorOrInline) {
if (aoi instanceof Inline) {
Inline inline = (Inline) aoi;
// Pic pic = inline.getGraphic().getGraphicData().getPic();
Graphic graphic = inline.getGraphic();
byte[] image = BinaryPartAbstractImage.getImage(wordMLPackage, graphic);
saveFile("D:/" +UUID.fastUUID() + ".png", image);
}
}
}
}
}
}

}
}
}

/**
* 将字节流转换成文件
*
* @param filename
* @param data
* @throws Exception
*/
public static void saveFile(String filePath, byte[] data) throws Exception {
if (data != null) {
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(data, 0, data.length);
fos.flush();
fos.close();
}
}