if ("Worksheet".equals(name)) {
//read xls
+ @SuppressWarnings({ "unused", "resource" })
HSSFWorkbook wb = new HSSFWorkbook(data.getData());
} else if ("Document".equals(name)) {
picIdx++;
HSLFPictureShape p = (HSLFPictureShape) shape;
HSLFPictureData data = p.getPictureData();
- String name = p.getPictureName();
- int type = data.getType();
- String ext;
- switch (type) {
- case HSLFPictureShape.JPEG:
- ext = ".jpg";
- break;
- case HSLFPictureShape.PNG:
- ext = ".png";
- break;
- case HSLFPictureShape.WMF:
- ext = ".wmf";
- break;
- case HSLFPictureShape.EMF:
- ext = ".emf";
- break;
- case HSLFPictureShape.PICT:
- ext = ".pict";
- break;
- case HSLFPictureShape.DIB:
- ext = ".dib";
- break;
- default:
- continue;
- }
+ String ext = data.getType().extension;
FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
out.write(data.getData());
out.close();
import java.io.FileInputStream;\r
import java.io.FileOutputStream;\r
import java.io.IOException;\r
+import org.apache.poi.sl.usermodel.PictureData.PictureType;\r
\r
/**\r
* Images\r
XSLFSlide slide = ppt.createSlide();\r
File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");\r
byte[] data = IOUtils.toByteArray(new FileInputStream(img));\r
- int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);\r
+ XSLFPictureData pictureIndex = ppt.addPicture(data, PictureType.PNG);\r
\r
/*XSLFPictureShape shape =*/ slide.createPicture(pictureIndex);\r
\r
import java.io.IOException;
public interface PictureData {
- public String getContentType();
+
+ enum PictureType {
+ /** Extended windows meta file */
+ EMF(2,2,"image/x-emf",".emf"),
+ /** Windows Meta File */
+ WMF(3,3,"image/x-wmf",".wmf"),
+ /** Mac PICT format */
+ PICT(4,4,"image/pict",".pict"), // or image/x-pict (for HSLF) ???
+ /** JPEG format */
+ JPEG(5,5,"image/jpeg",".jpg"),
+ /** PNG format */
+ PNG(6,6,"image/png",".png"),
+ /** Device independent bitmap */
+ DIB(7,7,"image/dib",".dib"),
+ /** GIF image format */
+ GIF(-1,8,"image/gif",".gif"),
+ /** Tag Image File (.tiff) */
+ TIFF(-1,9,"image/tiff",".tif"),
+ /** Encapsulated Postscript (.eps) */
+ EPS(-1,10,"image/x-eps",".eps"),
+ /** Windows Bitmap (.bmp) */
+ BMP(-1,11,"image/x-ms-bmp",".bmp"),
+ /** WordPerfect graphics (.wpg) */
+ WPG(-1,12,"image/x-wpg",".wpg"),
+ /** Microsoft Windows Media Photo image (.wdp) */
+ WDP(-1,13,"image/vnd.ms-photo",".wdp");
+
+ public final int nativeId, ooxmlId;
+ public final String contentType,extension;
- public byte[] getData();
- public void setData(byte[] data) throws IOException;
+ PictureType(int nativeId, int ooxmlId,String contentType,String extension) {
+ this.nativeId = nativeId;
+ this.ooxmlId = ooxmlId;
+ this.contentType = contentType;
+ this.extension = extension;
+ }
+
+ public static PictureType forNativeID(int nativeId) {
+ for (PictureType ans : values()) {
+ if (ans.nativeId == nativeId) return ans;
+ }
+ return null;
+ }
+
+ public static PictureType forOoxmlID(int ooxmlId) {
+ for (PictureType ans : values()) {
+ if (ans.ooxmlId == ooxmlId) return ans;
+ }
+ return null;
+ }
+ }
+
+
+ /**
+ * Returns content type (mime type) of this picture.
+ *
+ * @return content type of this picture.
+ */
+ String getContentType();
+
+ /**
+ * @return the picture type
+ */
+ PictureType getType();
+
+ /**
+ * Returns the binary data of this Picture
+ * @return picture data
+ */
+ byte[] getData();
+
+ /**
+ * Sets the binary picture data
+ * @param data picture data
+ */
+ void setData(byte[] data) throws IOException;
}
import java.io.IOException;
import java.util.List;
+import org.apache.poi.sl.usermodel.PictureData;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+
public interface SlideShow {
Slide<? extends Shape, ? extends SlideShow, ? extends Notes<?,?>> createSlide() throws IOException;
*
* @return the page size
*/
- Dimension getPageSize();
+ Dimension getPageSize();
+
+
+ /**
+ * Adds a picture to the workbook.
+ *
+ * @param pictureData The bytes of the picture
+ * @param format The format of the picture.
+ *
+ * @return the new picture reference
+ */
+ PictureData addPicture(byte[] pictureData, PictureType format) throws IOException;
}
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.sl.usermodel.MasterSheet;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.Resources;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.Beta;
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?"));
_pictures = new ArrayList<XSLFPictureData>(mediaParts.size());
for(PackagePart part : mediaParts){
- _pictures.add(new XSLFPictureData(part, null));
+ XSLFPictureData pd = new XSLFPictureData(part, null);
+ pd.setIndex(_pictures.size());
+ _pictures.add(pd);
}
}
return Collections.unmodifiableList(_pictures);
* @see XSLFPictureData#PICTURE_TYPE_PNG
* @see XSLFPictureData#PICTURE_TYPE_DIB
*/
- public int addPicture(byte[] pictureData, int format) {
+ public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
XSLFPictureData img = findPictureData(pictureData);
- // POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format];
-
- if(img == null) {
- int imageNumber = _pictures.size();
- img = (XSLFPictureData) createRelationship(
- XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber + 1, true);
- _pictures.add(img);
- try {
- OutputStream out = img.getPackagePart().getOutputStream();
- out.write(pictureData);
- out.close();
- } catch (IOException e) {
- throw new POIXMLException(e);
- }
- return _pictures.size() - 1;
- } else {
- return _pictures.indexOf(img);
+
+ if (img != null) return img;
+
+ int imageNumber = _pictures.size();
+ XSLFRelation relType = XSLFPictureData.getRelationForType(format);
+ if (relType == null) {
+ throw new IllegalArgumentException("Picture type "+format+" is not supported.");
}
+ img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true);
+ img.setIndex(imageNumber);
+ _pictures.add(img);
+ try {
+ OutputStream out = img.getPackagePart().getOutputStream();
+ out.write(pictureData);
+ out.close();
+ } catch (IOException e) {
+ throw new POIXMLException(e);
+ }
+
+ return img;
}
/**
return null;
}
- public MasterSheet createMasterSheet() throws IOException {
+ public MasterSheet<XSLFShape, XMLSlideShow> createMasterSheet() throws IOException {
// TODO: implement!
throw new UnsupportedOperationException();
}
import java.util.ArrayList;\r
import java.util.Iterator;\r
import java.util.List;\r
-import java.util.regex.Pattern;\r
\r
import org.apache.poi.openxml4j.opc.PackagePart;\r
import org.apache.poi.openxml4j.opc.PackageRelationship;\r
return sh;\r
}\r
\r
- public XSLFPictureShape createPicture(int pictureIndex){\r
-\r
- List<PackagePart> pics = getSheet().getPackagePart().getPackage()\r
- .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));\r
-\r
- if(pics.size() == 0) {\r
- throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");\r
- }\r
-\r
- PackagePart pic = pics.get(0);\r
+ public XSLFPictureShape createPicture(XSLFPictureData pictureData){\r
+ PackagePart pic = pictureData.getPackagePart();\r
\r
PackageRelationship rel = getSheet().getPackagePart().addRelationship(\r
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());\r
} else if (shape instanceof XSLFPictureShape) {\r
XSLFPictureShape p = (XSLFPictureShape)shape;\r
XSLFPictureData pd = p.getPictureData();\r
- int picId = getSheet().getSlideShow().addPicture(pd.getData(), pd.getPictureType());\r
- newShape = createPicture(picId);\r
+ XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());\r
+ newShape = createPicture(pdNew);\r
} else if (shape instanceof XSLFGroupShape) {\r
newShape = createGroup();\r
} else if (shape instanceof XSLFTable) {\r
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
-import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.sl.usermodel.PictureData;
/**
* Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type
- *
- * @author Yegor Kozlov
*/
@Beta
public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData {
- /**
- * Extended windows meta file
- */
- public static final int PICTURE_TYPE_EMF = 2;
-
- /**
- * Windows Meta File
- */
- public static final int PICTURE_TYPE_WMF = 3;
-
- /**
- * Mac PICT format
- */
- public static final int PICTURE_TYPE_PICT = 4;
-
- /**
- * JPEG format
- */
- public static final int PICTURE_TYPE_JPEG = 5;
-
- /**
- * PNG format
- */
- public static final int PICTURE_TYPE_PNG = 6;
-
- /**
- * Device independent bitmap
- */
- public static final int PICTURE_TYPE_DIB = 7;
-
- /**
- * GIF image format
- */
- public static final int PICTURE_TYPE_GIF = 8;
-
- /**
- * Tag Image File (.tiff)
- */
- public static final int PICTURE_TYPE_TIFF = 9;
-
- /**
- * Encapsulated Postscript (.eps)
- */
- public static final int PICTURE_TYPE_EPS = 10;
-
-
- /**
- * Windows Bitmap (.bmp)
- */
- public static final int PICTURE_TYPE_BMP = 11;
-
- /**
- * WordPerfect graphics (.wpg)
- */
- public static final int PICTURE_TYPE_WPG = 12;
-
- /**
- * Microsoft Windows Media Photo image (.wdp)
- */
- public static final int PICTURE_TYPE_WDP = 13;
-
- /**
- * Relationships for each known picture type
- */
- protected static final POIXMLRelation[] RELATIONS;
-
- static {
- RELATIONS = new POIXMLRelation[14];
- RELATIONS[PICTURE_TYPE_EMF] = XSLFRelation.IMAGE_EMF;
- RELATIONS[PICTURE_TYPE_WMF] = XSLFRelation.IMAGE_WMF;
- RELATIONS[PICTURE_TYPE_PICT] = XSLFRelation.IMAGE_PICT;
- RELATIONS[PICTURE_TYPE_JPEG] = XSLFRelation.IMAGE_JPEG;
- RELATIONS[PICTURE_TYPE_PNG] = XSLFRelation.IMAGE_PNG;
- RELATIONS[PICTURE_TYPE_DIB] = XSLFRelation.IMAGE_DIB;
- RELATIONS[PICTURE_TYPE_GIF] = XSLFRelation.IMAGE_GIF;
- RELATIONS[PICTURE_TYPE_TIFF] = XSLFRelation.IMAGE_TIFF;
- RELATIONS[PICTURE_TYPE_EPS] = XSLFRelation.IMAGE_EPS;
- RELATIONS[PICTURE_TYPE_BMP] = XSLFRelation.IMAGE_BMP;
- RELATIONS[PICTURE_TYPE_WPG] = XSLFRelation.IMAGE_WPG;
- RELATIONS[PICTURE_TYPE_WDP] = XSLFRelation.IMAGE_WDP;
- }
-
private Long checksum = null;
+ private int index = -1;
/**
* Create a new XSLFGraphicData node
public InputStream getInputStream() throws IOException {
return getPackagePart().getInputStream();
}
-
+
/**
* Gets the picture data as a byte array.
*
* You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method
- *
+ *
* @return the Picture data.
*/
public byte[] getData() {
return getPackagePart().getPartName().getExtension();
}
- /**
- * Return an integer constant that specifies type of this picture
- *
- * @return an integer constant that specifies type of this picture
- */
- public int getPictureType() {
- String contentType = getPackagePart().getContentType();
- for (int i = 0; i < RELATIONS.length; i++) {
- if (RELATIONS[i] == null) {
- continue;
- }
-
- if (RELATIONS[i].getContentType().equals(contentType)) {
- return i;
- }
- }
- return 0;
- }
-
long getChecksum(){
if(checksum == null){
try {
}
/**
- * *PictureData objects store the actual content in the part directly without keeping a
+ * *PictureData objects store the actual content in the part directly without keeping a
* copy like all others therefore we need to handle them differently.
*/
@Override
protected void prepareForCommit() {
// do not clear the part here
}
-
+
+ @Override
public String getContentType() {
- POIXMLRelation rel = RELATIONS[getPictureType()];
- return (rel == null) ? null : rel.getContentType();
+ return getPackagePart().getContentType();
}
public void setData(byte[] data) throws IOException {
// recalculate now since we already have the data bytes available anyhow
checksum = IOUtils.calculateChecksum(data);
}
+
+ @Override
+ public PictureType getType() {
+ String ct = getContentType();
+ if (XSLFRelation.IMAGE_EMF.getContentType().equals(ct)) {
+ return PictureType.EMF;
+ } else if (XSLFRelation.IMAGE_WMF.getContentType().equals(ct)) {
+ return PictureType.WMF;
+ } else if (XSLFRelation.IMAGE_PICT.getContentType().equals(ct)) {
+ return PictureType.PICT;
+ } else if (XSLFRelation.IMAGE_JPEG.getContentType().equals(ct)) {
+ return PictureType.JPEG;
+ } else if (XSLFRelation.IMAGE_PNG.getContentType().equals(ct)) {
+ return PictureType.PNG;
+ } else if (XSLFRelation.IMAGE_DIB.getContentType().equals(ct)) {
+ return PictureType.DIB;
+ } else if (XSLFRelation.IMAGE_GIF.getContentType().equals(ct)) {
+ return PictureType.GIF;
+ } else if (XSLFRelation.IMAGE_EPS.getContentType().equals(ct)) {
+ return PictureType.EPS;
+ } else if (XSLFRelation.IMAGE_BMP.getContentType().equals(ct)) {
+ return PictureType.BMP;
+ } else if (XSLFRelation.IMAGE_WPG.getContentType().equals(ct)) {
+ return PictureType.WPG;
+ } else if (XSLFRelation.IMAGE_WDP.getContentType().equals(ct)) {
+ return PictureType.WDP;
+ } else {
+ return null;
+ }
+ }
-
+ /* package */ static XSLFRelation getRelationForType(PictureType pt) {
+ switch (pt) {
+ case EMF: return XSLFRelation.IMAGE_EMF;
+ case WMF: return XSLFRelation.IMAGE_WMF;
+ case PICT: return XSLFRelation.IMAGE_PICT;
+ case JPEG: return XSLFRelation.IMAGE_JPEG;
+ case PNG: return XSLFRelation.IMAGE_PNG;
+ case DIB: return XSLFRelation.IMAGE_DIB;
+ case GIF: return XSLFRelation.IMAGE_GIF;
+ case EPS: return XSLFRelation.IMAGE_EPS;
+ case BMP: return XSLFRelation.IMAGE_BMP;
+ case WPG: return XSLFRelation.IMAGE_WPG;
+ case WDP: return XSLFRelation.IMAGE_WDP;
+ default: return null;
+ }
+ }
+
+ /**
+ * @return the 0-based index of this pictures within the picture parts
+ */
+ public int getIndex() {
+ return index;
+ }
+
+ /**
+ * @param index sets the 0-based index of this pictures within the picture parts
+ */
+ public void setIndex(int index) {
+ this.index = index;
+ }
}
\ No newline at end of file
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLRelation;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.Beta;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
@Beta
public class XSLFRelation extends POIXMLRelation {
- private static final POILogger log = POILogFactory.getLogger(XSLFRelation.class);
-
/**
* A map to lookup POIXMLRelation by its relation type
*/
);
public static final XSLFRelation IMAGE_EMF = new XSLFRelation(
- "image/x-emf",
+ PictureType.EMF.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.emf",
XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
- "image/x-wmf",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.wmf",
- XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
- "image/pict",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.pict",
- XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
- "image/jpeg",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.jpeg",
- XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
- "image/png",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.png",
- XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
- "image/dib",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.dib",
- XSLFPictureData.class
- );
- public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
- "image/gif",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.gif",
- XSLFPictureData.class
- );
+ );
+ public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
+ PictureType.WMF.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.wmf",
+ XSLFPictureData.class
+ );
+ public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
+ PictureType.PICT.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.pict",
+ XSLFPictureData.class
+ );
+ public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
+ PictureType.JPEG.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.jpeg",
+ XSLFPictureData.class
+ );
+ public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
+ PictureType.PNG.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.png",
+ XSLFPictureData.class
+ );
+ public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
+ PictureType.DIB.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.dib",
+ XSLFPictureData.class
+ );
+ public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
+ PictureType.GIF.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.gif",
+ XSLFPictureData.class
+ );
public static final XSLFRelation IMAGE_TIFF = new XSLFRelation(
- "image/tiff",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.tiff",
- XSLFPictureData.class
+ PictureType.TIFF.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.tiff",
+ XSLFPictureData.class
);
public static final XSLFRelation IMAGE_EPS = new XSLFRelation(
- "image/x-eps",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.eps",
- XSLFPictureData.class
+ PictureType.EPS.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.eps",
+ XSLFPictureData.class
);
public static final XSLFRelation IMAGE_BMP = new XSLFRelation(
- "image/x-ms-bmp",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.bmp",
- XSLFPictureData.class
+ PictureType.BMP.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.bmp",
+ XSLFPictureData.class
);
public static final XSLFRelation IMAGE_WPG = new XSLFRelation(
- "image/x-wpg",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.wpg",
- XSLFPictureData.class
+ PictureType.WPG.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.wpg",
+ XSLFPictureData.class
);
public static final XSLFRelation IMAGE_WDP = new XSLFRelation(
- "image/vnd.ms-photo",
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/ppt/media/image#.wdp",
- XSLFPictureData.class
+ PictureType.WDP.contentType,
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/ppt/media/image#.wdp",
+ XSLFPictureData.class
);
public static final XSLFRelation IMAGES = new XSLFRelation(
/**
* create a picture belonging to this container
*/
- XSLFPictureShape createPicture(int pictureIndex);
+ XSLFPictureShape createPicture(XSLFPictureData pictureData);
/**
* Removes all of the elements from this container (optional operation).
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.regex.Pattern;
import javax.xml.namespace.QName;
return sh;
}
- public XSLFPictureShape createPicture(int pictureIndex){
- List<PackagePart> pics = getPackagePart().getPackage()
- .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
-
- if(pics.size() == 0) {
- throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
- }
-
- PackagePart pic = pics.get(0);
+ public XSLFPictureShape createPicture(XSLFPictureData pictureData){
+ PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
XSLFPictureData data = new XSLFPictureData(blipPart, null);
XMLSlideShow ppt = getSlideShow();
- int pictureIdx = ppt.addPicture(data.getData(), data.getPictureType());
- PackagePart pic = ppt.getAllPictures().get(pictureIdx).getPackagePart();
+ XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
+ PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, blipRel.getRelationshipType());
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.DrawingParagraph;
import org.apache.poi.xslf.usermodel.DrawingTextBody;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.junit.Ignore;
import org.junit.Test;
+
+
public class TestXSLFBugs {
@Test
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("51187.pptx");
assertEquals(1, ss.getSlides().size());
- XSLFSlide slide = ss.getSlides().get(0);
// Check the relations on it
// Note - rId3 is a self reference
// Add a few pictures
for (int i=0; i<10; i++) {
- int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
- assertEquals(i, idx);
+ XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
+ assertEquals(i, data.getIndex());
assertEquals(i+1, ss.getAllPictures().size());
- XSLFPictureShape shape = slide.createPicture(idx);
+ XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[i], shape.getPictureData().getData());
assertEquals(i+2, slide.getShapes().size());
// Add past 10
for (int i=10; i<15; i++) {
- int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
- assertEquals(i, idx);
+ XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
+ assertEquals(i, data.getIndex());
assertEquals(i+1, ss.getAllPictures().size());
- XSLFPictureShape shape = slide.createPicture(idx);
+ XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[i], shape.getPictureData().getData());
assertEquals(i+2, slide.getShapes().size());
}
// Add a duplicate, check the right one is picked
- int idx = ss.addPicture(pics[3], XSLFPictureData.PICTURE_TYPE_JPEG);
- assertEquals(3, idx);
+ XSLFPictureData data = ss.addPicture(pics[3], PictureType.JPEG);
+ assertEquals(3, data.getIndex());
assertEquals(15, ss.getAllPictures().size());
- XSLFPictureShape shape = slide.createPicture(idx);
+ XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[3], shape.getPictureData().getData());
assertEquals(17, slide.getShapes().size());
assertArrayEquals(pics[3], shape.getPictureData().getData());
// Add another duplicate
- idx = ss.addPicture(pics[5], XSLFPictureData.PICTURE_TYPE_JPEG);
- assertEquals(5, idx);
+ data = ss.addPicture(pics[5], PictureType.JPEG);
+ assertEquals(5, data.getIndex());
assertEquals(15, ss.getAllPictures().size());
- shape = slide.createPicture(idx);
+ shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[5], shape.getPictureData().getData());
assertEquals(18, slide.getShapes().size());
==================================================================== */\r
package org.apache.poi.xslf.usermodel;\r
\r
-import static org.junit.Assert.*;\r
+import static org.junit.Assert.assertArrayEquals;\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertNotNull;\r
\r
-import java.util.*;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
\r
import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.apache.poi.sl.usermodel.PictureData.PictureType;\r
import org.junit.Test;\r
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
\r
assertEquals(0, ppt.getAllPictures().size());\r
byte[] data1 = new byte[100];\r
for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }\r
- int idx1 = ppt.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
- assertEquals(0, idx1);\r
+ XSLFPictureData pdata1 = ppt.addPicture(data1, PictureType.JPEG);\r
+ assertEquals(0, pdata1.getIndex());\r
assertEquals(1, ppt.getAllPictures().size());\r
\r
XSLFSlide slide = ppt.createSlide();\r
- XSLFPictureShape shape1 = slide.createPicture(idx1);\r
+ XSLFPictureShape shape1 = slide.createPicture(pdata1);\r
assertNotNull(shape1.getPictureData());\r
assertArrayEquals(data1, shape1.getPictureData().getData());\r
\r
byte[] data2 = new byte[200];\r
for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }\r
- int idx2 = ppt.addPicture(data2, XSLFPictureData.PICTURE_TYPE_PNG);\r
- XSLFPictureShape shape2 = slide.createPicture(idx2);\r
+ XSLFPictureData pdata2 = ppt.addPicture(data2, PictureType.PNG);\r
+ XSLFPictureShape shape2 = slide.createPicture(pdata2);\r
assertNotNull(shape2.getPictureData());\r
- assertEquals(1, idx2);\r
+ assertEquals(1, pdata2.getIndex());\r
assertEquals(2, ppt.getAllPictures().size());\r
assertArrayEquals(data2, shape2.getPictureData().getData());\r
\r
// first add 20 images to the slide\r
for (int i = 0; i < 20; i++, pictureIndex++) {\r
byte[] data = new byte[]{(byte)pictureIndex};\r
- int elementIndex = ppt.addPicture(data,\r
- XSLFPictureData.PICTURE_TYPE_PNG);\r
- assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19\r
- XSLFPictureShape picture = slide1.createPicture(elementIndex);\r
+ XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);\r
+ assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19\r
+ XSLFPictureShape picture = slide1.createPicture(elementData);\r
// POI saves images as image1.png, image2.png, etc.\r
- String fileName = "image" + (elementIndex + 1) + ".png";\r
+ String fileName = "image" + (elementData.getIndex()+1) + ".png";\r
assertEquals(fileName, picture.getPictureData().getFileName());\r
assertArrayEquals(data, picture.getPictureData().getData());\r
}\r
// and then add next 20 images to a group\r
for (int i = 0; i < 20; i++, pictureIndex++) {\r
byte[] data = new byte[]{(byte)pictureIndex};\r
- int elementIndex = ppt.addPicture(data,\r
- XSLFPictureData.PICTURE_TYPE_PNG);\r
- XSLFPictureShape picture = group1.createPicture(elementIndex);\r
+ XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);\r
+ XSLFPictureShape picture = group1.createPicture(elementData);\r
// POI saves images as image1.png, image2.png, etc.\r
- assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19\r
+ assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19\r
String fileName = "image" + (pictureIndex + 1) + ".png";\r
assertEquals(fileName, picture.getPictureData().getFileName());\r
assertArrayEquals(data, picture.getPictureData().getData());\r
XMLSlideShow ppt = new XMLSlideShow();\r
byte[] img1 = new byte[]{1,2,3};\r
byte[] img2 = new byte[]{3,4,5};\r
- int idx1 = ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG);\r
- assertEquals(0, idx1);\r
- assertEquals(0, ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG));\r
+ XSLFPictureData pdata1 = ppt.addPicture(img1, PictureType.PNG);\r
+ assertEquals(0, pdata1.getIndex());\r
+ assertEquals(0, ppt.addPicture(img1, PictureType.PNG).getIndex());\r
\r
- int idx2 = ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG);\r
- assertEquals(1, idx2);\r
- assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));\r
+ XSLFPictureData idx2 = ppt.addPicture(img2, PictureType.PNG);\r
+ assertEquals(1, idx2.getIndex());\r
+ assertEquals(1, ppt.addPicture(img2, PictureType.PNG).getIndex());\r
\r
XSLFSlide slide1 = ppt.createSlide();\r
assertNotNull(slide1);\r
public void testMerge() {\r
XMLSlideShow ppt1 = new XMLSlideShow();\r
byte[] data1 = new byte[100];\r
- int idx1 = ppt1.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
+ XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG);\r
\r
XSLFSlide slide1 = ppt1.createSlide();\r
- XSLFPictureShape shape1 = slide1.createPicture(idx1);\r
+ XSLFPictureShape shape1 = slide1.createPicture(pdata1);\r
CTPicture ctPic1 = (CTPicture)shape1.getXmlObject();\r
ctPic1.getNvPicPr().getNvPr().addNewCustDataLst().addNewTags().setId("rId99");\r
\r
/**
* Represents a bitmap picture data: JPEG or PNG.
* The data is not compressed and the exact file content is written in the stream.
- *
- * @author Yegor Kozlov
*/
-public abstract class Bitmap extends HSLFPictureData {
+public abstract class Bitmap extends HSLFPictureData {
+ @Override
public byte[] getData(){
byte[] rawdata = getRawData();
int prefixLen = 16*uidInstanceCount+1;
return imgdata;
}
+ @Override
public void setData(byte[] data) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i=0; i<uidInstanceCount; i++) {
package org.apache.poi.hslf.blip;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
-import org.apache.poi.util.LittleEndian;
-
import java.io.IOException;
+import org.apache.poi.util.LittleEndian;
+
/**
* Represents a DIB picture data in a PPT file
- *
- * @author Yegor Kozlov
*/
public final class DIB extends Bitmap {
/**
*/
private static final int HEADER_SIZE = 14;
- /**
- * @return type of this picture
- * @see org.apache.poi.hslf.usermodel.HSLFPictureShape#DIB
- */
- public int getType(){
- return HSLFPictureShape.DIB;
+ @Override
+ public PictureType getType(){
+ return PictureType.DIB;
}
/**
}
}
+ @Override
public byte[] getData(){
return addBMPHeader ( super.getData() );
}
return dib;
}
+ @Override
public void setData(byte[] data) throws IOException {
//cut off the bitmap file-header
byte[] dib = new byte[data.length-HEADER_SIZE];
System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);
super.setData(dib);
}
-
- public String getContentType() {
- return "image/bmp";
- }
}
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.Units;
/**
* Represents EMF (Windows Enhanced Metafile) picture data.
- *
- * @author Yegor Kozlov
*/
public final class EMF extends Metafile {
- /**
- * Extract compressed EMF data from a ppt
- */
+ @Override
public byte[] getData(){
try {
byte[] rawdata = getRawData();
}
}
+ @Override
public void setData(byte[] data) throws IOException {
byte[] compressed = compress(data, 0, data.length);
setRawData(out.toByteArray());
}
- public int getType(){
- return HSLFPictureShape.EMF;
+ @Override
+ public PictureType getType(){
+ return PictureType.EMF;
}
/**
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for EMF");
}
}
-
- public String getContentType() {
- return "image/x-emf";
- }
}
package org.apache.poi.hslf.blip;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
/**
* Represents a JPEG picture data in a PPT file
- *
- * @author Yegor Kozlov
*/
public final class JPEG extends Bitmap {
private ColorSpace colorSpace = ColorSpace.rgb;
- /**
- * @return type of this picture
- * @see org.apache.poi.hslf.usermodel.HSLFPictureShape#JPEG
- */
- public int getType(){
- return HSLFPictureShape.JPEG;
+ @Override
+ public PictureType getType(){
+ return PictureType.JPEG;
}
public ColorSpace getColorSpace() {
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for JPEG");
}
}
-
- public String getContentType() {
- return "image/jpeg";
- }
}
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.Units;
/**
* Represents Macintosh PICT picture data.
- *
- * @author Yegor Kozlov
*/
public final class PICT extends Metafile {
- /**
- * Extract compressed PICT data from a ppt
- */
+ @Override
public byte[] getData(){
byte[] rawdata = getRawData();
try {
return out.toByteArray();
}
+ @Override
public void setData(byte[] data) throws IOException {
int pos = 512; //skip the first 512 bytes - they are MAC specific crap
byte[] compressed = compress(data, pos, data.length-pos);
setRawData(out.toByteArray());
}
- /**
- * @see org.apache.poi.hslf.usermodel.HSLFPictureShape#PICT
- */
- public int getType(){
- return HSLFPictureShape.PICT;
+ @Override
+ public PictureType getType(){
+ return PictureType.PICT;
}
/**
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT");
}
}
-
- public String getContentType() {
- return "image/x-pict";
- }
-
}
package org.apache.poi.hslf.blip;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.PngUtils;
/**
* Represents a PNG picture data in a PPT file
- *
- * @author Yegor Kozlov
*/
public final class PNG extends Bitmap {
- /**
- * @return PNG data
- */
+ @Override
public byte[] getData() {
byte[] data = super.getData();
return data;
}
- /**
- * @return type of this picture
- * @see org.apache.poi.hslf.usermodel.HSLFPictureShape#PNG
- */
- public int getType(){
- return HSLFPictureShape.PNG;
+ @Override
+ public PictureType getType(){
+ return PictureType.PNG;
}
/**
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PNG");
}
}
-
- public String getContentType() {
- return "image/png";
- }
}
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Units;
/**
* Represents a WMF (Windows Metafile) picture data.
- *
- * @author Yegor Kozlov
*/
public final class WMF extends Metafile {
- /**
- * Extract compressed WMF data from a ppt
- */
+ @Override
public byte[] getData(){
try {
byte[] rawdata = getRawData();
}
}
+ @Override
public void setData(byte[] data) throws IOException {
int pos = 0;
AldusHeader aldus = new AldusHeader();
setRawData(out.toByteArray());
}
- /**
- * We are of type <code>Picture.WMF</code>
- */
- public int getType(){
- return HSLFPictureShape.WMF;
+ @Override
+ public PictureType getType(){
+ return PictureType.WMF;
}
/**
return 22;
}
}
-
-
- public String getContentType() {
- return "image/x-wmf";
- }
-
}
package org.apache.poi.hslf.extractor;
import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import java.io.IOException;
import java.io.FileOutputStream;
// picture data
byte[] data = pict.getData();
- int type = pict.getType();
- String ext;
- switch (type) {
- case HSLFPictureShape.JPEG:
- ext = ".jpg";
- break;
- case HSLFPictureShape.PNG:
- ext = ".png";
- break;
- case HSLFPictureShape.WMF:
- ext = ".wmf";
- break;
- case HSLFPictureShape.EMF:
- ext = ".emf";
- break;
- case HSLFPictureShape.PICT:
- ext = ".pict";
- break;
- case HSLFPictureShape.DIB:
- ext = ".dib";
- break;
- default:
- continue;
- }
- FileOutputStream out = new FileOutputStream("pict_" + i + ext);
+ PictureType type = pict.getType();
+ FileOutputStream out = new FileOutputStream("pict_" + i + type.extension);
out.write(data);
out.close();
}
/**
* Create a new <code>Picture</code>
*
- * @param pictureIdx the index of the picture
+ * @param pictureData the picture data
*/
- public ActiveXShape(int movieIdx, int pictureIdx){
- super(pictureIdx, null);
+ public ActiveXShape(int movieIdx, HSLFPictureData pictureData){
+ super(pictureData, null);
setActiveXIndex(movieIdx);
}
/**
* Create a new <code>Picture</code>
*
- * @param pictureIdx the index of the picture
+ * @param pictureData the picture data
*/
- public MovieShape(int movieIdx, int pictureIdx){
- super(pictureIdx, null);
+ public MovieShape(int movieIdx, HSLFPictureData pictureData){
+ super(pictureData, null);
setMovieIndex(movieIdx);
setAutoPlay(true);
}
/**
* Create a new <code>Picture</code>
*
- * @param idx the index of the picture
+ * @param pictureData the picture data
* @param parent the parent shape
*/
- public MovieShape(int movieIdx, int idx, ShapeContainer<HSLFShape> parent) {
- super(idx, parent);
+ public MovieShape(int movieIdx, HSLFPictureData pictureData, ShapeContainer<HSLFShape> parent) {
+ super(pictureData, parent);
setMovieIndex(movieIdx);
}
/**
* Create a new <code>OLEShape</code>
*
- * @param idx the index of the picture
+ * @param data the picture data
*/
- public OLEShape(int idx){
- super(idx);
+ public OLEShape(HSLFPictureData data){
+ super(data);
}
/**
* @param idx the index of the picture
* @param parent the parent shape
*/
- public OLEShape(int idx, ShapeContainer<HSLFShape> parent) {
- super(idx, parent);
+ public OLEShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
+ super(data, parent);
}
/**
/**
* Assign picture used to fill the underlying shape.
*
- * @param idx 0-based index of the picture added to this ppt by <code>SlideShow.addPicture</code> method.
+ * @param data the picture data added to this ppt by {@link HSLFSlideShow#addPicture(byte[], org.apache.poi.sl.usermodel.PictureData.PictureType)} method.
*/
- public void setPictureData(int idx){
+ public void setPictureData(HSLFPictureData data){
EscherOptRecord opt = shape.getEscherOptRecord();
- HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), idx);
- if( idx != 0 ) {
- if( shape.getSheet() != null ) {
- EscherBSERecord bse = getEscherBSERecord(idx);
- bse.setRef(bse.getRef() + 1);
- }
+ HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), (data == null ? 0 : data.getIndex()));
+ if(data != null && shape.getSheet() != null) {
+ EscherBSERecord bse = getEscherBSERecord(data.getIndex());
+ bse.setRef(bse.getRef() + 1);
}
}
* The instance type/signatures defines if one or two UID instances will be included
*/
protected int uidInstanceCount = 1;
-
- /**
- * Returns type of this picture.
- * Must be one of the static constants defined in the <code>Picture<code> class.
- *
- * @return type of this picture.
- */
- public abstract int getType();
-
/**
- * Returns content type (mime type) of this picture.
- *
- * @return content type of this picture.
+ * The 1-based index within the pictures stream
*/
- public abstract String getContentType();
+ protected int index = -1;
- /**
- * Returns the binary data of this Picture
- * @return picture data
- */
- public abstract byte[] getData();
-
- /**
- * Set picture data
- */
- public abstract void setData(byte[] data) throws IOException;
-
/**
* Blip signature.
*/
out.write(data);
data = new byte[LittleEndian.SHORT_SIZE];
- LittleEndian.putUShort(data, 0, getType() + 0xF018);
+ PictureType pt = getType();
+ LittleEndian.putUShort(data, 0, pt.nativeId + 0xF018);
out.write(data);
byte[] rawdata = getRawData();
* Must be one of the static constants defined in the <code>Picture<code> class.
* @return concrete instance of <code>PictureData</code>
*/
- public static HSLFPictureData create(int type){
+ public static HSLFPictureData create(PictureType type){
HSLFPictureData pict;
switch (type){
- case HSLFPictureShape.EMF:
- pict = new EMF();
- break;
- case HSLFPictureShape.WMF:
- pict = new WMF();
- break;
- case HSLFPictureShape.PICT:
- pict = new PICT();
- break;
- case HSLFPictureShape.JPEG:
- pict = new JPEG();
- break;
- case HSLFPictureShape.PNG:
- pict = new PNG();
- break;
- case HSLFPictureShape.DIB:
- pict = new DIB();
- break;
+ case EMF: pict = new EMF(); break;
+ case WMF: pict = new WMF(); break;
+ case PICT: pict = new PICT(); break;
+ case JPEG: pict = new JPEG(); break;
+ case PNG: pict = new PNG(); break;
+ case DIB: pict = new DIB(); break;
default:
throw new IllegalArgumentException("Unsupported picture type: " + type);
}
public int getSize(){
return getData().length;
}
+
+ /**
+ * @return the 1-based index of this pictures within the pictures stream
+ */
+ public int getIndex() {
+ return index;
+ }
+
+ /**
+ * @param index sets the 1-based index of this pictures within the pictures stream
+ */
+ public void setIndex(int index) {
+ this.index = index;
+ }
+
+ @Override
+ public final String getContentType() {
+ return getType().contentType;
+ }
}
*/
public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
- /**
- * Windows Enhanced Metafile (EMF)
- */
- public static final int EMF = 2;
-
- /**
- * Windows Metafile (WMF)
- */
- public static final int WMF = 3;
-
- /**
- * Macintosh PICT
- */
- public static final int PICT = 4;
-
- /**
- * JPEG
- */
- public static final int JPEG = 5;
-
- /**
- * PNG
- */
- public static final int PNG = 6;
-
- /**
- * Windows DIB (BMP)
- */
- public static final byte DIB = 7;
-
/**
* Create a new <code>Picture</code>
*
* @param idx the index of the picture
*/
- public HSLFPictureShape(int idx){
- this(idx, null);
+ public HSLFPictureShape(HSLFPictureData data){
+ this(data, null);
}
/**
* @param idx the index of the picture
* @param parent the parent shape
*/
- public HSLFPictureShape(int idx, ShapeContainer<HSLFShape> parent) {
+ public HSLFPictureShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
super(null, parent);
- _escherContainer = createSpContainer(idx, parent instanceof HSLFGroupShape);
+ _escherContainer = createSpContainer(data.getIndex(), parent instanceof HSLFGroupShape);
}
/**
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherOptRecord;
-import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.model.*;
-import org.apache.poi.hslf.record.Document;
-import org.apache.poi.hslf.record.DocumentAtom;
-import org.apache.poi.hslf.record.ExAviMovie;
-import org.apache.poi.hslf.record.ExControl;
-import org.apache.poi.hslf.record.ExEmbed;
-import org.apache.poi.hslf.record.ExEmbedAtom;
-import org.apache.poi.hslf.record.ExHyperlink;
-import org.apache.poi.hslf.record.ExHyperlinkAtom;
-import org.apache.poi.hslf.record.ExMCIMovie;
-import org.apache.poi.hslf.record.ExObjList;
-import org.apache.poi.hslf.record.ExObjListAtom;
-import org.apache.poi.hslf.record.ExOleObjAtom;
-import org.apache.poi.hslf.record.ExOleObjStg;
-import org.apache.poi.hslf.record.ExVideoContainer;
-import org.apache.poi.hslf.record.FontCollection;
-import org.apache.poi.hslf.record.FontEntityAtom;
-import org.apache.poi.hslf.record.HeadersFootersContainer;
-import org.apache.poi.hslf.record.PersistPtrHolder;
-import org.apache.poi.hslf.record.PositionDependentRecord;
-import org.apache.poi.hslf.record.PositionDependentRecordContainer;
-import org.apache.poi.hslf.record.Record;
-import org.apache.poi.hslf.record.RecordContainer;
-import org.apache.poi.hslf.record.RecordTypes;
-import org.apache.poi.hslf.record.SlideListWithText;
+import org.apache.poi.hslf.model.HeadersFooters;
+import org.apache.poi.hslf.model.MovieShape;
+import org.apache.poi.hslf.model.PPFont;
+import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
-import org.apache.poi.hslf.record.SlidePersistAtom;
-import org.apache.poi.hslf.record.UserEditAtom;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.sl.usermodel.*;
+import org.apache.poi.sl.usermodel.MasterSheet;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+import org.apache.poi.sl.usermodel.Resources;
+import org.apache.poi.sl.usermodel.Shape;
+import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Units;
return slide;
}
- /**
- * Adds a picture to this presentation and returns the associated index.
- *
- * @param data
- * picture data
- * @param format
- * the format of the picture. One of constans defined in the
- * <code>Picture</code> class.
- * @return the index to this picture (1 based).
- */
- public int addPicture(byte[] data, int format) throws IOException {
- byte[] uid = HSLFPictureData.getChecksum(data);
+ @Override
+ public HSLFPictureData addPicture(byte[] data, PictureType format) throws IOException {
+ if (format == null || format.nativeId == -1) {
+ throw new IllegalArgumentException("Unsupported picture format: " + format);
+ }
+
+ byte[] uid = HSLFPictureData.getChecksum(data);
+ for (HSLFPictureData pd : getPictureData()) {
+ if (Arrays.equals(pd.getUID(), uid)) {
+ return pd;
+ }
+ }
+
EscherContainerRecord bstore;
EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
bstore.setRecordId(EscherContainerRecord.BSTORE_CONTAINER);
dggContainer.addChildBefore(bstore, EscherOptRecord.RECORD_ID);
- } else {
- Iterator<EscherRecord> iter = bstore.getChildIterator();
- for (int i = 0; iter.hasNext(); i++) {
- EscherBSERecord bse = (EscherBSERecord) iter.next();
- if (Arrays.equals(bse.getUid(), uid)) {
- return i + 1;
- }
- }
}
HSLFPictureData pict = HSLFPictureData.create(format);
EscherBSERecord bse = new EscherBSERecord();
bse.setRecordId(EscherBSERecord.RECORD_ID);
- bse.setOptions((short) (0x0002 | (format << 4)));
+ bse.setOptions((short) (0x0002 | (format.nativeId << 4)));
bse.setSize(pict.getRawData().length + 8);
bse.setUid(uid);
- bse.setBlipTypeMacOS((byte) format);
- bse.setBlipTypeWin32((byte) format);
+ bse.setBlipTypeMacOS((byte) format.nativeId);
+ bse.setBlipTypeWin32((byte) format.nativeId);
- if (format == HSLFPictureShape.EMF)
- bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
- else if (format == HSLFPictureShape.WMF)
- bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
- else if (format == HSLFPictureShape.PICT)
- bse.setBlipTypeWin32((byte) HSLFPictureShape.WMF);
+ if (format == PictureType.EMF) {
+ bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
+ } else if (format == PictureType.WMF) {
+ bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
+ } else if (format == PictureType.PICT) {
+ bse.setBlipTypeWin32((byte) PictureType.WMF.nativeId);
+ }
bse.setRef(0);
bse.setOffset(offset);
int count = bstore.getChildRecords().size();
bstore.setOptions((short) ((count << 4) | 0xF));
- return count;
+ return pict;
}
/**
* <code>Picture</code> class.
* @return the index to this picture (1 based).
*/
- public int addPicture(File pict, int format) throws IOException {
+ public HSLFPictureData addPicture(File pict, PictureType format) throws IOException {
int length = (int) pict.length();
byte[] data = new byte[length];
FileInputStream is = null;
import org.apache.poi.poifs.filesystem.EntryUtils;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
}
// If they type (including the bonus 0xF018) is 0, skip it
- if(type == 0) {
+ PictureType pt = PictureType.forNativeID(type - 0xF018);
+ if(type == 0 || pt == null) {
logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
logger.log(POILogger.ERROR, "" + pos);
} else {
// Build the PictureData object from the data
- try {
- HSLFPictureData pict = HSLFPictureData.create(type - 0xF018);
+ try {
+ HSLFPictureData pict = HSLFPictureData.create(pt);
// Copy the data, ready to pass to PictureData
byte[] imgdata = new byte[imgsize];
pict.setRawData(imgdata);
pict.setOffset(offset);
+ pict.setIndex(_pictures.size());
_pictures.add(pict);
} catch(IllegalArgumentException e) {
logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
offset = prev.getOffset() + prev.getRawData().length + 8;
}
img.setOffset(offset);
+ img.setIndex(_pictures.size()+1);
_pictures.add(img);
return offset;
}
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.hslf.record.Document;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFAutoShape;
+import org.apache.poi.hslf.usermodel.HSLFFill;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFShape;
+import org.apache.poi.hslf.usermodel.HSLFSheet;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.ShapeType;
import org.junit.Test;
HSLFSlide slide;
HSLFFill fill;
HSLFShape shape;
- int idx;
+ HSLFPictureData data;
//slide 1
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+ data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PICTURE);
- fill.setPictureData(idx);
+ fill.setPictureData(data);
shape = new HSLFAutoShape(ShapeType.RECT);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+ data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PATTERN);
- fill.setPictureData(idx);
+ fill.setPictureData(data);
fill.setBackgroundColor(Color.green);
fill.setForegroundColor(Color.red);
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+ data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_TEXTURE);
- fill.setPictureData(idx);
+ fill.setPictureData(data);
shape = new HSLFAutoShape(ShapeType.RECT);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
fill = shape.getFill();
fill.setFillType(HSLFFill.FILL_PICTURE);
- idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
- fill.setPictureData(idx);
+ data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+ fill.setPictureData(data);
slide.addShape(shape);
// slide 4
assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
shape = slides.get(3).getShapes().get(0);
assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
-
}
private int getFillPictureRefCount(HSLFShape shape, HSLFFill fill) {
package org.apache.poi.hslf.model;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
/**
String path = "/test-movie.mpg";
int movieIdx = ppt.addMovie(path, MovieShape.MOVIE_MPEG);
- int thumbnailIdx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+ HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
- MovieShape shape = new MovieShape(movieIdx, thumbnailIdx);
+ MovieShape shape = new MovieShape(movieIdx, thumbnailData);
shape.setAnchor(new Rectangle2D.Float(300,225,120,90));
slide.addShape(shape);
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
public final class TestOleEmbedding {
HSLFSlideShow ppt = new HSLFSlideShow(_hslfSlideShow);
File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg");
- int pictId = ppt.addPicture(pict, HSLFPictureShape.JPEG);
+ HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG);
InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("Employee.xls");
POIFSFileSystem poiData1 = new POIFSFileSystem(is);
int oleObjectId1 = ppt.addEmbed(poiData1);
HSLFSlide slide1 = ppt.createSlide();
- OLEShape oleShape1 = new OLEShape(pictId);
+ OLEShape oleShape1 = new OLEShape(pictData);
oleShape1.setObjectID(oleObjectId1);
slide1.addShape(oleShape1);
oleShape1.setAnchor(new Rectangle2D.Double(100,100,100,100));
// add second slide with different order in object creation
HSLFSlide slide2 = ppt.createSlide();
- OLEShape oleShape2 = new OLEShape(pictId);
+ OLEShape oleShape2 = new OLEShape(pictData);
is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleWithImages.xls");
POIFSFileSystem poiData2 = new POIFSFileSystem(is);
package org.apache.poi.hslf.model;
-import static org.junit.Assert.*;
-
-import java.awt.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.ddf.*;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.ddf.EscherDgRecord;
+import org.apache.poi.ddf.EscherDggRecord;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherSimpleProperty;
+import org.apache.poi.hslf.usermodel.HSLFAutoShape;
+import org.apache.poi.hslf.usermodel.HSLFGroupShape;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFPictureShape;
+import org.apache.poi.hslf.usermodel.HSLFShape;
+import org.apache.poi.hslf.usermodel.HSLFSimpleShape;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.hslf.usermodel.HSLFTextBox;
+import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
+import org.apache.poi.hslf.usermodel.HSLFTextRun;
+import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Before;
import org.junit.Test;
group.setAnchor(new Rectangle(0, 0, (int)pgsize.getWidth(), (int)pgsize.getHeight()));
slide.addShape(group);
- int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
- HSLFPictureShape pict = new HSLFPictureShape(idx, group);
+ HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+ HSLFPictureShape pict = new HSLFPictureShape(data, group);
pict.setAnchor(new Rectangle(0, 0, 200, 200));
group.addShape(pict);
package org.apache.poi.hslf.usermodel;
-import static org.junit.Assert.*;
-
-import java.awt.Color;
-import java.awt.Rectangle;
-import java.io.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
-import org.apache.poi.hslf.model.*;
-import org.apache.poi.hslf.model.textproperties.TextPropCollection;
-import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.model.HeadersFooters;
+import org.apache.poi.hslf.record.Document;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.SlideListWithText;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.util.*;
-import org.junit.Ignore;
+import org.apache.poi.hslf.record.TextHeaderAtom;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.StringUtil;
+import org.apache.poi.util.Units;
import org.junit.Test;
/**
HSLFPictureData[] pict = ppt.getPictureData();
assertEquals(2, pict.length);
- assertEquals(HSLFPictureShape.JPEG, pict[0].getType());
- assertEquals(HSLFPictureShape.JPEG, pict[1].getType());
+ assertEquals(PictureType.JPEG, pict[0].getType());
+ assertEquals(PictureType.JPEG, pict[1].getType());
}
/**
HSLFPictureData pict = f.getPictureData();
assertNotNull(pict);
- assertEquals(HSLFPictureShape.JPEG, pict.getType());
+ assertEquals(PictureType.JPEG, pict.getType());
}
/**
package org.apache.poi.hslf.usermodel;
-import static org.junit.Assert.*;
-
-import java.awt.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
import java.awt.image.BufferedImage;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
import java.lang.reflect.Constructor;
-import java.util.*;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hssf.usermodel.DummyGraphics2d;
import org.apache.poi.sl.draw.Drawable;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.JvmBugs;
HSLFSlide s2 = ppt.createSlide();
HSLFSlide s3 = ppt.createSlide();
- int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- HSLFPictureShape pict2 = new HSLFPictureShape(idx);
- HSLFPictureShape pict3 = new HSLFPictureShape(idx);
+ HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ HSLFPictureShape pict2 = new HSLFPictureShape(data);
+ HSLFPictureShape pict3 = new HSLFPictureShape(data);
pict.setAnchor(new Rectangle(10,10,100,100));
s.addShape(pict);
public void bug46122() {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
-
- HSLFPictureShape pict = new HSLFPictureShape(-1); //index to non-existing picture data
+ HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG);
+
+ HSLFPictureShape pict = new HSLFPictureShape(pd); //index to non-existing picture data
pict.setSheet(slide);
HSLFPictureData data = pict.getPictureData();
assertNull(data);
null // EMF
};
- for (int i = 0; i < pictures.length; i++) {
- BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures[i].getData()));
-
- if (pictures[i].getType() != HSLFPictureShape.WMF && pictures[i].getType() != HSLFPictureShape.EMF) {
- assertNotNull(image);
-
- int[] dimensions = expectedSizes[i];
- assertEquals(dimensions[0], image.getWidth());
- assertEquals(dimensions[1], image.getHeight());
+ int i = 0;
+ for (HSLFPictureData pd : pictures) {
+ int[] dimensions = expectedSizes[i++];
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(pd.getData()));
+ switch (pd.getType()) {
+ case WMF:
+ case EMF:
+ break;
+ default:
+ assertNotNull(image);
+ assertEquals(dimensions[0], image.getWidth());
+ assertEquals(dimensions[1], image.getHeight());
+ break;
}
}
}
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.blip.*;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
/**
* Test adding/reading pictures
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("cow.pict");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PICT);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PICT);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.PICT, pictures[0].getType());
+ assertEquals(PictureType.PICT, pictures[0].getType());
assertTrue(pictures[0] instanceof PICT);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("santa.wmf");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.WMF);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.WMF);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.WMF, pictures[0].getType());
+ assertEquals(PictureType.WMF, pictures[0].getType());
assertTrue(pictures[0] instanceof WMF);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("wrench.emf");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.EMF);
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.EMF);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.EMF, pictures[0].getType());
+ assertEquals(PictureType.EMF, pictures[0].getType());
assertTrue(pictures[0] instanceof EMF);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("tomcat.png");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PNG);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PNG);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.PNG, pictures[0].getType());
+ assertEquals(PictureType.PNG, pictures[0].getType());
assertTrue(pictures[0] instanceof PNG);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("clock.jpg");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.JPEG);
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.JPEG);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.JPEG, pictures[0].getType());
+ assertEquals(PictureType.JPEG, pictures[0].getType());
assertTrue(pictures[0] instanceof JPEG);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("clock.dib");
- int idx = ppt.addPicture(src_bytes, HSLFPictureShape.DIB);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
- assertEquals(idx, pict.getPictureIndex());
+ HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.DIB);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
+ assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
- assertEquals(idx, pict.getPictureIndex());
+ assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
- assertEquals(HSLFPictureShape.DIB, pictures[0].getType());
+ assertEquals(PictureType.DIB, pictures[0].getType());
assertTrue(pictures[0] instanceof DIB);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
pict = (HSLFPictureShape)slides.get(0).getShapes().get(0); //the first slide contains JPEG
pdata = pict.getPictureData();
assertTrue(pdata instanceof JPEG);
- assertEquals(HSLFPictureShape.JPEG, pdata.getType());
+ assertEquals(PictureType.JPEG, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("clock.jpg");
assertArrayEquals(src_bytes, ppt_bytes);
pict = (HSLFPictureShape)slides.get(1).getShapes().get(0); //the second slide contains PNG
pdata = pict.getPictureData();
assertTrue(pdata instanceof PNG);
- assertEquals(HSLFPictureShape.PNG, pdata.getType());
+ assertEquals(PictureType.PNG, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("tomcat.png");
assertArrayEquals(src_bytes, ppt_bytes);
pict = (HSLFPictureShape)slides.get(2).getShapes().get(0); //the third slide contains WMF
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
- assertEquals(HSLFPictureShape.WMF, pdata.getType());
+ assertEquals(PictureType.WMF, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("santa.wmf");
assertEquals(src_bytes.length, ppt_bytes.length);
pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT
pdata = pict.getPictureData();
assertTrue(pdata instanceof PICT);
- assertEquals(HSLFPictureShape.PICT, pdata.getType());
+ assertEquals(PictureType.PICT, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("cow.pict");
assertEquals(src_bytes.length, ppt_bytes.length);
pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF
pdata = pict.getPictureData();
assertTrue(pdata instanceof EMF);
- assertEquals(HSLFPictureShape.EMF, pdata.getType());
+ assertEquals(PictureType.EMF, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("wrench.emf");
assertArrayEquals(src_bytes, ppt_bytes);
// Should still have 2 real pictures
assertEquals(2, hslf.getPictures().length);
// Both are real pictures, both WMF
- assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
- assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
+ assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
+ assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
// Now test what happens when we use the SlideShow interface
HSLFSlideShow ppt = new HSLFSlideShow(hslf);
pict = (HSLFPictureShape)slides.get(0).getShapes().get(1); // 2nd object on 1st slide
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
- assertEquals(HSLFPictureShape.WMF, pdata.getType());
+ assertEquals(PictureType.WMF, pdata.getType());
pict = (HSLFPictureShape)slides.get(0).getShapes().get(2); // 3rd object on 1st slide
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
- assertEquals(HSLFPictureShape.WMF, pdata.getType());
+ assertEquals(PictureType.WMF, pdata.getType());
}
/**
// Should still have 2 real pictures
assertEquals(2, hslf.getPictures().length);
// Both are real pictures, both WMF
- assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
- assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
+ assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
+ assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
// Now test what happens when we use the SlideShow interface
HSLFSlideShow ppt = new HSLFSlideShow(hslf);
pict = (HSLFPictureShape)slides.get(6).getShapes().get(13);
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
- assertEquals(HSLFPictureShape.WMF, pdata.getType());
+ assertEquals(PictureType.WMF, pdata.getType());
pict = (HSLFPictureShape)slides.get(7).getShapes().get(13);
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
- assertEquals(HSLFPictureShape.WMF, pdata.getType());
+ assertEquals(PictureType.WMF, pdata.getType());
//add a new picture, it should be correctly appended to the Pictures stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
int streamSize = out.size();
- HSLFPictureData data = HSLFPictureData.create(HSLFPictureShape.JPEG);
+ HSLFPictureData data = HSLFPictureData.create(PictureType.JPEG);
data.setData(new byte[100]);
int offset = hslf.addPicture(data);
assertEquals(streamSize, offset);
HSLFSlide slide = ppt.createSlide();
byte[] img = slTests.readFile("tomcat.png");
- int idx = ppt.addPicture(img, HSLFPictureShape.PNG);
- HSLFPictureShape pict = new HSLFPictureShape(idx);
+ HSLFPictureData data = ppt.addPicture(img, PictureType.PNG);
+ HSLFPictureShape pict = new HSLFPictureShape(data);
pict.setPictureName("tomcat.png");
slide.addShape(pict);