I have added to 'my' code so that it looks like this now;

import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.record.*;
import org.apache.poi.ddf.*;

/**
 *
 * @author Mark Beardsley
 */
public class ImageTest {
    
    public static final int ESCHER_RECORDS = 0;
    public static final int HSSF_PATRIARCH = 1;
    
    public ImageTest(String filename, int procType) {
        File file = null;
        FileInputStream fis = null;
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        HSSFPicture picture = null;
        List<HSSFPictureData> picList = null;
        Iterator<HSSFPictureData> picIter = null;
        HSSFPictureData picData = null;
        EscherAggregate eschAgg = null;
        List<EscherRecord> eschList = null;
        Iterator<EscherRecord> eschIter = null;
        EscherRecord eschRec = null;
        try {
            
            // Open a workbook, get the first sheet, the EscherAggregate for
            // that sheet and an Iterator so that we can step through any 
            // records the aggregate contains.
            file = new File(filename);
            fis = new FileInputStream(file);
            workbook = new HSSFWorkbook(fis);
            sheet = workbook.getSheetAt(0);
            
            // Try processing the Drawing Patriarch first
            if(procType == ImageTest.HSSF_PATRIARCH) {
                ImageTest.procPatriarch(sheet.getDrawingPatriarch());
            }
            else {
                // Now iterate the Escher Records
                eschAgg = sheet.getDrawingEscherAggregate();
                eschList = eschAgg.getEscherRecords();
                eschIter = eschList.iterator();
                while(eschIter.hasNext()) {
                    eschRec = eschIter.next();
                    // Display the type of record found in the container. If
this
                    // record is itself a container then iterate through the
                    // records it holds.
                    System.out.println("Found a " +
eschRec.getClass().getName());
                    if(eschRec.isContainerRecord()) {
                        ImageTest.iterateContainer(eschRec, 1);
                    }
                }
            }
        }
        catch(IOException ioEx) {
            ImageTest.printException(ioEx);
        }
        finally {
            if(fis != null) {
                try {
                    fis.close();
                    fis = null;
                }
                catch(IOException ioEx) {
                    ImageTest.printException(ioEx);
                }
            }
        }
    }
    
    private static void procPatriarch(HSSFPatriarch patriarch) {
        List<HSSFShape> shapeList = null;
        Iterator<HSSFShape> shapeIter = null;
        HSSFShape shape = null;
        shapeList = patriarch.getChildren();
        shapeIter = shapeList.iterator();
        HSSFPicture picture = null;
        HSSFClientAnchor anchor = null;
        while(shapeIter.hasNext()) {
            shape = shapeIter.next();
            if(shape instanceof HSSFPicture) {
                picture = (HSSFPicture)shape;
                anchor = picture.getPreferredSize();
                ImageTest.procClientAnchor(anchor);
            }
        }
    }
    
    private static void procClientAnchor(HSSFClientAnchor anchor) {
        StringBuilder buffer = new StringBuilder("Working from the
HSSFClientAnchor now! ");
        buffer.append(" The image spans from cell ");
        buffer.append(anchor.getCol1());
        buffer.append(", ");
        buffer.append(anchor.getRow1());
        buffer.append(" X offset ");
        buffer.append(anchor.getDx1());
        buffer.append(" and Y offset ");
        buffer.append(anchor.getDy1());
        buffer.append(" to cell ");
        buffer.append(anchor.getCol2());
        buffer.append(", ");
        buffer.append(anchor.getRow2());
        buffer.append(" X offset ");
        buffer.append(anchor.getDx2());
        buffer.append(" and Y offset ");
        buffer.append(anchor.getDy2());
        System.out.println(buffer.toString().trim());
    }
    
    // Step through the records within the container.
    private static void iterateContainer(EscherRecord eschRec, int level) {
        StringBuilder buffer = null;
        List<EscherRecord> eschList = null;
        Iterator<EscherRecord> eschIter = null;
        eschList = eschRec.getChildRecords();
        eschIter = eschList.iterator();
        while(eschIter.hasNext()) {
            eschRec = eschIter.next();
            if(eschRec.isContainerRecord()) {
                ImageTest.iterateContainer(eschRec, level + 1);
            }
            else {
                buffer = new StringBuilder();
                for(int i = 0; i < level; i++) {
                    buffer.append(".....");
                }
                buffer.append(" Found a/an ");
                buffer.append(eschRec.getClass().getName());
                System.out.println(buffer.toString().trim());
                if(eschRec instanceof EscherClientAnchorRecord) {
                   
ImageTest.procAnchorRec(((EscherClientAnchorRecord)eschRec), (level + 1));
                }
                else if(eschRec instanceof EscherOptRecord) {
                    // Need to look into what this record actually does.
                }
            }
        }
    }
    
    // Display the location of the image; the co-ordinates of the cells at
    // it's top right and bottom left hand corners along with the X and Y
    // offsets within each.
    private static void procAnchorRec(EscherClientAnchorRecord anchorRec,
int level) {
        StringBuilder buffer = new StringBuilder();
        String prefix = null;
        for(int i = 0; i < level; i++) {
            buffer.append(".....");
        }
        prefix = buffer.toString().trim();
        buffer = new StringBuilder(prefix);
        buffer.append(" The image spans from cell ");
        buffer.append(anchorRec.getCol1());
        buffer.append(", ");
        buffer.append(anchorRec.getRow1());
        buffer.append(" X offset ");
        buffer.append(anchorRec.getDx1());
        buffer.append(" and Y offset ");
        buffer.append(anchorRec.getDy1());
        buffer.append(" to cell ");
        buffer.append(anchorRec.getCol2());
        buffer.append(", ");
        buffer.append(anchorRec.getRow2());
        buffer.append(" X offset ");
        buffer.append(anchorRec.getDx2());
        buffer.append(" and Y offset ");
        buffer.append(anchorRec.getDy2());
        System.out.println(buffer.toString().trim());
    }
    
    private static void printException(Throwable th) {
        System.out.println("Caught an: " + th.getClass().getName());
        System.out.println("Message: " + th.getMessage());
        System.out.println("Stacktrace follows:.....");
        th.printStackTrace(System.out);
    }
    
    public static void main(String[] args) {
        if(args.length == 2) {
            new ImageTest(args[0], Integer.parseInt(args[1]));
        }
    }
}

and if you call it something like this;

ImageTest.main(new String[]{"C:/temp/Image Test.xls", "0"});
ImageTest.main(new String[]{"C:/temp/Image Test.xls", "1"});

with one call made straight after the other, it will process the workbook
and report the locations of the images using both the Escher Records and
drawing patriarch; and there are differences. As you observed, when using
the drawing patriarch, the original size of the image is reported unless you
drag the top left hand corner to increase or reduce the images size. In that
case it does seem to be recognised and reported; something which is not done
if you drag the bottom right hand corner of the image (I have not tested
other scenarios with the resizing handles). 

Can you manage with the Escher Records version of the code? The reason I ask
this is that I have not yet found a way to tie the Escher Records back to a
specific image. If you need to relate image to size then it would be far
better for you to work from the patriarch and I do feel you may have to log
an error through bugzilla here.

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/Excel-image-sizes-tp5582506p5586980.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to