Can I ask you to run a small test for me please?
If you will, can you use Excel to create a new workbook. Insert an image -
anything you have to hand - onto the first sheet in the workbook - Sheet 1 -
before you save the workbook away. Next could you compile and then run this
code against that workbook;
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 ImageTest(String filename) {
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);
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);
}
}
}
}
// 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 " +
eschRec.getClass().getName());
System.out.println(buffer.toString().trim());
if(eschRec instanceof EscherClientAnchorRecord) {
ImageTest.procAnchorRec(((EscherClientAnchorRecord)eschRec), (level + 1));
}
}
}
}
// 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 = null;
String prefix = null;
for(int i = 0; i < level; i++) {
buffer.append(".....");
}
buffer = new StringBuilder(prefix);
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 == 1) {
new ImageTest(args[0]);
}
}
}
All you need to do is something like - ImageTest(new
String[]{"C:/test/Book1.xls"});
where the array of Strings passed to the main() method of ImageTest contains
the path to and name of the workbook you have created above.
If all works to plan, that should iterate through the records in the Escher
layer - this will only work for HSSF by the way currently - and it should
display to you the location of the image. Now, open the workbook again with
Excel and move the image, either change it's size or location on the sheet
or do both. Remember though that at the moment, the code will only look for
pictures inserted into the first sheet. After saving the workbook away
again, run the code a second time and you ought to see that the location of
the image has changed.
Is this the sort of information you were looking to establish - the actual
location of the image on the sheet from which we should be able to determine
it's size?
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Excel-image-sizes-tp5582506p5585711.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]