Search in sources :

Example 1 with CTMarker

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker in project poi by apache.

the class XSSFDrawing method addShapes.

private void addShapes(XmlCursor cur, List<XSSFShape> lst) {
    try {
        do {
            cur.push();
            if (cur.toFirstChild()) {
                do {
                    XmlObject obj = cur.getObject();
                    XSSFShape shape;
                    if (obj instanceof CTMarker) {
                        // ignore anchor elements
                        continue;
                    } else if (obj instanceof CTPicture) {
                        shape = new XSSFPicture(this, (CTPicture) obj);
                    } else if (obj instanceof CTConnector) {
                        shape = new XSSFConnector(this, (CTConnector) obj);
                    } else if (obj instanceof CTShape) {
                        shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj) : new XSSFSimpleShape(this, (CTShape) obj);
                    } else if (obj instanceof CTGraphicalObjectFrame) {
                        shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
                    } else if (obj instanceof CTGroupShape) {
                        shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
                    } else if (obj instanceof XmlAnyTypeImpl) {
                        LOG.log(POILogger.WARN, "trying to parse AlternateContent, " + "this unlinks the returned Shapes from the underlying xml content, " + "so those shapes can't be used to modify the drawing, " + "i.e. modifications will be ignored!");
                        // XmlAnyTypeImpl is returned for AlternateContent parts, which might contain a CTDrawing
                        cur.push();
                        cur.toFirstChild();
                        XmlCursor cur2 = null;
                        try {
                            // need to parse AlternateContent again, otherwise the child elements aren't typed,
                            // but also XmlAnyTypes
                            CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
                            cur2 = alterWS.newCursor();
                            if (cur2.toFirstChild()) {
                                addShapes(cur2, lst);
                            }
                        } catch (XmlException e) {
                            LOG.log(POILogger.WARN, "unable to parse CTDrawing in alternate content.", e);
                        } finally {
                            if (cur2 != null) {
                                cur2.dispose();
                            }
                            cur.pop();
                        }
                        continue;
                    } else {
                        // ignore anything else
                        continue;
                    }
                    assert (shape != null);
                    shape.anchor = getAnchorFromParent(obj);
                    lst.add(shape);
                } while (cur.toNextSibling());
            }
            cur.pop();
        } while (cur.toNextSibling());
    } finally {
        cur.dispose();
    }
}
Also used : CTMarker(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker) CTConnector(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector) CTGraphicalObjectFrame(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame) CTDrawing(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) CTGroupShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject)

Example 2 with CTMarker

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker in project hutool by looly.

the class ExcelPicUtil method getPicMapXlsx.

/**
 * 获取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<>();
    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) {
                if (shape instanceof XSSFPicture) {
                    pic = (XSSFPicture) shape;
                    ctMarker = pic.getPreferredSize().getFrom();
                    sheetIndexPicMap.put(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData());
                }
            // 其他类似于图表等忽略,see: https://gitee.com/loolly/hutool/issues/I38857
            }
        }
    }
    return sheetIndexPicMap;
}
Also used : CTMarker(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker) XSSFShape(org.apache.poi.xssf.usermodel.XSSFShape) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) HashMap(java.util.HashMap) POIXMLDocumentPart(org.apache.poi.ooxml.POIXMLDocumentPart) XSSFPicture(org.apache.poi.xssf.usermodel.XSSFPicture) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) HSSFPictureData(org.apache.poi.hssf.usermodel.HSSFPictureData) PictureData(org.apache.poi.ss.usermodel.PictureData)

Aggregations

CTMarker (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker)2 HashMap (java.util.HashMap)1 HSSFPictureData (org.apache.poi.hssf.usermodel.HSSFPictureData)1 POIXMLDocumentPart (org.apache.poi.ooxml.POIXMLDocumentPart)1 PictureData (org.apache.poi.ss.usermodel.PictureData)1 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)1 XSSFPicture (org.apache.poi.xssf.usermodel.XSSFPicture)1 XSSFShape (org.apache.poi.xssf.usermodel.XSSFShape)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 XmlObject (org.apache.xmlbeans.XmlObject)1 XmlAnyTypeImpl (org.apache.xmlbeans.impl.values.XmlAnyTypeImpl)1 CTConnector (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector)1 CTDrawing (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing)1 CTGraphicalObjectFrame (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame)1 CTGroupShape (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape)1 CTPicture (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture)1 CTShape (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape)1