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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hwpf.usermodel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.hwpf.model.PicturesTable;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Test the picture handling
*
* @author Nick Burch
*/
public final class TestPictures {
/**
* two jpegs
*/
@Test
void testTwoImages() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("two_images.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics);
assertEquals(pics.size(), 2);
for(int i=0; i<pics.size(); i++) {
Picture pic = pics.get(i);
assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName());
}
Picture picA = pics.get(0);
Picture picB = pics.get(1);
assertEquals("jpg", picA.suggestFileExtension());
assertEquals("png", picB.suggestFileExtension());
}
/**
* pngs and jpegs
*/
@Test
void testDifferentImages() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testPictures.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics);
assertEquals(7, pics.size());
for(Picture pic : pics) {
assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName());
}
assertEquals("jpg", pics.get(0).suggestFileExtension());
assertEquals("image/jpeg", pics.get(0).getMimeType());
assertEquals("jpg", pics.get(1).suggestFileExtension());
assertEquals("image/jpeg", pics.get(1).getMimeType());
assertEquals("png", pics.get(3).suggestFileExtension());
assertEquals("image/png", pics.get(3).getMimeType());
assertEquals("png", pics.get(4).suggestFileExtension());
assertEquals("image/png", pics.get(4).getMimeType());
assertEquals("wmf", pics.get(5).suggestFileExtension());
assertEquals("image/x-wmf", pics.get(5).getMimeType());
assertEquals("jpg", pics.get(6).suggestFileExtension());
assertEquals("image/jpeg", pics.get(6).getMimeType());
}
/**
* emf image, nice and simple
*/
@Test
void testEmfImage() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("vector_image.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics);
assertEquals(1, pics.size());
Picture pic = pics.get(0);
assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName());
assertTrue(pic.getSize() > 128);
// Check right contents
byte[] emf = POIDataSamples.getDocumentInstance().readFile("vector_image.emf");
byte[] pemf = pic.getContent();
assertEquals(emf.length, pemf.length);
for(int i=0; i<emf.length; i++) {
assertEquals(emf[i], pemf[i]);
}
}
@Test
void testPicturesWithTable() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug44603.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertEquals(2, pics.size());
}
@Test
void testPicturesInHeader() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("header_image.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertEquals(2, pics.size());
}
@Test
void testFastSaved() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("rasp.doc");
doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception
}
@Test
void testFastSaved2() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("o_kurs.doc");
doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception
}
@Test
void testFastSaved3() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("ob_is.doc");
doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception
}
/**
* When you embed another office document into Word, it stores
* a rendered "icon" picture of what that document looks like.
* This image is re-created when you edit the embeded document,
* then used as-is to speed things up.
* Check that we can properly read one of these
*/
@Test
void testEmbededDocumentIcon() {
// This file has two embeded excel files, an embeded powerpoint
// file and an embeded word file, in that order
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("word_with_embeded.doc");
// Check we don't break loading the pictures
doc.getPicturesTable().getAllPictures();
PicturesTable pictureTable = doc.getPicturesTable();
// Check the text, and its embeded images
Paragraph p;
Range r = doc.getRange();
assertEquals(1, r.numSections());
assertEquals(5, r.numParagraphs());
p = r.getParagraph(0);
assertEquals(2, p.numCharacterRuns());
assertEquals("I have lots of embedded files in me\r", p.text());
assertFalse(pictureTable.hasPicture(p.getCharacterRun(0)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(1)));
p = r.getParagraph(1);
assertEquals(5, p.numCharacterRuns());
assertEquals("\u0013 EMBED Excel.Sheet.8 \u0014\u0001\u0015\r", p.text());
assertFalse(pictureTable.hasPicture(p.getCharacterRun(0)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(1)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(2)));
assertTrue(pictureTable.hasPicture(p.getCharacterRun(3)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(4)));
p = r.getParagraph(2);
assertEquals(6, p.numCharacterRuns());
assertEquals("\u0013 EMBED Excel.Sheet.8 \u0014\u0001\u0015\r", p.text());
assertFalse(pictureTable.hasPicture(p.getCharacterRun(0)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(1)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(2)));
assertTrue(pictureTable.hasPicture(p.getCharacterRun(3)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(4)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(5)));
p = r.getParagraph(3);
assertEquals(6, p.numCharacterRuns());
assertEquals("\u0013 EMBED PowerPoint.Show.8 \u0014\u0001\u0015\r", p.text());
assertFalse(pictureTable.hasPicture(p.getCharacterRun(0)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(1)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(2)));
assertTrue(pictureTable.hasPicture(p.getCharacterRun(3)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(4)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(5)));
p = r.getParagraph(4);
assertEquals(6, p.numCharacterRuns());
assertEquals("\u0013 EMBED Word.Document.8 \\s \u0014\u0001\u0015\r", p.text());
assertFalse(pictureTable.hasPicture(p.getCharacterRun(0)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(1)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(2)));
assertTrue(pictureTable.hasPicture(p.getCharacterRun(3)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(4)));
assertFalse(pictureTable.hasPicture(p.getCharacterRun(5)));
// Look at the pictures table
List<Picture> pictures = pictureTable.getAllPictures();
assertEquals(4, pictures.size());
Picture picture = pictures.get( 0 );
assertEquals( "emf", picture.suggestFileExtension() );
assertEquals( "0.emf", picture.suggestFullFileName() );
assertEquals( "image/x-emf", picture.getMimeType() );
picture = pictures.get( 1 );
assertEquals( "emf", picture.suggestFileExtension() );
assertEquals( "469.emf", picture.suggestFullFileName() );
assertEquals( "image/x-emf", picture.getMimeType() );
picture = pictures.get( 2 );
assertEquals( "emf", picture.suggestFileExtension() );
assertEquals( "8c7.emf", picture.suggestFullFileName() );
assertEquals( "image/x-emf", picture.getMimeType() );
picture = pictures.get( 3 );
assertEquals( "emf", picture.suggestFileExtension() );
assertEquals( "10a8.emf", picture.suggestFullFileName() );
assertEquals( "image/x-emf", picture.getMimeType() );
}
@Test
void testEquation()
{
HWPFDocument doc = HWPFTestDataSamples.openSampleFile( "equation.doc" );
PicturesTable pictures = doc.getPicturesTable();
final List<Picture> allPictures = pictures.getAllPictures();
assertEquals( 1, allPictures.size() );
Picture picture = allPictures.get( 0 );
assertNotNull( picture );
assertEquals( PictureType.EMF, picture.suggestPictureType() );
assertEquals( PictureType.EMF.getExtension(),
picture.suggestFileExtension() );
assertEquals( PictureType.EMF.getMime(), picture.getMimeType() );
assertEquals( "0.emf", picture.suggestFullFileName() );
}
/**
* In word you can have floating or fixed pictures.
* Fixed have a \u0001 in place with an offset to the
* picture data.
* Floating have a \u0008 in place, which references a
* \u0001 which has the offset. More than one can
* reference the same \u0001
*/
@Test
void testFloatingPictures() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("FloatingPictures.doc");
PicturesTable pictures = doc.getPicturesTable();
// There are 19 images in the picture, but some are
// duplicate floating ones
assertEquals(17, pictures.getAllPictures().size());
int plain8s = 0;
int escher8s = 0;
int image1s = 0;
Range r = doc.getRange();
for(int np=0; np < r.numParagraphs(); np++) {
Paragraph p = r.getParagraph(np);
for(int nc=0; nc < p.numCharacterRuns(); nc++) {
CharacterRun cr = p.getCharacterRun(nc);
if(pictures.hasPicture(cr)) {
image1s++;
} else if(pictures.hasEscherPicture(cr)) {
escher8s++;
} else if(cr.text().startsWith("\u0008")) {
plain8s++;
}
}
}
// Total is 20, as the 4 escher 8s all reference
// the same regular image
assertEquals(16, image1s);
assertEquals(4, escher8s);
assertEquals(0, plain8s);
}
@SuppressWarnings( "deprecation" )
@Test
void testCroppedPictures() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testCroppedPictures.doc");
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics);
assertEquals(2, pics.size());
Picture pic1 = pics.get(0);
assertEquals( -1, pic1.getWidth(), "FIXME: unable to get image width" );
assertEquals(270, pic1.getHorizontalScalingFactor());
assertEquals(271, pic1.getVerticalScalingFactor());
assertEquals(12000, pic1.getDxaGoal()); // 21.17 cm / 2.54 cm/inch * 72dpi * 20 = 12000
assertEquals(9000, pic1.getDyaGoal()); // 15.88 cm / 2.54 cm/inch * 72dpi * 20 = 9000
assertEquals(0, pic1.getDxaCropLeft());
assertEquals(0, pic1.getDxaCropRight());
assertEquals(0, pic1.getDyaCropTop());
assertEquals(0, pic1.getDyaCropBottom());
Picture pic2 = pics.get(1);
assertEquals( -1, pic2.getWidth(), "FIXME: unable to get image width" );
assertEquals(764, pic2.getHorizontalScalingFactor());
assertEquals(685, pic2.getVerticalScalingFactor());
assertEquals(12000, pic2.getDxaGoal()); // 21.17 cm / 2.54 cm/inch * 72dpi * 20 = 12000
assertEquals(9000, pic2.getDyaGoal()); // 15.88 cm / 2.54 cm/inch * 72dpi * 20 = 9000
assertEquals(0, pic2.getDxaCropLeft()); // TODO YK: The Picture is cropped but HWPF reads the crop parameters all zeros
assertEquals(0, pic2.getDxaCropRight());
assertEquals(0, pic2.getDyaCropTop());
assertEquals(0, pic2.getDyaCropBottom());
}
@Test
void testPictureDetectionWithPNG() {
HWPFDocument document = HWPFTestDataSamples.openSampleFile("PngPicture.doc");
PicturesTable pictureTable = document.getPicturesTable();
assertEquals(1, pictureTable.getAllPictures().size());
Picture p = pictureTable.getAllPictures().get(0);
assertEquals(PictureType.PNG, p.suggestPictureType());
assertEquals("png", p.suggestFileExtension());
}
@Test
void testPictureWithAlternativeText() {
HWPFDocument document = HWPFTestDataSamples.openSampleFile("Picture_Alternative_Text.doc");
PicturesTable pictureTable = document.getPicturesTable();
Picture picture = pictureTable.getAllPictures().get(0);
assertEquals("This is the alternative text for the picture.", picture.getDescription());
}
@Disabled("This bug is not fixed yet")
@Test
void test58804_1() throws Exception {
HWPFDocument docA = HWPFTestDataSamples.openSampleFile("58804_1.doc");
expectImages(docA, 1);
HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
try (OutputStream out = new FileOutputStream("/tmp/58804_1_out.doc")) {
docB.write(out);
}
expectImages(docB, 1);
}
@Disabled("This bug is not fixed yet")
@Test
void test58804() throws Exception {
HWPFDocument docA = HWPFTestDataSamples.openSampleFile("58804.doc");
expectImages(docA, 7);
HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
expectImages(docB, 7);
}
private void expectImages(HWPFDocument docA, int expectedCount) {
assertNotNull(docA.getPicturesTable());
PicturesTable picA = docA.getPicturesTable();
List<Picture> picturesA = picA.getAllPictures();
assertEquals(expectedCount, picturesA.size());
}
}
|