@Override public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) { return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); }
/** * Create and initialize a CTTwoCellAnchor that anchors a shape against * top-left and bottom-right cells. * * @return a new CTTwoCellAnchor */ private CTTwoCellAnchor createTwoCellAnchor(XSSFClientAnchor anchor) { CTTwoCellAnchor ctAnchor = drawing.addNewTwoCellAnchor(); ctAnchor.setFrom(anchor.getFrom()); ctAnchor.setTo(anchor.getTo()); ctAnchor.addNewClientData(); anchor.setTo(ctAnchor.getTo()); anchor.setFrom(ctAnchor.getFrom()); STEditAs.Enum editAs; switch (anchor.getAnchorType()) { case DONT_MOVE_AND_RESIZE: editAs = STEditAs.ABSOLUTE; break; case MOVE_AND_RESIZE: editAs = STEditAs.TWO_CELL; break; case MOVE_DONT_RESIZE: editAs = STEditAs.ONE_CELL; break; default: editAs = STEditAs.ONE_CELL; } ctAnchor.setEditAs(editAs); return ctAnchor; }
/** * 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. */ @Override public void resize(double scale){ XSSFClientAnchor anchor = getClientAnchor(); XSSFClientAnchor pref = getPreferredSize(scale); if (anchor == null || pref == null) { logger.log(POILogger.WARN, "picture is not anchored via client anchor - ignoring resize call"); return; } 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()); }
@Override public boolean equals(Object o) { if (o == null || !(o instanceof XSSFClientAnchor)) return false; XSSFClientAnchor anchor = (XSSFClientAnchor) o; return getDx1() == anchor.getDx1() && getDx2() == anchor.getDx2() && getDy1() == anchor.getDy1() && getDy2() == anchor.getDy2() && getCol1() == anchor.getCol1() && getCol2() == anchor.getCol2() && getRow1() == anchor.getRow1() && getRow2() == anchor.getRow2() ; }
int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(0 * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(0 * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{ int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(0 * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(0 * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{ int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i);
/** * 获取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
/** * Creates a comment. * @param anchor the client anchor describes how this comment is attached * to the sheet. * @return the newly created comment. */ public XSSFComment createCellComment(ClientAnchor anchor) { XSSFClientAnchor ca = (XSSFClientAnchor)anchor; XSSFSheet sheet = (XSSFSheet)getParent(); //create comments and vmlDrawing parts if they don't exist CommentsTable comments = sheet.getCommentsTable(true); XSSFVMLDrawing vml = sheet.getVMLDrawing(true); schemasMicrosoftComVml.CTShape vmlShape = vml.newCommentShape(); if(ca.isSet()){ String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0"; vmlShape.getClientDataArray(0).setAnchorArray(0, position); } XSSFComment shape = new XSSFComment(comments, comments.newComment(), vmlShape); shape.setColumn(ca.getCol1()); shape.setRow(ca.getRow1()); return shape; }
/** * Imports the chart from the <code>srcChart</code> into this drawing. * * @param srcChart * the source chart to be cloned into this drawing. * @return the newly created chart. * @throws XmlException * @throws IOException * @since 4.0.0 */ public XSSFChart importChart(XSSFChart srcChart) throws IOException, XmlException { CTTwoCellAnchor anchor = ((XSSFDrawing) srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0); CTMarker from = (CTMarker) anchor.getFrom().copy(); CTMarker to = (CTMarker) anchor.getTo().copy(); XSSFClientAnchor destAnchor = new XSSFClientAnchor(from, to); destAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); XSSFChart destChart = createChart(destAnchor); destChart.getCTChartSpace().set(srcChart.getCTChartSpace().copy()); destChart.getCTChart().set(srcChart.getCTChart().copy()); return destChart; }
private CTTransform2D createXfrm(XSSFClientAnchor anchor) { CTTransform2D xfrm = CTTransform2D.Factory.newInstance(); CTPoint2D off = xfrm.addNewOff(); off.setX(anchor.getDx1()); off.setY(anchor.getDy1()); XSSFSheet sheet = getSheet(); double widthPx = 0; for (int col = anchor.getCol1(); col < anchor.getCol2(); col++) { widthPx += sheet.getColumnWidthInPixels(col); } double heightPx = 0; for (int row = anchor.getRow1(); row < anchor.getRow2(); row++) { heightPx += ImageUtils.getRowHeightInPixels(sheet, row); } long width = Units.pixelToEMU((int) widthPx); long height = Units.pixelToEMU((int) heightPx); CTPositiveSize2D ext = xfrm.addNewExt(); ext.setCx(width - anchor.getDx1() + anchor.getDx2()); ext.setCy(height - anchor.getDy1() + anchor.getDy2()); // TODO: handle vflip/hflip return xfrm; }
int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(leftMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(topMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{ int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(leftMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(topMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{
/** * 获取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
/** * Imports the chart from the <code>srcChart</code> into this drawing. * * @param srcChart * the source chart to be cloned into this drawing. * @return the newly created chart. * @throws XmlException * @throws IOException * @since 4.0.0 */ public XSSFChart importChart(XSSFChart srcChart) throws IOException, XmlException { CTTwoCellAnchor anchor = ((XSSFDrawing) srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0); CTMarker from = (CTMarker) anchor.getFrom().copy(); CTMarker to = (CTMarker) anchor.getTo().copy(); XSSFClientAnchor destAnchor = new XSSFClientAnchor(from, to); destAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); XSSFChart destChart = createChart(destAnchor); destChart.getCTChartSpace().set(srcChart.getCTChartSpace().copy()); destChart.getCTChart().set(srcChart.getCTChart().copy()); return destChart; }
XSSFVMLDrawing vml = sheet.getVMLDrawing(true); com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape(); if (ca.isSet()) { int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL; int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL; int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL; int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL; String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca .getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels; vmlShape.getClientDataArray(0).setAnchorArray(0, position); CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(leftMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(topMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{ int pictureFormat=buildImageFormat(img); int pictureIndex=wb.addPicture(bytes, pictureFormat); anchor.setCol1(i); anchor.setCol2(i+colSpan); anchor.setRow1(rowNumber); anchor.setRow2(rowNumber+rowSpan); anchor.setDx1(leftMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDx2(width * XSSFShape.EMU_PER_PIXEL); anchor.setDy1(topMargin * XSSFShape.EMU_PER_PIXEL); anchor.setDy2(height * XSSFShape.EMU_PER_PIXEL); drawing.createPicture(anchor, pictureIndex); }finally{
/** * Creates a XSSFClientAnchor. Use this object to position drawing object in * a sheet * * @return a XSSFClientAnchor instance * @see org.apache.poi.ss.usermodel.Drawing */ @Override public XSSFClientAnchor createClientAnchor() { return new XSSFClientAnchor(); }