Search in sources :

Example 6 with PicturesTable

use of org.apache.poi.hwpf.model.PicturesTable in project poi by apache.

the class TestPictures method testFloatingPictures.

/**
     * In word you can have floating or fixed pictures.
     * Fixed have a  in place with an offset to the
     *  picture data.
     * Floating have a  in place, which references a
     *   which has the offset. More than one can
     *  reference the same 
     */
@Test
public 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("")) {
                plain8s++;
            }
        }
    }
    // Total is 20, as the 4 escher 8s all reference
    //  the same regular image
    assertEquals(16, image1s);
    assertEquals(4, escher8s);
    assertEquals(0, plain8s);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) Test(org.junit.Test)

Example 7 with PicturesTable

use of org.apache.poi.hwpf.model.PicturesTable in project poi by apache.

the class TestPictures method testEquation.

@Test
public 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());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) Test(org.junit.Test)

Example 8 with PicturesTable

use of org.apache.poi.hwpf.model.PicturesTable in project poi by apache.

the class TestPictures method testEmbededDocumentIcon.

/**
     * 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
public 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());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    p = r.getParagraph(1);
    assertEquals(5, p.numCharacterRuns());
    assertEquals(" EMBED Excel.Sheet.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    p = r.getParagraph(2);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED Excel.Sheet.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(5)));
    p = r.getParagraph(3);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED PowerPoint.Show.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(5)));
    p = r.getParagraph(4);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED Word.Document.8 \\s \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, 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());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) Test(org.junit.Test)

Example 9 with PicturesTable

use of org.apache.poi.hwpf.model.PicturesTable in project poi by apache.

the class TestHWPFPictures method testImageCount.

/**
	 * Test that we have the right numbers of images in each file
	 */
public void testImageCount() {
    HWPFDocument docA = HWPFTestDataSamples.openSampleFile(docAFile);
    HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile);
    assertNotNull(docA.getPicturesTable());
    assertNotNull(docB.getPicturesTable());
    PicturesTable picA = docA.getPicturesTable();
    PicturesTable picB = docB.getPicturesTable();
    List<Picture> picturesA = picA.getAllPictures();
    List<Picture> picturesB = picB.getAllPictures();
    assertEquals(7, picturesA.size());
    assertEquals(2, picturesB.size());
}
Also used : Picture(org.apache.poi.hwpf.usermodel.Picture) PicturesTable(org.apache.poi.hwpf.model.PicturesTable)

Example 10 with PicturesTable

use of org.apache.poi.hwpf.model.PicturesTable in project poi by apache.

the class TestHWPFPictures method testMacImages.

public void testMacImages() throws Exception {
    HWPFDocument docC = HWPFTestDataSamples.openSampleFile("53446.doc");
    PicturesTable picturesTable = docC.getPicturesTable();
    List<Picture> pictures = picturesTable.getAllPictures();
    assertEquals(4, pictures.size());
    int[][] expectedSizes = { // PNG
    { 185, 42 }, // PNG
    { 260, 114 }, // PNG
    { 185, 42 }, // PNG
    { 260, 114 } };
    for (int i = 0; i < pictures.size(); i++) {
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures.get(i).getContent()));
        assertNotNull(image);
        int[] dimensions = expectedSizes[i];
        assertEquals(dimensions[0], image.getWidth());
        assertEquals(dimensions[1], image.getHeight());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Picture(org.apache.poi.hwpf.usermodel.Picture) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) BufferedImage(java.awt.image.BufferedImage)

Aggregations

PicturesTable (org.apache.poi.hwpf.model.PicturesTable)12 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)6 Picture (org.apache.poi.hwpf.usermodel.Picture)6 Test (org.junit.Test)5 Paragraph (org.apache.poi.hwpf.usermodel.Paragraph)2 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 OldWordFileFormatException (org.apache.poi.hwpf.OldWordFileFormatException)1 SavedByEntry (org.apache.poi.hwpf.model.SavedByEntry)1 SavedByTable (org.apache.poi.hwpf.model.SavedByTable)1 StyleDescription (org.apache.poi.hwpf.model.StyleDescription)1 CharacterRun (org.apache.poi.hwpf.usermodel.CharacterRun)1 Field (org.apache.poi.hwpf.usermodel.Field)1 HeaderStories (org.apache.poi.hwpf.usermodel.HeaderStories)1 Range (org.apache.poi.hwpf.usermodel.Range)1 Table (org.apache.poi.hwpf.usermodel.Table)1 TableCell (org.apache.poi.hwpf.usermodel.TableCell)1 TableRow (org.apache.poi.hwpf.usermodel.TableRow)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1