OK Matthew, I have no idea whether or not you still need to do this operation
but I have been able to figure out some more information and to get part of
the way to a solution - I say part as currently I am getting multiple copies
of the information and need to identify how to prevent this from happening.
To get at the size and location of the images, it is necessary to mess
around with objects in the Escher Layer; to be precise, we need to get at
the EscherOptRecord and the EscherClientAnchorRecord instances associated
with each picture. The way to get at these is to grab the EscherAggregate
object from the sheet, get a reference to the associated
EscherContainerRecord and to then iterate through the lower level children
of this container. I have put together some code that does this and included
it below.
As you will be able to see, there are two classes, the second of which is
just a convenient way to aggregate the two class instances together. Just
look into the main() method of the ExtractPictures class to see how to run
the code against a workbook. More work still needs to be done unfortunately,
but I hope this class points the way forward.
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import java.util.HashMap;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.record.EscherAggregate;
import org.apache.poi.ddf.EscherContainerRecord;
/**
*
* @author MB
*/
public class ExtractPictures {
private HashMap<String, ShapeInformation> shapes = new HashMap<String,
ShapeInformation>();
/**
* @param args the command line arguments
*/
public void findPictures(String inputFilename) {
File inputFile = null;
FileInputStream fileIStream = null;
BufferedInputStream bufIStream = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
EscherAggregate escherAggregate = null;
EscherContainerRecord escherContainer = null;
List childContainers = null;
List childRecords = null;
Iterator listIterator = null;
try{
inputFile = new File(inputFilename);
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
workbook = new HSSFWorkbook(bufIStream);
sheet = workbook.getSheetAt(0);
escherAggregate = sheet.getDrawingEscherAggregate();
if(escherAggregate != null) {
escherContainer = escherAggregate.getEscherContainer();
this.iterateContainer(escherContainer, 1);
}
java.util.Set<String> keys = shapes.keySet();
Iterator<String> keyIterator = keys.iterator();
while(keyIterator.hasNext()) {
String key = keyIterator.next();
ShapeInformation shape = shapes.get(key);
System.out.println(shape);
}
}
catch(Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows:...............");
ex.printStackTrace(System.out);
}
finally {
if(bufIStream != null) {
try {
bufIStream.close();
}
catch(Exception ex) {
// I G NO R E //
}
}
}
}
private void iterateContainer(EscherContainerRecord escherContainer, int
level) {
List childContainers = escherContainer.getChildContainers();
List childRecords = escherContainer.getChildRecords();
Iterator listIterator = childContainers.iterator();
org.apache.poi.ddf.EscherSpgrRecord sprgRecord = null;
org.apache.poi.ddf.EscherSpRecord spRecord = null;
org.apache.poi.ddf.EscherOptRecord optRecord = null;
org.apache.poi.ddf.EscherClientAnchorRecord anchrRecord = null;
org.apache.poi.ddf.EscherClientDataRecord dataRecord = null;
org.apache.poi.ddf.EscherDgRecord dgRecord = null;
Object next = null;
while(listIterator.hasNext()) {
next = listIterator.next();
if(next instanceof org.apache.poi.ddf.EscherContainerRecord) {
this.iterateContainer((org.apache.poi.ddf.EscherContainerRecord)next,
++level);
}
else {
System.out.println("Found an instance of " +
next.getClass().getName() + " in the list of child containers.");
}
}
listIterator = childRecords.iterator();
while(listIterator.hasNext()) {
next = listIterator.next();
if(next instanceof org.apache.poi.ddf.EscherContainerRecord) {
this.iterateContainer((org.apache.poi.ddf.EscherContainerRecord)next,
++level);
}
else {
if(next instanceof org.apache.poi.ddf.EscherSpgrRecord) {
sprgRecord = (org.apache.poi.ddf.EscherSpgrRecord)next;
}
else if(next instanceof org.apache.poi.ddf.EscherSpRecord) {
spRecord = (org.apache.poi.ddf.EscherSpRecord)next;
}
else if(next instanceof org.apache.poi.ddf.EscherOptRecord)
{
optRecord = (org.apache.poi.ddf.EscherOptRecord)next;
String key = String.valueOf(level);
if(this.shapes.containsKey(key)) {
this.shapes.get(key).setOptRecord(optRecord);
}
else {
ShapeInformation shape = new
ShapeInformation(level);
shape.setOptRecord(optRecord);
this.shapes.put(key, shape);
}
}
else if(next instanceof
org.apache.poi.ddf.EscherClientAnchorRecord) {
anchrRecord =
(org.apache.poi.ddf.EscherClientAnchorRecord)next;
String key = String.valueOf(level);
if(this.shapes.containsKey(key)) {
this.shapes.get(key).setAnchorRecord(anchrRecord);
}
else {
ShapeInformation shape = new
ShapeInformation(level);
shape.setAnchorRecord(anchrRecord);
this.shapes.put(key, shape);
}
}
else if(next instanceof
org.apache.poi.ddf.EscherClientDataRecord) {
dataRecord =
(org.apache.poi.ddf.EscherClientDataRecord)next;
}
else if(next instanceof org.apache.poi.ddf.EscherDgRecord) {
dgRecord = (org.apache.poi.ddf.EscherDgRecord)next;
}
}
}
}
public static final void main(String[] args) {
new ExtractPictures().findPictures("C:\\temp\\picture.xls");
}
}
import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.ddf.EscherClientAnchorRecord;
/**
*
* @author MB
*/
public class ShapeInformation {
private EscherOptRecord optRecord = null;
private EscherClientAnchorRecord anchrRecord = null;
private int level = 0;
public ShapeInformation(int level) {
this.level = level;
}
public void setOptRecord(EscherOptRecord optRecord) {
this.optRecord = optRecord;
}
public void setAnchorRecord(EscherClientAnchorRecord anchrRecord) {
this.anchrRecord = anchrRecord;
}
public EscherOptRecord getOptRecord() {
return(this.optRecord);
}
public EscherClientAnchorRecord getAnchorRecord() {
return(this.anchrRecord);
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("**** Shape Information for level: " + this.level + "
****");
buffer.append("\n");
buffer.append("[org.apache.poi.ddf.EscherOptRecord]");
buffer.append("\n");
buffer.append(this.optRecord.toString());
buffer.append("\n");
buffer.append("[org.apache.poi.ddf.EscherClientAnchorRecord]");
buffer.append("\n");
buffer.append(this.anchrRecord.toString());
buffer.append("\n");
return(buffer.toString());
}
}
matthewxb wrote:
>
> I have a workbook contains two pictures. I want to copy the pictures from
> a source workbook to a target workbook. How can I do that?
>
> I used HSSFWorkbook.getAllPictures() to retrieve all the pictures, but it
> only allow me to read the pictures' binary data. I have to know the size
> and the location of each picture.
> I write a loop to read each worksheet and call
> HSSFSheet.getDrawingPatriarch(). It contains four methods in
> HSSFPatriarch, suppose they represent the location of picture.
>
> HSSFPatriarch.getX1(),HSSFPatriarch.getX2(), HSSFPatriarch.getY1(),
> HSSFPatriarch.getY2()
>
> But above methods return 0, which is incorrect.
>
> And the size of picture I couldn't find a way to know, since the actual
> picture size and the size shown in worksheet are different.
>
> I want to copy the pictures from a source workbook to a target workbook.
> How can I do that? Please advice, thank you very much!
>
--
View this message in context:
http://www.nabble.com/Copy-images-from-Workbook-to-Workbook-tp23032425p23124134.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]