/** * Return the dimension of the embedded image in pixel * * @return image dimension in pixels */ public Dimension getImageDimension() { XSSFPictureData picData = getPictureData(); return getImageDimension(picData.getPackagePart(), picData.getPictureType()); }
/** * Calculate the preferred size for this picture. * * @return XSSFClientAnchor with the preferred size for this image */ public XSSFClientAnchor getPreferredSize(){ return getPreferredSize(1); }
/** * Creates a picture. * * @param anchor the client anchor describes how this picture is attached to the sheet. * @param pictureIndex the index of the picture in the workbook collection of pictures, * {@link XSSFWorkbook#getAllPictures()} . * @return the newly created picture shape. */ public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) { PackageRelationship rel = getDrawing().addPictureReference(pictureIndex); CTPicture ctShape = ctGroup.addNewPic(); ctShape.set(XSSFPicture.prototype()); XSSFPicture shape = new XSSFPicture(getDrawing(), ctShape); shape.parent = this; shape.anchor = anchor; shape.setPictureReference(rel); return shape; }
/** * 获取XLSX工作簿指定sheet中图片列表 * * @param workbook 工作簿{@link Workbook} * @param sheetIndex sheet的索引 * @return 图片映射,键格式:行_列,值:{@link PictureData} */ private static Map<String, PictureData> getPicMapXlsx(XSSFWorkbook workbook, int sheetIndex) { final Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); final XSSFSheet sheet = workbook.getSheetAt(sheetIndex); XSSFDrawing drawing; for (POIXMLDocumentPart dr : sheet.getRelations()) { if (dr instanceof XSSFDrawing) { drawing = (XSSFDrawing) dr; final List<XSSFShape> shapes = drawing.getShapes(); XSSFPicture pic; CTMarker ctMarker; for (XSSFShape shape : shapes) { pic = (XSSFPicture) shape; ctMarker = pic.getPreferredSize().getFrom(); sheetIndexPicMap.put(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData()); } } } return sheetIndexPicMap; } // -------------------------------------------------------------------------------------------------------------- Private method end
XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor(); XSSFPictureData data = getPictureData(); Dimension size = getImageDimension(data.getPackagePart(), data.getPictureType()); double scaledWidth = size.getWidth() * scale; double scaledHeight = size.getHeight() * scale; w += getColumnWidthInPixels(col2); if(w > scaledWidth) break; col2++; double cw = getColumnWidthInPixels(col2 ); double delta = w - scaledWidth; dx2 = (int)(EMU_PER_PIXEL*(cw-delta)); h += getRowHeightInPixels(row2); if(h > scaledHeight) break; row2++; double ch = getRowHeightInPixels(row2); double delta = h - scaledHeight; dy2 = (int)(EMU_PER_PIXEL*(ch-delta));
XSSFClientAnchor anchor = getClientAnchor(); XSSFClientAnchor pref = getPreferredSize(scaleX,scaleY); if (anchor == null || pref == null) { logger.log(POILogger.WARN, "picture is not anchored via client anchor - ignoring resize call");
/** * Reset the image to the original size. * <p> * Please note, that this method works correctly only for workbooks * with the default font size (Calibri 11pt for .xlsx). * If the default font is changed the resized image can be streched vertically or horizontally. * </p> * * @param scale the amount by which image dimensions are multiplied relative to the original size. * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original, * <code>resize(2.0)</code> resizes to 200% of the original. */ public void resize(double scale){ XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor(); XSSFClientAnchor pref = getPreferredSize(scale); int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1()); int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1()); anchor.setCol2(col2); anchor.setDx1(0); anchor.setDx2(pref.getDx2()); anchor.setRow2(row2); anchor.setDy1(0); anchor.setDy2(pref.getDy2()); }
/** * Return picture data for this shape * * @return picture data for this shape */ @Override public XSSFPictureData getPictureData() { return _picture.getPictureData(); }
public XSSFDrawing getDrawing() { return _picture.getDrawing(); }
/** * Resize the image proportionally. * * @see #resize(double, double) */ public void resize(double scale) { resize(scale, scale); }
/** * @return the anchor that is used by this shape. */ @Override public XSSFClientAnchor getClientAnchor() { XSSFAnchor a = getAnchor(); return (a instanceof XSSFClientAnchor) ? (XSSFClientAnchor)a : null; }
/** * Calculate the preferred size for this picture. * * @param scaleX the amount by which image width is multiplied relative to the original width. * @param scaleY the amount by which image height is multiplied relative to the original height. * @return XSSFClientAnchor with the preferred size for this image */ public XSSFClientAnchor getPreferredSize(double scaleX, double scaleY){ Dimension dim = ImageUtils.setPreferredSize(this, scaleX, scaleY); CTPositiveSize2D size2d = ctPicture.getSpPr().getXfrm().getExt(); size2d.setCx((int)dim.getWidth()); size2d.setCy((int)dim.getHeight()); return getClientAnchor(); }
shape = new XSSFPicture(this, (CTPicture) obj); } else if (obj instanceof CTConnector) { shape = new XSSFConnector(this, (CTConnector) obj);
@Override public Dimension getImageDimension() { return _picture.getImageDimension(); }
/** * 获取XLSX工作簿指定sheet中图片列表 * * @param workbook 工作簿{@link Workbook} * @param sheetIndex sheet的索引 * @return 图片映射,键格式:行_列,值:{@link PictureData} */ private static Map<String, PictureData> getPicMapXlsx(XSSFWorkbook workbook, int sheetIndex) { final Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); final XSSFSheet sheet = workbook.getSheetAt(sheetIndex); XSSFDrawing drawing; for (POIXMLDocumentPart dr : sheet.getRelations()) { if (dr instanceof XSSFDrawing) { drawing = (XSSFDrawing) dr; final List<XSSFShape> shapes = drawing.getShapes(); XSSFPicture pic; CTMarker ctMarker; for (XSSFShape shape : shapes) { pic = (XSSFPicture) shape; ctMarker = pic.getPreferredSize().getFrom(); sheetIndexPicMap.put(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData()); } } } return sheetIndexPicMap; } // -------------------------------------------------------------------------------------------------------------- Private method end
XSSFClientAnchor anchor = getClientAnchor(); XSSFClientAnchor pref = getPreferredSize(scaleX,scaleY); if (anchor == null || pref == null) { logger.log(POILogger.WARN, "picture is not anchored via client anchor - ignoring resize call");
/** * Return picture data for this shape * * @return picture data for this shape */ @Override public XSSFPictureData getPictureData() { return _picture.getPictureData(); }
/** * @return the sheet which contains the picture shape */ @Override public XSSFSheet getSheet() { return (XSSFSheet)getDrawing().getParent(); }
/** * Reset the image to the dimension of the embedded image * * @see #resize(double, double) */ public void resize(){ resize(Double.MAX_VALUE); }
@Override public XSSFAnchor getAnchor() { return _picture.getAnchor(); }