import existing chart on current drawing
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1840076 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a8d1307025
commit
5abdfcb0a6
@ -101,40 +101,44 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
/**
|
/**
|
||||||
* Construct a SpreadsheetML drawing from a package part
|
* Construct a SpreadsheetML drawing from a package part
|
||||||
*
|
*
|
||||||
* @param part the package part holding the drawing data,
|
* @param part
|
||||||
* the content type must be <code>application/vnd.openxmlformats-officedocument.drawing+xml</code>
|
* the package part holding the drawing data, the content type
|
||||||
|
* must be
|
||||||
|
* <code>application/vnd.openxmlformats-officedocument.drawing+xml</code>
|
||||||
*
|
*
|
||||||
* @since POI 3.14-Beta1
|
* @since POI 3.14-Beta1
|
||||||
*/
|
*/
|
||||||
public XSSFDrawing(PackagePart part) throws IOException, XmlException {
|
public XSSFDrawing(PackagePart part) throws IOException, XmlException {
|
||||||
super(part);
|
super(part);
|
||||||
XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
|
XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
|
||||||
//Removing root element
|
// Removing root element
|
||||||
options.setLoadReplaceDocumentElement(null);
|
options.setLoadReplaceDocumentElement(null);
|
||||||
InputStream is = part.getInputStream();
|
InputStream is = part.getInputStream();
|
||||||
try {
|
try {
|
||||||
drawing = CTDrawing.Factory.parse(is,options);
|
drawing = CTDrawing.Factory.parse(is, options);
|
||||||
} finally {
|
} finally {
|
||||||
is.close();
|
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
|
* @return a new CTDrawing bean
|
||||||
*/
|
*/
|
||||||
private static CTDrawing newDrawing(){
|
private static CTDrawing newDrawing() {
|
||||||
return CTDrawing.Factory.newInstance();
|
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
|
* @return the underlying CTDrawing bean
|
||||||
*/
|
*/
|
||||||
@Internal
|
@Internal
|
||||||
public CTDrawing getCTDrawing(){
|
public CTDrawing getCTDrawing() {
|
||||||
return drawing;
|
return drawing;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,14 +147,13 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
|
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Saved drawings must have the following namespaces set:
|
* Saved drawings must have the following namespaces set: <xdr:wsDr
|
||||||
<xdr:wsDr
|
* xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
|
||||||
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
|
* xmlns:xdr=
|
||||||
xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
|
* "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
|
||||||
*/
|
*/
|
||||||
xmlOptions.setSaveSyntheticDocumentElement(
|
xmlOptions
|
||||||
new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr")
|
.setSaveSyntheticDocumentElement(new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr"));
|
||||||
);
|
|
||||||
|
|
||||||
PackagePart part = getPackagePart();
|
PackagePart part = getPackagePart();
|
||||||
OutputStream out = part.getOutputStream();
|
OutputStream out = part.getOutputStream();
|
||||||
@ -158,20 +161,20 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2,
|
public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) {
|
||||||
int col1, int row1, int col2, int row2) {
|
return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
|
||||||
return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a textbox under the drawing.
|
* Constructs a textbox under the drawing.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this group is attached
|
* @param anchor
|
||||||
* to the sheet.
|
* the client anchor describes how this group is attached to the
|
||||||
* @return the newly created textbox.
|
* sheet.
|
||||||
|
* @return the newly created textbox.
|
||||||
*/
|
*/
|
||||||
public XSSFTextBox createTextbox(XSSFClientAnchor anchor){
|
public XSSFTextBox createTextbox(XSSFClientAnchor anchor) {
|
||||||
long shapeId = newShapeId();
|
long shapeId = newShapeId();
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
||||||
CTShape ctShape = ctAnchor.addNewSp();
|
CTShape ctShape = ctAnchor.addNewSp();
|
||||||
@ -186,14 +189,18 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
/**
|
/**
|
||||||
* Creates a picture.
|
* Creates a picture.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this picture is attached to the sheet.
|
* @param anchor
|
||||||
* @param pictureIndex the index of the picture in the workbook collection of pictures,
|
* the client anchor describes how this picture is attached to
|
||||||
* {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
|
* the sheet.
|
||||||
|
* @param pictureIndex
|
||||||
|
* the index of the picture in the workbook collection of
|
||||||
|
* pictures,
|
||||||
|
* {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()}
|
||||||
|
* .
|
||||||
*
|
*
|
||||||
* @return the newly created picture shape.
|
* @return the newly created picture shape.
|
||||||
*/
|
*/
|
||||||
public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex)
|
public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) {
|
||||||
{
|
|
||||||
PackageRelationship rel = addPictureReference(pictureIndex);
|
PackageRelationship rel = addPictureReference(pictureIndex);
|
||||||
|
|
||||||
long shapeId = newShapeId();
|
long shapeId = newShapeId();
|
||||||
@ -212,23 +219,24 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public XSSFPicture createPicture(ClientAnchor anchor, int pictureIndex){
|
public XSSFPicture createPicture(ClientAnchor anchor, int pictureIndex) {
|
||||||
return createPicture((XSSFClientAnchor)anchor, pictureIndex);
|
return createPicture((XSSFClientAnchor) anchor, pictureIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a chart.
|
* Creates a chart.
|
||||||
* @param anchor the client anchor describes how this chart is attached to
|
*
|
||||||
* the sheet.
|
* @param anchor
|
||||||
* @return the newly created chart
|
* the client anchor describes how this chart is attached to the
|
||||||
* @see org.apache.poi.xssf.usermodel.XSSFDrawing#createChart(ClientAnchor)
|
* sheet.
|
||||||
*/
|
* @return the newly created chart
|
||||||
|
* @see org.apache.poi.xssf.usermodel.XSSFDrawing#createChart(ClientAnchor)
|
||||||
|
*/
|
||||||
public XSSFChart createChart(XSSFClientAnchor anchor) {
|
public XSSFChart createChart(XSSFClientAnchor anchor) {
|
||||||
int chartNumber = getPackagePart().getPackage().
|
int chartNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.CHART.getContentType())
|
||||||
getPartsByContentType(XSSFRelation.CHART.getContentType()).size() + 1;
|
.size() + 1;
|
||||||
|
|
||||||
RelationPart rp = createRelationship(
|
RelationPart rp = createRelationship(XSSFRelation.CHART, XSSFFactory.getInstance(), chartNumber, false);
|
||||||
XSSFRelation.CHART, XSSFFactory.getInstance(), chartNumber, false);
|
|
||||||
XSSFChart chart = rp.getDocumentPart();
|
XSSFChart chart = rp.getDocumentPart();
|
||||||
String chartRelId = rp.getRelationship().getId();
|
String chartRelId = rp.getRelationship().getId();
|
||||||
|
|
||||||
@ -239,25 +247,52 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a chart.
|
* Creates a chart.
|
||||||
* @param anchor the client anchor describes how this chart is attached to
|
*
|
||||||
* the sheet.
|
* @param anchor
|
||||||
* @return the newly created chart
|
* the client anchor describes how this chart is attached to the
|
||||||
*/
|
* sheet.
|
||||||
|
* @return the newly created chart
|
||||||
|
*/
|
||||||
public XSSFChart createChart(ClientAnchor anchor) {
|
public XSSFChart createChart(ClientAnchor anchor) {
|
||||||
return createChart((XSSFClientAnchor)anchor);
|
return createChart((XSSFClientAnchor) anchor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the indexed picture to this drawing relations
|
* Add the indexed picture to this drawing relations
|
||||||
*
|
*
|
||||||
* @param pictureIndex the index of the picture in the workbook collection of pictures,
|
* @param pictureIndex
|
||||||
* {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
|
* the index of the picture in the workbook collection of
|
||||||
|
* pictures,
|
||||||
|
* {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()}
|
||||||
|
* .
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
protected PackageRelationship addPictureReference(int pictureIndex){
|
protected PackageRelationship addPictureReference(int pictureIndex) {
|
||||||
XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent();
|
XSSFWorkbook wb = (XSSFWorkbook) getParent().getParent();
|
||||||
XSSFPictureData data = wb.getAllPictures().get(pictureIndex);
|
XSSFPictureData data = wb.getAllPictures().get(pictureIndex);
|
||||||
XSSFPictureData pic = new XSSFPictureData(data.getPackagePart());
|
XSSFPictureData pic = new XSSFPictureData(data.getPackagePart());
|
||||||
RelationPart rp = addRelation(null, XSSFRelation.IMAGES, pic);
|
RelationPart rp = addRelation(null, XSSFRelation.IMAGES, pic);
|
||||||
@ -265,15 +300,15 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
||||||
* and ovals.
|
* and ovals.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this group is attached
|
* @param anchor
|
||||||
* to the sheet.
|
* the client anchor describes how this group is attached to the
|
||||||
* @return the newly created shape.
|
* sheet.
|
||||||
|
* @return the newly created shape.
|
||||||
*/
|
*/
|
||||||
public XSSFSimpleShape createSimpleShape(XSSFClientAnchor anchor)
|
public XSSFSimpleShape createSimpleShape(XSSFClientAnchor anchor) {
|
||||||
{
|
|
||||||
long shapeId = newShapeId();
|
long shapeId = newShapeId();
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
||||||
CTShape ctShape = ctAnchor.addNewSp();
|
CTShape ctShape = ctAnchor.addNewSp();
|
||||||
@ -286,15 +321,15 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
||||||
* and ovals.
|
* and ovals.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this group is attached
|
* @param anchor
|
||||||
* to the sheet.
|
* the client anchor describes how this group is attached to the
|
||||||
* @return the newly created shape.
|
* sheet.
|
||||||
|
* @return the newly created shape.
|
||||||
*/
|
*/
|
||||||
public XSSFConnector createConnector(XSSFClientAnchor anchor)
|
public XSSFConnector createConnector(XSSFClientAnchor anchor) {
|
||||||
{
|
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
||||||
CTConnector ctShape = ctAnchor.addNewCxnSp();
|
CTConnector ctShape = ctAnchor.addNewCxnSp();
|
||||||
ctShape.set(XSSFConnector.prototype());
|
ctShape.set(XSSFConnector.prototype());
|
||||||
@ -305,20 +340,20 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
||||||
* and ovals.
|
* and ovals.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this group is attached
|
* @param anchor
|
||||||
* to the sheet.
|
* the client anchor describes how this group is attached to the
|
||||||
* @return the newly created shape.
|
* sheet.
|
||||||
|
* @return the newly created shape.
|
||||||
*/
|
*/
|
||||||
public XSSFShapeGroup createGroup(XSSFClientAnchor anchor)
|
public XSSFShapeGroup createGroup(XSSFClientAnchor anchor) {
|
||||||
{
|
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
||||||
CTGroupShape ctGroup = ctAnchor.addNewGrpSp();
|
CTGroupShape ctGroup = ctAnchor.addNewGrpSp();
|
||||||
ctGroup.set(XSSFShapeGroup.prototype());
|
ctGroup.set(XSSFShapeGroup.prototype());
|
||||||
CTTransform2D xfrm = createXfrm(anchor);
|
CTTransform2D xfrm = createXfrm(anchor);
|
||||||
CTGroupTransform2D grpXfrm =ctGroup.getGrpSpPr().getXfrm();
|
CTGroupTransform2D grpXfrm = ctGroup.getGrpSpPr().getXfrm();
|
||||||
grpXfrm.setOff(xfrm.getOff());
|
grpXfrm.setOff(xfrm.getOff());
|
||||||
grpXfrm.setExt(xfrm.getExt());
|
grpXfrm.setExt(xfrm.getExt());
|
||||||
grpXfrm.setChExt(xfrm.getExt());
|
grpXfrm.setChExt(xfrm.getExt());
|
||||||
@ -328,38 +363,38 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
return shape;
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a comment.
|
* Creates a comment.
|
||||||
* @param anchor the client anchor describes how this comment is attached
|
*
|
||||||
* to the sheet.
|
* @param anchor
|
||||||
* @return the newly created comment.
|
* the client anchor describes how this comment is attached to
|
||||||
*/
|
* the sheet.
|
||||||
|
* @return the newly created comment.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public XSSFComment createCellComment(ClientAnchor anchor) {
|
public XSSFComment createCellComment(ClientAnchor anchor) {
|
||||||
XSSFClientAnchor ca = (XSSFClientAnchor)anchor;
|
XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
|
||||||
XSSFSheet sheet = getSheet();
|
XSSFSheet sheet = getSheet();
|
||||||
|
|
||||||
//create comments and vmlDrawing parts if they don't exist
|
// create comments and vmlDrawing parts if they don't exist
|
||||||
CommentsTable comments = sheet.getCommentsTable(true);
|
CommentsTable comments = sheet.getCommentsTable(true);
|
||||||
XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
|
XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
|
||||||
com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
|
com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
|
||||||
if(ca.isSet()){
|
if (ca.isSet()) {
|
||||||
// convert offsets from emus to pixels since we get a DrawingML-anchor
|
// convert offsets from emus to pixels since we get a
|
||||||
|
// DrawingML-anchor
|
||||||
// but create a VML Drawing
|
// but create a VML Drawing
|
||||||
int dx1Pixels = ca.getDx1()/Units.EMU_PER_PIXEL;
|
int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL;
|
||||||
int dy1Pixels = ca.getDy1()/Units.EMU_PER_PIXEL;
|
int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL;
|
||||||
int dx2Pixels = ca.getDx2()/Units.EMU_PER_PIXEL;
|
int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL;
|
||||||
int dy2Pixels = ca.getDy2()/Units.EMU_PER_PIXEL;
|
int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL;
|
||||||
String position =
|
String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca
|
||||||
ca.getCol1() + ", " + dx1Pixels + ", " +
|
.getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels;
|
||||||
ca.getRow1() + ", " + dy1Pixels + ", " +
|
|
||||||
ca.getCol2() + ", " + dx2Pixels + ", " +
|
|
||||||
ca.getRow2() + ", " + dy2Pixels;
|
|
||||||
vmlShape.getClientDataArray(0).setAnchorArray(0, position);
|
vmlShape.getClientDataArray(0).setAnchorArray(0, position);
|
||||||
}
|
}
|
||||||
CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
|
CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
|
||||||
|
|
||||||
if(comments.findCellComment(ref) != null) {
|
if (comments.findCellComment(ref) != null) {
|
||||||
throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
|
throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,9 +404,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
/**
|
/**
|
||||||
* Creates a new graphic frame.
|
* Creates a new graphic frame.
|
||||||
*
|
*
|
||||||
* @param anchor the client anchor describes how this frame is attached
|
* @param anchor
|
||||||
* to the sheet
|
* the client anchor describes how this frame is attached to the
|
||||||
* @return the newly created graphic frame
|
* sheet
|
||||||
|
* @return the newly created graphic frame
|
||||||
*/
|
*/
|
||||||
private XSSFGraphicFrame createGraphicFrame(XSSFClientAnchor anchor) {
|
private XSSFGraphicFrame createGraphicFrame(XSSFClientAnchor anchor) {
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
|
||||||
@ -395,36 +431,40 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
/*
|
/*
|
||||||
* The shape id of the ole object seems to be a legacy shape id.
|
* The shape id of the ole object seems to be a legacy shape id.
|
||||||
*
|
*
|
||||||
* see 5.3.2.1 legacyDrawing (Legacy Drawing Object):
|
* see 5.3.2.1 legacyDrawing (Legacy Drawing Object): Legacy Shape ID
|
||||||
* Legacy Shape ID that is unique throughout the entire document.
|
* that is unique throughout the entire document. Legacy shape IDs
|
||||||
* Legacy shape IDs should be assigned based on which portion of the document the
|
* should be assigned based on which portion of the document the drawing
|
||||||
* drawing resides on. The assignment of these ids is broken down into clusters of
|
* resides on. The assignment of these ids is broken down into clusters
|
||||||
* 1024 values. The first cluster is 1-1024, the second 1025-2048 and so on.
|
* of 1024 values. The first cluster is 1-1024, the second 1025-2048 and
|
||||||
|
* so on.
|
||||||
*
|
*
|
||||||
* Ole shapes seem to start with 1025 on the first sheet ...
|
* Ole shapes seem to start with 1025 on the first sheet ... and not
|
||||||
* and not sure, if the ids need to be reindexed when sheets are removed
|
* sure, if the ids need to be reindexed when sheets are removed or more
|
||||||
* or more than 1024 shapes are on a given sheet (see #51332 for a similar issue)
|
* than 1024 shapes are on a given sheet (see #51332 for a similar
|
||||||
|
* issue)
|
||||||
*/
|
*/
|
||||||
XSSFSheet sheet = getSheet();
|
XSSFSheet sheet = getSheet();
|
||||||
XSSFWorkbook wb = sheet.getWorkbook();
|
XSSFWorkbook wb = sheet.getWorkbook();
|
||||||
int sheetIndex = wb.getSheetIndex(sheet);
|
int sheetIndex = wb.getSheetIndex(sheet);
|
||||||
long shapeId = (sheetIndex+1)*1024 + newShapeId();
|
long shapeId = (sheetIndex + 1) * 1024 + newShapeId();
|
||||||
|
|
||||||
// add reference to OLE part
|
// add reference to OLE part
|
||||||
PackagePartName olePN;
|
PackagePartName olePN;
|
||||||
try {
|
try {
|
||||||
olePN = PackagingURIHelper.createPartName( "/xl/embeddings/oleObject"+storageId+".bin" );
|
olePN = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + storageId + ".bin");
|
||||||
} catch (InvalidFormatException e) {
|
} catch (InvalidFormatException e) {
|
||||||
throw new POIXMLException(e);
|
throw new POIXMLException(e);
|
||||||
}
|
}
|
||||||
PackageRelationship olePR = sheetPart.addRelationship( olePN, TargetMode.INTERNAL, POIXMLDocument.OLE_OBJECT_REL_TYPE );
|
PackageRelationship olePR = sheetPart.addRelationship(olePN, TargetMode.INTERNAL,
|
||||||
|
POIXMLDocument.OLE_OBJECT_REL_TYPE);
|
||||||
|
|
||||||
// add reference to image part
|
// add reference to image part
|
||||||
XSSFPictureData imgPD = sh.getWorkbook().getAllPictures().get(pictureIndex);
|
XSSFPictureData imgPD = sh.getWorkbook().getAllPictures().get(pictureIndex);
|
||||||
PackagePartName imgPN = imgPD.getPackagePart().getPartName();
|
PackagePartName imgPN = imgPD.getPackagePart().getPartName();
|
||||||
PackageRelationship imgSheetPR = sheetPart.addRelationship( imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART );
|
PackageRelationship imgSheetPR = sheetPart.addRelationship(imgPN, TargetMode.INTERNAL,
|
||||||
PackageRelationship imgDrawPR = getPackagePart().addRelationship( imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART );
|
PackageRelationshipTypes.IMAGE_PART);
|
||||||
|
PackageRelationship imgDrawPR = getPackagePart().addRelationship(imgPN, TargetMode.INTERNAL,
|
||||||
|
PackageRelationshipTypes.IMAGE_PART);
|
||||||
|
|
||||||
// add OLE part metadata to sheet
|
// add OLE part metadata to sheet
|
||||||
CTWorksheet cwb = sh.getCTWorksheet();
|
CTWorksheet cwb = sh.getCTWorksheet();
|
||||||
@ -443,7 +483,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
|
cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
|
||||||
cur1.insertAttributeWithValue("moveWithCells", "1");
|
cur1.insertAttributeWithValue("moveWithCells", "1");
|
||||||
|
|
||||||
CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor)anchor);
|
CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
|
||||||
|
|
||||||
XmlCursor cur2 = ctAnchor.newCursor();
|
XmlCursor cur2 = ctAnchor.newCursor();
|
||||||
cur2.copyXmlContents(cur1);
|
cur2.copyXmlContents(cur1);
|
||||||
@ -460,7 +500,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
// add a new shape and link OLE & image part
|
// add a new shape and link OLE & image part
|
||||||
CTShape ctShape = ctAnchor.addNewSp();
|
CTShape ctShape = ctAnchor.addNewSp();
|
||||||
ctShape.set(XSSFObjectData.prototype());
|
ctShape.set(XSSFObjectData.prototype());
|
||||||
ctShape.getSpPr().setXfrm(createXfrm((XSSFClientAnchor)anchor));
|
ctShape.getSpPr().setXfrm(createXfrm((XSSFClientAnchor) anchor));
|
||||||
|
|
||||||
// workaround for not having the vmlDrawing filled
|
// workaround for not having the vmlDrawing filled
|
||||||
CTBlipFillProperties blipFill = ctShape.getSpPr().addNewBlipFill();
|
CTBlipFillProperties blipFill = ctShape.getSpPr().addNewBlipFill();
|
||||||
@ -469,35 +509,35 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
|
|
||||||
CTNonVisualDrawingProps cNvPr = ctShape.getNvSpPr().getCNvPr();
|
CTNonVisualDrawingProps cNvPr = ctShape.getNvSpPr().getCNvPr();
|
||||||
cNvPr.setId(shapeId);
|
cNvPr.setId(shapeId);
|
||||||
cNvPr.setName("Object "+shapeId);
|
cNvPr.setName("Object " + shapeId);
|
||||||
|
|
||||||
XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
|
XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
|
||||||
extCur.toFirstChild();
|
extCur.toFirstChild();
|
||||||
extCur.setAttributeText(new QName("spid"), "_x0000_s"+shapeId);
|
extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
|
||||||
extCur.dispose();
|
extCur.dispose();
|
||||||
|
|
||||||
XSSFObjectData shape = new XSSFObjectData(this, ctShape);
|
XSSFObjectData shape = new XSSFObjectData(this, ctShape);
|
||||||
shape.anchor = (XSSFClientAnchor)anchor;
|
shape.anchor = (XSSFClientAnchor) anchor;
|
||||||
|
|
||||||
return shape;
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all charts in this drawing.
|
* Returns all charts in this drawing.
|
||||||
*/
|
*/
|
||||||
public List<XSSFChart> getCharts() {
|
public List<XSSFChart> getCharts() {
|
||||||
List<XSSFChart> charts = new ArrayList<>();
|
List<XSSFChart> charts = new ArrayList<>();
|
||||||
for(POIXMLDocumentPart part : getRelations()) {
|
for (POIXMLDocumentPart part : getRelations()) {
|
||||||
if(part instanceof XSSFChart) {
|
if (part instanceof XSSFChart) {
|
||||||
charts.add((XSSFChart)part);
|
charts.add((XSSFChart) part);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return charts;
|
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
|
* @return a new CTTwoCellAnchor
|
||||||
*/
|
*/
|
||||||
@ -508,14 +548,21 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
ctAnchor.addNewClientData();
|
ctAnchor.addNewClientData();
|
||||||
anchor.setTo(ctAnchor.getTo());
|
anchor.setTo(ctAnchor.getTo());
|
||||||
anchor.setFrom(ctAnchor.getFrom());
|
anchor.setFrom(ctAnchor.getFrom());
|
||||||
STEditAs.Enum aditAs;
|
STEditAs.Enum editAs;
|
||||||
switch(anchor.getAnchorType()) {
|
switch (anchor.getAnchorType()) {
|
||||||
case DONT_MOVE_AND_RESIZE: aditAs = STEditAs.ABSOLUTE; break;
|
case DONT_MOVE_AND_RESIZE:
|
||||||
case MOVE_AND_RESIZE: aditAs = STEditAs.TWO_CELL; break;
|
editAs = STEditAs.ABSOLUTE;
|
||||||
case MOVE_DONT_RESIZE: aditAs = STEditAs.ONE_CELL; break;
|
break;
|
||||||
default: aditAs = STEditAs.ONE_CELL;
|
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(aditAs);
|
ctAnchor.setEditAs(editAs);
|
||||||
return ctAnchor;
|
return ctAnchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -526,15 +573,15 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
off.setY(anchor.getDy1());
|
off.setY(anchor.getDy1());
|
||||||
XSSFSheet sheet = getSheet();
|
XSSFSheet sheet = getSheet();
|
||||||
double widthPx = 0;
|
double widthPx = 0;
|
||||||
for (int col=anchor.getCol1(); col<anchor.getCol2(); col++) {
|
for (int col = anchor.getCol1(); col < anchor.getCol2(); col++) {
|
||||||
widthPx += sheet.getColumnWidthInPixels(col);
|
widthPx += sheet.getColumnWidthInPixels(col);
|
||||||
}
|
}
|
||||||
double heightPx = 0;
|
double heightPx = 0;
|
||||||
for (int row=anchor.getRow1(); row<anchor.getRow2(); row++) {
|
for (int row = anchor.getRow1(); row < anchor.getRow2(); row++) {
|
||||||
heightPx += ImageUtils.getRowHeightInPixels(sheet, row);
|
heightPx += ImageUtils.getRowHeightInPixels(sheet, row);
|
||||||
}
|
}
|
||||||
long width = Units.pixelToEMU((int)widthPx);
|
long width = Units.pixelToEMU((int) widthPx);
|
||||||
long height = Units.pixelToEMU((int)heightPx);
|
long height = Units.pixelToEMU((int) heightPx);
|
||||||
CTPositiveSize2D ext = xfrm.addNewExt();
|
CTPositiveSize2D ext = xfrm.addNewExt();
|
||||||
ext.setCx(width - anchor.getDx1() + anchor.getDx2());
|
ext.setCx(width - anchor.getDx1() + anchor.getDx2());
|
||||||
ext.setCy(height - anchor.getDy1() + anchor.getDy2());
|
ext.setCy(height - anchor.getDy1() + anchor.getDy2());
|
||||||
@ -543,17 +590,15 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
return xfrm;
|
return xfrm;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long newShapeId(){
|
private long newShapeId() {
|
||||||
return 1+
|
return 1 + drawing.sizeOfAbsoluteAnchorArray() + drawing.sizeOfOneCellAnchorArray() + drawing
|
||||||
drawing.sizeOfAbsoluteAnchorArray()+
|
.sizeOfTwoCellAnchorArray();
|
||||||
drawing.sizeOfOneCellAnchorArray()+
|
|
||||||
drawing.sizeOfTwoCellAnchorArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return list of shapes in this drawing
|
* @return list of shapes in this drawing
|
||||||
*/
|
*/
|
||||||
public List<XSSFShape> getShapes(){
|
public List<XSSFShape> getShapes() {
|
||||||
List<XSSFShape> lst = new ArrayList<>();
|
List<XSSFShape> lst = new ArrayList<>();
|
||||||
XmlCursor cur = drawing.newCursor();
|
XmlCursor cur = drawing.newCursor();
|
||||||
try {
|
try {
|
||||||
@ -569,7 +614,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
/**
|
/**
|
||||||
* @return list of shapes in this shape group
|
* @return list of shapes in this shape group
|
||||||
*/
|
*/
|
||||||
public List<XSSFShape> getShapes(XSSFShapeGroup groupshape){
|
public List<XSSFShape> getShapes(XSSFShapeGroup groupshape) {
|
||||||
List<XSSFShape> lst = new ArrayList<>();
|
List<XSSFShape> lst = new ArrayList<>();
|
||||||
XmlCursor cur = groupshape.getCTGroupShape().newCursor();
|
XmlCursor cur = groupshape.getCTGroupShape().newCursor();
|
||||||
try {
|
try {
|
||||||
@ -593,29 +638,28 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
// ignore anchor elements
|
// ignore anchor elements
|
||||||
continue;
|
continue;
|
||||||
} else if (obj instanceof CTPicture) {
|
} else if (obj instanceof CTPicture) {
|
||||||
shape = new XSSFPicture(this, (CTPicture)obj) ;
|
shape = new XSSFPicture(this, (CTPicture) obj);
|
||||||
} else if(obj instanceof CTConnector) {
|
} else if (obj instanceof CTConnector) {
|
||||||
shape = new XSSFConnector(this, (CTConnector)obj) ;
|
shape = new XSSFConnector(this, (CTConnector) obj);
|
||||||
} else if(obj instanceof CTShape) {
|
} else if (obj instanceof CTShape) {
|
||||||
shape = hasOleLink(obj)
|
shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj)
|
||||||
? new XSSFObjectData(this, (CTShape)obj)
|
: new XSSFSimpleShape(this, (CTShape) obj);
|
||||||
: new XSSFSimpleShape(this, (CTShape)obj) ;
|
} else if (obj instanceof CTGraphicalObjectFrame) {
|
||||||
} else if(obj instanceof CTGraphicalObjectFrame) {
|
shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
|
||||||
shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame)obj) ;
|
} else if (obj instanceof CTGroupShape) {
|
||||||
} else if(obj instanceof CTGroupShape) {
|
shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
|
||||||
shape = new XSSFShapeGroup(this, (CTGroupShape)obj) ;
|
} else if (obj instanceof XmlAnyTypeImpl) {
|
||||||
} else if(obj instanceof XmlAnyTypeImpl) {
|
LOG.log(POILogger.WARN,
|
||||||
LOG.log(POILogger.WARN, "trying to parse AlternateContent, "
|
"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!");
|
||||||
+ "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
|
// XmlAnyTypeImpl is returned for AlternateContent
|
||||||
|
// parts, which might contain a CTDrawing
|
||||||
cur.push();
|
cur.push();
|
||||||
cur.toFirstChild();
|
cur.toFirstChild();
|
||||||
XmlCursor cur2 = null;
|
XmlCursor cur2 = null;
|
||||||
try {
|
try {
|
||||||
// need to parse AlternateContent again, otherwise the child elements aren't typed,
|
// need to parse AlternateContent again,
|
||||||
|
// otherwise the child elements aren't typed,
|
||||||
// but also XmlAnyTypes
|
// but also XmlAnyTypes
|
||||||
CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
|
CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
|
||||||
cur2 = alterWS.newCursor();
|
cur2 = alterWS.newCursor();
|
||||||
@ -636,7 +680,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(shape != null);
|
assert (shape != null);
|
||||||
shape.anchor = getAnchorFromParent(obj);
|
shape.anchor = getAnchorFromParent(obj);
|
||||||
lst.add(shape);
|
lst.add(shape);
|
||||||
|
|
||||||
@ -651,7 +695,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
|
|
||||||
private boolean hasOleLink(XmlObject shape) {
|
private boolean hasOleLink(XmlObject shape) {
|
||||||
QName uriName = new QName(null, "uri");
|
QName uriName = new QName(null, "uri");
|
||||||
String xquery = "declare namespace a='"+XSSFRelation.NS_DRAWINGML+"' .//a:extLst/a:ext";
|
String xquery = "declare namespace a='" + XSSFRelation.NS_DRAWINGML + "' .//a:extLst/a:ext";
|
||||||
XmlCursor cur = shape.newCursor();
|
XmlCursor cur = shape.newCursor();
|
||||||
cur.selectPath(xquery);
|
cur.selectPath(xquery);
|
||||||
try {
|
try {
|
||||||
@ -667,21 +711,21 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private XSSFAnchor getAnchorFromParent(XmlObject obj){
|
private XSSFAnchor getAnchorFromParent(XmlObject obj) {
|
||||||
XSSFAnchor anchor = null;
|
XSSFAnchor anchor = null;
|
||||||
|
|
||||||
XmlObject parentXbean = null;
|
XmlObject parentXbean = null;
|
||||||
XmlCursor cursor = obj.newCursor();
|
XmlCursor cursor = obj.newCursor();
|
||||||
if(cursor.toParent()) {
|
if (cursor.toParent()) {
|
||||||
parentXbean = cursor.getObject();
|
parentXbean = cursor.getObject();
|
||||||
}
|
}
|
||||||
cursor.dispose();
|
cursor.dispose();
|
||||||
if(parentXbean != null){
|
if (parentXbean != null) {
|
||||||
if (parentXbean instanceof CTTwoCellAnchor) {
|
if (parentXbean instanceof CTTwoCellAnchor) {
|
||||||
CTTwoCellAnchor ct = (CTTwoCellAnchor)parentXbean;
|
CTTwoCellAnchor ct = (CTTwoCellAnchor) parentXbean;
|
||||||
anchor = new XSSFClientAnchor(ct.getFrom(), ct.getTo());
|
anchor = new XSSFClientAnchor(ct.getFrom(), ct.getTo());
|
||||||
} else if (parentXbean instanceof CTOneCellAnchor) {
|
} else if (parentXbean instanceof CTOneCellAnchor) {
|
||||||
CTOneCellAnchor ct = (CTOneCellAnchor)parentXbean;
|
CTOneCellAnchor ct = (CTOneCellAnchor) parentXbean;
|
||||||
anchor = new XSSFClientAnchor(getSheet(), ct.getFrom(), ct.getExt());
|
anchor = new XSSFClientAnchor(getSheet(), ct.getFrom(), ct.getExt());
|
||||||
} else if (parentXbean instanceof CTAbsoluteAnchor) {
|
} else if (parentXbean instanceof CTAbsoluteAnchor) {
|
||||||
CTAbsoluteAnchor ct = (CTAbsoluteAnchor) parentXbean;
|
CTAbsoluteAnchor ct = (CTAbsoluteAnchor) parentXbean;
|
||||||
@ -700,7 +744,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
|
|||||||
* @return the sheet associated with the drawing
|
* @return the sheet associated with the drawing
|
||||||
*/
|
*/
|
||||||
public XSSFSheet getSheet() {
|
public XSSFSheet getSheet() {
|
||||||
return (XSSFSheet)getParent();
|
return (XSSFSheet) getParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user