Return a list of all pictures for a workbook. This shows a discrepancy between the way pictures are stored in OLE2 files and the way they are stored in OOXML files. In the former case, pictures are associated with the workbook, in the latter they are referred to in "drawings" which are, in turn, referred to in sheets.
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@629170 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c278230bb6
commit
8191f45e44
@ -17,15 +17,15 @@
|
||||
|
||||
package org.apache.poi.hssf.usermodel;
|
||||
|
||||
import org.apache.poi.ddf.EscherBitmapBlip;
|
||||
import org.apache.poi.ddf.EscherBlipRecord;
|
||||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
|
||||
/**
|
||||
* Represents binary data stored in the file. Eg. A GIF, JPEG etc...
|
||||
*
|
||||
* @author Daniel Noll
|
||||
*/
|
||||
public class HSSFPictureData
|
||||
public class HSSFPictureData implements PictureData
|
||||
{
|
||||
// MSOBI constants for various formats.
|
||||
public static final short MSOBI_WMF = 0x2160;
|
||||
@ -52,20 +52,16 @@ public class HSSFPictureData
|
||||
this.blip = blip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the picture data.
|
||||
*
|
||||
* @return the picture data.
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.poi.hssf.usermodel.PictureData#getData()
|
||||
*/
|
||||
public byte[] getData()
|
||||
{
|
||||
return blip.getPicturedata();
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggests a file extension for this image.
|
||||
*
|
||||
* @return the file extension.
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.poi.hssf.usermodel.PictureData#suggestFileExtension()
|
||||
*/
|
||||
public String suggestFileExtension()
|
||||
{
|
||||
|
@ -0,0 +1,20 @@
|
||||
/* ====================================================================
|
||||
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.ss.usermodel;
|
||||
|
||||
public interface PictureData {}
|
@ -0,0 +1,36 @@
|
||||
/* ====================================================================
|
||||
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.ss.usermodel;
|
||||
|
||||
public interface PictureData {
|
||||
|
||||
/**
|
||||
* Gets the picture data.
|
||||
*
|
||||
* @return the picture data.
|
||||
*/
|
||||
byte[] getData();
|
||||
|
||||
/**
|
||||
* Suggests a file extension for this image.
|
||||
*
|
||||
* @return the file extension.
|
||||
*/
|
||||
String suggestFileExtension();
|
||||
|
||||
}
|
@ -65,7 +65,14 @@ public abstract class POIXMLDocument {
|
||||
return this.corePart;
|
||||
}
|
||||
|
||||
protected PackagePart getPart(PackageRelationship rel) throws InvalidFormatException {
|
||||
/**
|
||||
* Get the PackagePart that is the target of a relationship.
|
||||
*
|
||||
* @param rel The relationship
|
||||
* @return The target part
|
||||
* @throws InvalidFormatException
|
||||
*/
|
||||
protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException {
|
||||
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
|
||||
PackagePart part = getPackage().getPart(relName);
|
||||
if (part == null) {
|
||||
|
@ -0,0 +1,42 @@
|
||||
/* ====================================================================
|
||||
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.xssf.usermodel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
import org.openxml4j.opc.PackagePart;
|
||||
|
||||
|
||||
public class XSSFPictureData implements PictureData {
|
||||
|
||||
private PackagePart packagePart;
|
||||
|
||||
public XSSFPictureData(PackagePart packagePart) {
|
||||
this.packagePart = packagePart;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public String suggestFileExtension() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
|
||||
package org.apache.poi.xssf.usermodel;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -28,6 +29,7 @@ import org.apache.poi.ss.usermodel.Comment;
|
||||
import org.apache.poi.ss.usermodel.Footer;
|
||||
import org.apache.poi.ss.usermodel.Header;
|
||||
import org.apache.poi.ss.usermodel.Patriarch;
|
||||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
import org.apache.poi.ss.usermodel.PrintSetup;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
|
@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.DataFormat;
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Name;
|
||||
import org.apache.poi.ss.usermodel.Palette;
|
||||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
import org.apache.poi.ss.usermodel.SharedStringSource;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
@ -66,6 +67,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
|
||||
private static final String SHARED_STRINGS_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
|
||||
|
||||
private static final String DRAWING_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
|
||||
|
||||
private static final String IMAGE_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
|
||||
private CTWorkbook workbook;
|
||||
|
||||
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||
@ -92,17 +97,15 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
Iterator<PackageRelationship> it = prc.iterator();
|
||||
if (it.hasNext()) {
|
||||
PackageRelationship rel = it.next();
|
||||
PackagePart part = getPart(rel);
|
||||
PackagePart part = getTargetPart(rel);
|
||||
this.sharedStringSource = new SharedStringsTable(part);
|
||||
}
|
||||
// Load individual sheets
|
||||
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
|
||||
PackageRelationship rel = this.getCorePart().getRelationship(ctSheet.getId());
|
||||
if (rel == null) {
|
||||
log.log(log.WARN, "No relationship found for sheet " + ctSheet.getId());
|
||||
PackagePart part = getPackagePart(ctSheet);
|
||||
if (part == null) {
|
||||
continue;
|
||||
}
|
||||
PackagePart part = getPart(rel);
|
||||
WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
|
||||
XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this);
|
||||
this.sheets.add(sheet);
|
||||
@ -118,6 +121,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
return this.workbook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PackagePart corresponding to a given sheet.
|
||||
*
|
||||
* @param ctSheet The sheet
|
||||
* @return A PackagePart, or null if no matching part found.
|
||||
* @throws InvalidFormatException
|
||||
*/
|
||||
private PackagePart getPackagePart(CTSheet ctSheet) throws InvalidFormatException {
|
||||
PackageRelationship rel = this.getCorePart().getRelationship(ctSheet.getId());
|
||||
if (rel == null) {
|
||||
log.log(POILogger.WARN, "No relationship found for sheet " + ctSheet.getId());
|
||||
return null;
|
||||
}
|
||||
return getTargetPart(rel);
|
||||
}
|
||||
|
||||
public int addPicture(byte[] pictureData, int format) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
@ -209,9 +228,30 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List getAllPictures() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public List<PictureData> getAllPictures() {
|
||||
// In OOXML pictures are referred to in sheets
|
||||
List<PictureData> pictures = new LinkedList<PictureData>();
|
||||
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
|
||||
try {
|
||||
PackagePart sheetPart = getPackagePart(ctSheet);
|
||||
if (sheetPart == null) {
|
||||
continue;
|
||||
}
|
||||
PackageRelationshipCollection prc = sheetPart.getRelationshipsByType(DRAWING_RELATIONSHIP);
|
||||
for (PackageRelationship rel : prc) {
|
||||
PackagePart drawingPart = getTargetPart(rel);
|
||||
PackageRelationshipCollection prc2 = drawingPart.getRelationshipsByType(IMAGE_RELATIONSHIP);
|
||||
for (PackageRelationship rel2 : prc2) {
|
||||
PackagePart imagePart = getTargetPart(rel2);
|
||||
XSSFPictureData pd = new XSSFPictureData(imagePart);
|
||||
pictures.add(pd);
|
||||
}
|
||||
}
|
||||
} catch (InvalidFormatException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return pictures;
|
||||
}
|
||||
|
||||
public boolean getBackupFlag() {
|
||||
|
BIN
src/ooxml/testcases/org/apache/poi/xssf/data/picture.xlsx
Normal file
BIN
src/ooxml/testcases/org/apache/poi/xssf/data/picture.xlsx
Normal file
Binary file not shown.
@ -18,11 +18,13 @@
|
||||
package org.apache.poi.xssf.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.PictureData;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
@ -63,4 +65,10 @@ public class TestLoadSaveXSSF extends TestCase {
|
||||
// assertNotNull(style);
|
||||
}
|
||||
|
||||
public void testLoadPictures() throws Exception {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(new File(filename, "picture.xlsx").getAbsolutePath());
|
||||
List<PictureData> pictures = workbook.getAllPictures();
|
||||
assertEquals(1, pictures.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user