From c6046f22637229fe64bedae0513dbf67af002298 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Wed, 5 Sep 2018 01:00:45 +0000 Subject: [PATCH] import existing chart on current drawing git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1840076 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/XSSFDrawing.java | 388 ++++++++++-------- 1 file changed, 216 insertions(+), 172 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index 078a2f7442..3f42faab5f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -101,40 +101,44 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawingapplication/vnd.openxmlformats-officedocument.drawing+xml + * @param part + * the package part holding the drawing data, the content type + * must be + * application/vnd.openxmlformats-officedocument.drawing+xml * * @since POI 3.14-Beta1 */ public XSSFDrawing(PackagePart part) throws IOException, XmlException { super(part); - XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS); - //Removing root element + XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS); + // Removing root element options.setLoadReplaceDocumentElement(null); InputStream is = part.getInputStream(); try { - drawing = CTDrawing.Factory.parse(is,options); + drawing = CTDrawing.Factory.parse(is, options); } finally { is.close(); } } /** - * Construct a new CTDrawing bean. By default, it's just an empty placeholder for drawing objects + * Construct a new CTDrawing bean. By default, it's just an empty + * placeholder for drawing objects * * @return a new CTDrawing bean */ - private static CTDrawing newDrawing(){ + private static CTDrawing newDrawing() { return CTDrawing.Factory.newInstance(); } /** - * Return the underlying CTDrawing bean, the root element of the SpreadsheetML Drawing part. + * Return the underlying CTDrawing bean, the root element of the + * SpreadsheetML Drawing part. * * @return the underlying CTDrawing bean */ @Internal - public CTDrawing getCTDrawing(){ + public CTDrawing getCTDrawing() { return drawing; } @@ -143,14 +147,13 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing - */ - xmlOptions.setSaveSyntheticDocumentElement( - new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr") - ); + * Saved drawings must have the following namespaces set: + */ + xmlOptions + .setSaveSyntheticDocumentElement(new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr")); PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); @@ -158,20 +161,20 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements DrawingsrcChart 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; + } /** * Add the indexed picture to this drawing relations * - * @param pictureIndex the index of the picture in the workbook collection of pictures, - * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} . + * @param pictureIndex + * the index of the picture in the workbook collection of + * pictures, + * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} + * . */ @SuppressWarnings("resource") - protected PackageRelationship addPictureReference(int pictureIndex){ - XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent(); + protected PackageRelationship addPictureReference(int pictureIndex) { + XSSFWorkbook wb = (XSSFWorkbook) getParent().getParent(); XSSFPictureData data = wb.getAllPictures().get(pictureIndex); XSSFPictureData pic = new XSSFPictureData(data.getPackagePart()); RelationPart rp = addRelation(null, XSSFRelation.IMAGES, pic); @@ -265,15 +300,15 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing getCharts() { - List charts = new ArrayList<>(); - for(POIXMLDocumentPart part : getRelations()) { - if(part instanceof XSSFChart) { - charts.add((XSSFChart)part); - } - } - return charts; + List charts = new ArrayList<>(); + for (POIXMLDocumentPart part : getRelations()) { + if (part instanceof XSSFChart) { + charts.add((XSSFChart) part); + } + } + return charts; } /** - * Create and initialize a CTTwoCellAnchor that anchors a shape against top-left and bottom-right cells. + * Create and initialize a CTTwoCellAnchor that anchors a shape against + * top-left and bottom-right cells. * * @return a new CTTwoCellAnchor */ @@ -508,14 +548,21 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing getShapes(){ + public List getShapes() { List lst = new ArrayList<>(); XmlCursor cur = drawing.newCursor(); try { @@ -569,7 +614,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing getShapes(XSSFShapeGroup groupshape){ + public List getShapes(XSSFShapeGroup groupshape) { List lst = new ArrayList<>(); XmlCursor cur = groupshape.getCTGroupShape().newCursor(); try { @@ -593,29 +638,28 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing