We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Can not get shapeId when i update word from version 14.6 to 21.7

We recently updated from version 14.6 to 21.7,
when I use cells-words-14.6.0-jdk15.jar,I can get shapeid from DrawingDL instance like this:

    Method[] methods = drawingML.getClass().getDeclaredMethods();
     for (Method method : methods) {
	if (org.w3c.dom.Document.class.isAssignableFrom(method.getReturnType())) {
		method.setAccessible(true);
		org.w3c.dom.Document xml = (org.w3c.dom.Document) 
                method.invoke(drawingML);
		Element graphicData = XmlUtility.getChildElementByTagNameSuffix(xml.getDocumentElement(),":graphicData");
		Element chart = XmlUtility.getChildElementByTagNameSuffix(graphicData, ":chart");
		shapeId = XmlUtility.getAttributeByNameSuffix(chart, ":id");
	}
}

but now aspose-words-21.7 is not has class drawingML ,I see it has merged into Shape
11B6105B-8344-4930-99C1-3589184F1393.png (88.7 KB)

so I change to use class Shape,but I can not get shapeId now , So how can i get shapeId in 21.7 ?
Look forward to your reply,Thanks.

@xhaixia As I can see you use org.w3c.dom.Document to parse documents XML to get id attribute of the chart. I suspect you then use this id to get the appropriate chart.xml and change it. If so, in new version of Aspose.Words there is Shape.getChart() property chart returns a Chart object that allows manipulating the chart.
Could you please elaborate your scenario a bit more? We will investigate and provide you more information.

1、I parse the word file into a ZipInputStream instance,then I loop the entry,when I find the name equals “word/_rels/document.xml.rels”,I can get the charts id from attribute,then I put id and target chartExcelFiles into a map.

in = new ByteArrayInputStream(word);
     ZipInputStream zip = new ZipInputStream(in);
ZipEntry entry = zip.getNextEntry();
while (entry != null) {
    if (entry.getName().startsWith("word/charts/") && entry.getName().endsWith(".xml")) {
		org.w3c.dom.Document xml = XmlUtility.parse(new IgnoreCloseInputStream(zip));
		int index = entry.getName().lastIndexOf('/') + 1;
		int endIndex = entry.getName().lastIndexOf('.');
		chartDocMap.put(entry.getName().substring(index, endIndex), xml);
	} else if (entry.getName().equals("word/_rels/document.xml.rels")) {
		org.w3c.dom.Document xml = XmlUtility.parse(new IgnoreCloseInputStream(zip));
		NodeList list = xml.getElementsByTagName("Relationship");
		for (int i = 0, len = list.getLength(); i < len; i++) {
			Element relationship = (Element) list.item(i);
			String target = relationship.getAttribute("Target");
			if (target.startsWith("charts/")) {
				target = target.substring("charts/".length(), target.lastIndexOf('.'));
				documentRels.put(relationship.getAttribute("Id"), target);
            }
        }
    }
    zip.closeEntry();
	entry = zip.getNextEntry();
}

2、when I want to update the chart data , I need the shapeId to find target Excel , Then I just need to update the excel file

String chartName = documentRels.get(shapeId);
chartName = "";
org.w3c.dom.Document xml = (org.w3c.dom.Document) chartDocMap.get(chartName);
String fileName = chartRels.get(chartName);
byte[] excel = embeddings.get(fileName);
.......
replaceItems.put("word/charts/" + chartName + ".xml", newExcelFile.toByteArray());

The method Shape.getChart() I tried , but can not get shapeId also.
Look forward to your reply,Thanks.

@xhaixia In the old 14.6.0 version of Aspose.Words not fully supported DrawingML shapes. To preserve them XML representation of these shapes was stored in Aspose.Words model and was written into the output documents without modifications. There were an internal property that returned this XML. It seems in your code you are using exactly this property. There was no public API to get the chart’s id.
Now DrawingML shapes are read into Aspose.Words DOM so it is not required to keep source XML representation of them in the model. Moreover, chart identifier might be changed after processing document. It is generated while saving document to OOXML file formats.
In new version you can use Aspose.Words API to modify charts without parsing the source document ZIP.

I tried to rewrite it using the new API, but I encountered several problems that could not be solved:

  1. The x-axis cannot be obtained. It is a combination of several columns of data. I tried chart.getLegend().getPosition(). It seems that it can, but I’m not sure
    图1.png (125.2 KB)

  2. The graphic seriesname in word is not displayed, but I have a value when I get the name through chart.getSeries().get(index).getName(), and I can’t find the corresponding interface to set whether to display it
    图2.png (66.1 KB)

  3. When there are primary and secondary axes, how should I process the secondary axis data through chart. I can only get the information of the main coordinate axis through chart.getSeries()
    图3.png (23.7 KB)

@xhaixia

Could you please attach a document with a sample chart here for testing? We will check and provide you more information.

Series name is always there, but in your case it is hidden in the legend. Unfortunately, Aspose.Words does not have API to hide items in the legend yet. I have logged this feature request as WORDSNET-23210.

Currently there is no API in Aspose.Words to work with composite charts. This feature is logged as WORDSNET-20209.

word_chart.docx (23.0 KB)
above is the document with a sample chart.

Since most attributes which I need cannot be obtained through Aspose.Words API , Cound you provide a way for me to obtain the XML content of chart like 14.7,so that I can run the program normally after upgrading to 21.7.
Look forward to your reply,Thanks.

@xhaixia Thank you for additional information. I have logged a feature request WORDSNET-23216 to allow accessing multilevel X values of series.
Unfortunately it is not possible to provide access to the source XML of the shape, since it is not stored in the Aspose.Words DOM. In your case I think you can get this XML directly from document.xml from the source document.

The issues you have found earlier (filed as WORDSNET-23210) have been fixed in this Aspose.Words for Java 22.4 update.