int idx=slideShow.addPicture(new File(filename), org.apache.poi.hslf.model.Picture.JPEG); org.apache.poi.hslf.model.Picture pict = new org.apache.poi.hslf.model.Picture(idx);
/** * Create a new <code>Picture</code> * * @param idx the index of the picture * @param parent the parent shape */ public Picture(int idx, Shape parent) { super(null, parent); _escherContainer = createSpContainer(idx, parent instanceof ShapeGroup); }
/** * By default set the orininal image size */ protected void afterInsert(Sheet sh){ super.afterInsert(sh); EscherBSERecord bse = getEscherBSERecord(); bse.setRef(bse.getRef() + 1); java.awt.Rectangle anchor = getAnchor(); if (anchor.equals(new java.awt.Rectangle())){ setDefaultSize(); } }
/** * Returns index associated with this picture. * Index starts with 1 and points to a EscherBSE record which * holds information about this picture. * * @return the index to this picture (1 based). */ public int getPictureIndex(){ EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY); return prop == null ? 0 : prop.getPropertyValue(); }
/** * Resize this picture to the default size. * For PNG and JPEG resizes the image to 100%, * for other types sets the default size of 200x200 pixels. */ public void setDefaultSize(){ PictureData pict = getPictureData(); if (pict instanceof Bitmap){ BufferedImage img = null; try { img = ImageIO.read(new ByteArrayInputStream(pict.getData())); } catch (IOException e){} catch (NegativeArraySizeException ne) {} if(img != null) { // Valid image, set anchor from it setAnchor(new java.awt.Rectangle(0, 0, img.getWidth()*POINT_DPI/PIXEL_DPI, img.getHeight()*POINT_DPI/PIXEL_DPI)); } else { // Invalid image, go with the default metafile size setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); } } else { //default size of a metafile picture is 200x200 setAnchor(new java.awt.Rectangle(50, 50, 200, 200)); } }
/** * Create a new Picture and populate the inital structure of the <code>EscherSp</code> record which holds information about this picture. * @param idx the index of the picture which referes to <code>EscherBSE</code> container. * @return the create Picture object */ protected EscherContainerRecord createSpContainer(int idx, boolean isChild) { _escherContainer = super.createSpContainer(isChild); _escherContainer.setOptions((short)15); EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID); spRecord.setOptions((short)((ShapeTypes.PictureFrame << 4) | 0x2)); //set default properties for a picture EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x800080); //another weird feature of powerpoint: for picture id we must add 0x4000. setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx); return _escherContainer; }
protected EscherBSERecord getEscherBSERecord(){ SlideShow ppt = getSheet().getSlideShow(); Document doc = ppt.getDocumentRecord(); EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer(); EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER); if(bstore == null) { logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found "); return null; } List lst = bstore.getChildRecords(); int idx = getPictureIndex(); if (idx == 0){ logger.log(POILogger.DEBUG, "picture index was not found, returning "); return null; } return (EscherBSERecord)lst.get(idx-1); }
/** * Returns the picture data for this picture. * * @return the picture data for this picture. */ public PictureData getPictureData(){ SlideShow ppt = getSheet().getSlideShow(); PictureData[] pict = ppt.getPictureData(); EscherBSERecord bse = getEscherBSERecord(); if (bse == null){ logger.log(POILogger.ERROR, "no reference to picture data found "); } else { for ( int i = 0; i < pict.length; i++ ) { if (pict[i].getOffset() == bse.getOffset()){ return pict[i]; } } logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset()); } return null; }
public void draw(Graphics2D graphics){ AffineTransform at = graphics.getTransform(); ShapePainter.paint(this, graphics); PictureData data = getPictureData(); if(data != null) data.draw(graphics, this); graphics.setTransform(at); } }
public void paint(Graphics2D graphics, PictureData pict, Picture parent) { BufferedImage img; try { img = ImageIO.read(new ByteArrayInputStream(pict.getData())); } catch (Exception e){ logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType()); return; } Rectangle anchor = parent.getLogicalAnchor2D().getBounds(); graphics.drawImage(img, anchor.x, anchor.y, anchor.width, anchor.height, null); }
/** * Name of this picture. * * @param name of this picture */ public void setPictureName(String name){ EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); try { byte[] data = (name + '\u0000').getBytes("UTF-16LE"); EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data); opt.addEscherProperty(prop); } catch (UnsupportedEncodingException e){ throw new HSLFException(e); } }
/** * Name of this picture. * * @return name of this picture */ public String getPictureName(){ EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherComplexProperty prop = (EscherComplexProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME); String name = null; if(prop != null){ try { name = new String(prop.getComplexData(), "UTF-16LE"); int idx = name.indexOf('\u0000'); return idx == -1 ? name : name.substring(0, idx); } catch (UnsupportedEncodingException e){ throw new HSLFException(e); } } return name; }
if(shape == null) shape = new Picture(spContainer, parent); break;
/** * Create a new Placeholder and initialize internal structures * * @return the created <code>EscherContainerRecord</code> which holds shape data */ protected EscherContainerRecord createSpContainer(int idx, boolean isChild) { _escherContainer = super.createSpContainer(idx, isChild); EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID); spRecord.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE | EscherSpRecord.FLAG_OLESHAPE); setShapeType(ShapeTypes.HostControl); setEscherProperty(EscherProperties.BLIP__PICTUREID, idx); setEscherProperty(EscherProperties.LINESTYLE__COLOR, 0x8000001); setEscherProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80008); setEscherProperty(EscherProperties.SHADOWSTYLE__COLOR, 0x8000002); setEscherProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, -1); EscherClientDataRecord cldata = new EscherClientDataRecord(); cldata.setOptions((short)0xF); _escherContainer.addChildRecord(cldata); // TODO unit test to prove getChildRecords().add is wrong OEShapeAtom oe = new OEShapeAtom(); //convert hslf into ddf ByteArrayOutputStream out = new ByteArrayOutputStream(); try { oe.writeOut(out); } catch(Exception e){ throw new HSLFException(e); } cldata.setRemainingData(out.toByteArray()); return _escherContainer; }
_escherContainer = super.createSpContainer(idx, isChild);