How to Print String type data in Shape Data from java

Hi sir,

I am unable to show name of the shape in Shape data from java program.

please see image i mentioned instead of string there is showing number value.aspose.png (40.5 KB)

looking forward here for you response. please help me.

Thanks in advance

@gopi17242,
Please try the following code:

[Java]

String dataDir = "C:\\temp\\";
// Call a Diagram class constructor to load the VSDX diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// Get page by name
Page page = diagram.getPages().getPage("Page-3");
// get shape by ID
Shape shape = page.getShapes().getShape(1);
	
for (Prop prop :(Iterable<Prop>) shape.getProps())
{
    if (prop.getLabel().getValue() == "DeviceType")
        prop.getValue().setVal(shape.getName());
}

// save Visio drawing
diagram.save(dataDir + "Output.vsdx", SaveFileFormat.VSDX);

Hi Imran,

Thank you so much for response. but still i am facing the problem that i explained in the image please see once. i used your code but something i am doing wrong that unable to find for proper result.shape data.png (105.7 KB)

Thanks in advance.

@gopi17242,
Kindly send the complete details of your use case, including source drawing and code. Please cut off the additional shapes and lines of the code and then send details. We will investigate and share our findings with you. Your response is awaited.

package com.test.visio.main;

public class ExportVisioMap {

public static void main(String[] args) throws Exception {
	


	String dirPath = Utils.getSharedDataDir(ExportVisioMap.class) ;  
	String shapeDir = Utils.getSharedDataDir(ExportVisioMap.class) + "shapes/";

// String shapeDir = “shapes/”;
License license = new License();
license.setLicense(dirPath+ “license/” + “Aspose.Diagram.lic”);
// license.setLicense(“license/” + “Aspose.Diagram.lic”);

	// Create directory if it is not already present.
	File file = new File(dirPath);
	if (!file.exists())
		file.mkdir();
	// initialize a new Diagram
	Diagram diagram = new Diagram();

	int maxPageId  = GetMaxPageID(diagram);

	// Initialize a new page object
	Page newPage = new Page();
	// Set name
	newPage.setName("spectre map");
	// Set page ID
	newPage.setID(maxPageId + 1);
	// Add a new blank page
	diagram.getPages().add(newPage);

	// master shapes
	String generalappliance = "generalappliance", serverswitch = "serverswitch", bridge = "bridge", 
			commserver = "commserver", firewall = "firewall", universalgateway ="universalgateway",
			hub = "hub", router = "router", kvmDevice = "kvmdevice", layer3switch = "layer3switch",
			virtuallayerswitch = "virtuallayerswitch", netsonar = "netsonar", printer = "printer",
			storagearray = "storagearray", terminal = "terminal", ups= "ups", bug = "bug",
			dynamicConnector = "Dynamic connector", cellularphone = "cellularphone",unknown = "unknown";

	diagram.addMaster(shapeDir + "Test.vss", generalappliance);
	diagram.addMaster(shapeDir + "Test.vss", serverswitch);
	diagram.addMaster(shapeDir + "Test.vss", bridge);
	diagram.addMaster(shapeDir + "Test.vss", commserver);
	diagram.addMaster(shapeDir + "Test.vss", firewall);
	diagram.addMaster(shapeDir + "Test.vss", universalgateway);
	diagram.addMaster(shapeDir + "Test.vss", hub);
	diagram.addMaster(shapeDir + "Test.vss", router);
	diagram.addMaster(shapeDir + "Test.vss", kvmDevice);
	diagram.addMaster(shapeDir + "Test.vss", layer3switch);
	diagram.addMaster(shapeDir + "Test.vss", virtuallayerswitch);
	diagram.addMaster(shapeDir + "Test.vss", netsonar);
	diagram.addMaster(shapeDir + "Test.vss", printer);
	diagram.addMaster(shapeDir + "Test.vss", storagearray);
	diagram.addMaster(shapeDir + "Test.vss", terminal);
	diagram.addMaster(shapeDir + "Test.vss", ups);
	diagram.addMaster(shapeDir + "Test.vss", bug);
	diagram.addMaster(shapeDir + "Test.vss", dynamicConnector);
	diagram.addMaster(shapeDir + "Test.vss", cellularphone);
	diagram.addMaster(shapeDir + "Test.vss", unknown);


	//removing default page
	Page page = diagram.getPages().getPage(0);
	diagram.getPages().remove(diagram.getPages().get(page.getID()));
	
	int PageIndex = 0;

	Page p = diagram.getPages().getPage("spectre map");

	p.getPageSheet().getPageProps().getDrawingResizeType().setValue(DrawingResizeTypeValue.DEPENDS_ON_DRAWING_SIZE_TYPE);

	// page orientation
	p.getPageSheet().getPrintProps().getPrintPageOrientation().setValue(PrintPageOrientationValue.PORTRAIT);

	//iterating json data

	JSONParser nodeParser = new JSONParser();
	JSONParser edgeParser = new JSONParser();
	Map<Source, Target> sourceTargetMap = null;
	ArrayList<Nodes>  totalNodesList = new  ArrayList<Nodes>();
	long  highestX = 0;   
	long  highestY = 0;
	try {

		Object nodesobj = nodeParser.parse(new FileReader(dirPath + "//json//esi_nodes.json"));
		Object edgeobj = edgeParser.parse(new FileReader(dirPath + "//json//esi_edges.json"));
	/*	Object nodesobj = nodeParser.parse(new FileReader("json/nodes.json"));
		Object edgeobj = edgeParser.parse(new FileReader("json/edges.json"));

*/
JSONArray nodesList = (JSONArray) nodesobj;
JSONArray edgeList = (JSONArray) edgeobj;

		for (int i = 0; i < nodesList.size(); i++) {

			JSONObject jsonObject = (JSONObject) nodesList.get(i);

			Nodes nodes = new Nodes();
			long node_id = (Long) jsonObject.get("node_id");
			String ip_address = (String) jsonObject.get("ip_address".trim());
			long xAxis = (Long) jsonObject.get("x");
			long yAxis = (Long) jsonObject.get("y");
			String device_type = (String) jsonObject.get("device type");

			nodes.setNode_id(node_id);
			nodes.setIp_address(ip_address);
			nodes.setDevice_type(device_type);
			nodes.setX(xAxis);
			nodes.setY(yAxis);
			totalNodesList.add(nodes);

			long  highestXtemp = 0;   
			long  highestYtemp = 0;   

			// to find highest value of X in a Nodes
			if(xAxis > highestX)
				highestXtemp = xAxis;
			if(highestXtemp > highestX)
				highestX = highestXtemp;	

			// to find highest value of Y in a Nodes
			if(yAxis > highestY)
				highestYtemp = yAxis;
			if(highestYtemp > highestY)
				highestY = highestYtemp;		

		}

		//converting X and Y axis to Inches 
		if(highestX != 0 && highestY != 0) {
			highestX = (long) (highestX*0.1042/10);
			highestY = (long) (highestY*0.1042/10);
		} else {
			highestX = 100;
			highestY = 100;
		}
		
		System.out.println("highest X axis :: "+highestX);
		System.out.println("highest Y axis :: "+highestY);
		
		//setting highest X and Y as Page width and height
		p.getPageSheet().getPageProps().getPageWidth().setValue(highestX+1);
		p.getPageSheet().getPageProps().getPageHeight().setValue(highestY+1);

		System.out.println("Number of Nodes :: "+totalNodesList.size());

		sourceTargetMap = new HashMap<Source, Target>();

		for (int i = 0; i < edgeList.size(); i++) {

			JSONObject jsonObject = (JSONObject) edgeList.get(i);

			long source = (Long) jsonObject.get("source");
			long target = (Long) jsonObject.get("target");

			Source sourceObj = new Source();
			sourceObj.setSource(source);
			Target targetObj = new Target();
			targetObj.setTarget(target);
			sourceTargetMap.put(sourceObj, targetObj);
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ParseException e) {
		e.printStackTrace();
	}

	ArrayList<Positions> positionList = new ArrayList<Positions>();

	for (Nodes nodes : totalNodesList) {

		double sourceWidth = (double)nodes.getX();
		double sourceHeight =(double)nodes.getY();

		if(sourceWidth != 0) {
			sourceWidth = sourceWidth*0.1042/10;
		} 
		if(sourceHeight != 0) {
			sourceHeight = sourceHeight*0.1042/10;
		} 
		long sourcePosition = diagram.addShape(sourceWidth, sourceHeight, nodes.getDevice_type(), PageIndex);
		Shape shape = p.getShapes().getShape(sourcePosition);
		shape.getTextXForm().getTxtWidth().setValue(1.5); 
		
		shape.getParas().add(new Para()); 
		shape.getParas().getPara(0).getHorzAlign().setValue(HorzAlignValue.JUSTIFY); 
		//set index for para
		shape.getParas().getPara(0).setIX(0);
		// mark character run and add text 
		shape.getText().getValue().add(new Cp(0)); 
		shape.getText().getValue().add(new Txt(nodes.getIp_address())); 
		
		
		Prop prop1 = new Prop();
		prop1.setName("IP");
		prop1.getLabel().setValue("IP");

// prop1.getValue().setVal(nodes.getIp_address().toString());
prop1.getType().setValue(PropType.STRING);
prop1.getValue().setVal(nodes.getIp_address().toString());

		Prop prop2 = new Prop();
		prop2.setName("XAxis");
		prop2.getLabel().setValue("XAxis");
		prop2.getValue().setVal(sourceWidth+"");
		
		Prop prop3 = new Prop();
		prop3.setName("YAxis");
		prop3.getLabel().setValue("YAxis");
		prop3.getValue().setVal(sourceHeight+"");
		
		Prop prop4 = new Prop();
		TypeProp typeProp4 = new TypeProp(TypePropValue.STRING);
		prop4.setName("DeviceType");
		prop4.getLabel().setValue("DeviceType");
		String deviceType = nodes.getDevice_type();
	//	System.out.println("typeProp4========"+typeProp4.getValue());
		prop4.getType().setValue(typeProp4.getValue());
	//	prop4.getValue().setVal(typeProp4.getValue());
	//	System.err.println("type==="+nodes.getDevice_type());
		
		shape.getProps().add(prop1);
		shape.getProps().add(prop2);
		shape.getProps().add(prop3);
		shape.getProps().add(prop4);
		
	//	System.err.println("count==="+shape.getProps().getCount());
		
		
		/*for (Prop prop :(Iterable<Prop>) shape.getProps())
		{
		    if (prop.getLabel().getValue() == "DeviceType")
		        prop.getValue().setVal(shape.getName());
		}*/

		Positions position = new Positions();
		position.setNode_id(nodes.getNode_id());
		position.setPosition(sourcePosition);
		positionList.add(position);
	}
	long connectorPosition = 0;
	Set<Source> keySet = sourceTargetMap.keySet();           
	for (Source keys : keySet) {
		Source sourceId = keys;
		Target targetId =  (Target)sourceTargetMap.get(keys);

		long sourcePosition=0;
		long targetPosition=0;

		for (Positions pos : positionList) {
			if(sourceId.getSource() == pos.getNode_id()) {
				sourcePosition = pos.getPosition();
			}if(targetId.getTarget() == pos.getNode_id()) {
				targetPosition = pos.getPosition();
			}
		}

		Shape connectorShape = new Shape();
		// set line weight, default in PT
		connectorShape.getLine().getLineWeight().setValue(0.01388);
		// set color of the shape's line
		connectorShape.getLine().getLineColor().setValue("#3058a7");
		// set line rounding, default in inch
		connectorShape.getLine().getRounding().setValue(0);
		// set line caps
		connectorShape.getLine().getLineCap().setValue(BOOL.TRUE);
		// set line color transparency in percent
		connectorShape.getLine().getLineColorTrans().setValue(50);

		connectorPosition = diagram.addShape(connectorShape, dynamicConnector, PageIndex);
		p.connectShapesViaConnector(sourcePosition, ConnectionPointPlace.RIGHT, targetPosition, ConnectionPointPlace.LEFT, connectorPosition);
	}

	/*Shape shape1 = p.getShapes().getShape(1);
	for (Prop prop :(Iterable<Prop>) shape1.getProps())
	{
		System.out.println("Label Name=="+prop.getLabel().getValue());
		System.out.println("Label Value=="+prop.getValue().getVal());
		
	    if (prop.getLabel().getValue() == "DeviceType")
	        prop.getValue().setVal(prop.getValue().getVal());
	}*/
	
	// Save diagram
	SimpleDateFormat localDateFormat = new SimpleDateFormat("MMddYYYY-HHmmss");
	 String fileNameWithDataAndTime = "spectre_digram-"+localDateFormat.format(new Date())+".vsdx";
	System.out.println(fileNameWithDataAndTime);
	diagram.save(dirPath + "diagrams/" +fileNameWithDataAndTime , SaveFileFormat.VSDX);

// diagram.save(“diagrams/” +fileNameWithDataAndTime , SaveFileFormat.VSDX);
}

private static int GetMaxPageID(Diagram diagram)
{
	int max = diagram.getPages().getPage(0).getID();
	for (int i = 1; i < diagram.getPages().getCount(); i++)
	{
		if (max < diagram.getPages().getPage(i).getID())
			max = diagram.getPages().getPage(i).getID();
	}
	return max;
}

}

1 Like

That is my java code and this is the out put file ShapeData.png (110.9 KB)

please let me know if need any other information from my side.

please give needful suggestion to workout it. here looking forward.

Thanks in Advaced

@gopi17242,
Kindly send us your source files Test.vss, esi_nodes.json and esi_edges.json which you are using in the code example. It would be great if you can also simplify the code example or prepare a small project application, and then send a Zip of this project application. We will investigate further and share our findings with you.

Hi Imran,

Here is whole my java project(maven). it contains all required files and you can run directly in eclipse or what ever.

test-visio.zip (512.6 KB)

once again i am telling my problem is whenever trying to create shape data to every shape from java program it is showing in generated diagram in visio file at shape data option. but problem is whenever String data is coming as value to labels it is convert as numeric(double) instead String(name of the shape).

Note: its need to create String type data from java program and not manually in visio file.(in visio file manually we are able to set String type property value)

please let me know if need any info.

looking forward here.

Thanks in Adavance

@gopi17242,
We managed to replicate the said problem in our environment. It has been logged under the ticket ID DIAGRAMJAVA-50547 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Hi Imran,

Thank you. Good news is i got the solution.
Node nodes = new Node();
//adding shape data
Prop xProp = new Prop();
xProp.setName(“DeviceType”);
xProp.getLabel().setValue(“Device Type”);
xProp.getValue().setVal("’"+nodes.getDevice_type()+"’");
xProp.getFormat().setValue("’"+nodes.getDevice_type()+"’");

Now i am getting label value as String. That’s fine.

But Still i am having a problem in Shape Data when i will go into the option at Define Shape Data.

There i am not getting any value. means my String data is not appear in that prompt window.

@gopi17242,
We have logged this information under the same ticket ID DIAGRAMJAVA-50547 in our issue tracking system.

Hi Imran,

Thank you for your support and quick response.

Here i am facing the problem while showing the complete values of a shape at seeing defined shape data after creating the visio file from java. please find attached image Shape1.png (136.5 KB)
for better understand the problem.

here my complete code as maven project and you have to run the ExportVisioMap.java file as console based. then you can see the visio file in diagram folder in resources.

test-visio.zip (1.3 MB)

Please let me know if need any extra information.

looking forward here.

Thank you.

@gopi17242,
The linked ticket ID DIAGRAMJAVA-50547 has been resolved. The problem is about the unit of the value which is not set to “String” in Visio when the value is a string value. The next version 17.9 will be published in the recent week and we will notify you. The recent use case will be covered in the next release version 17.9 of Aspose.Diagram for Java API.

@gopi17242,
The linked ticket ID DIAGRAMJAVA-50547 has been fixed. Please download and try the new version 17.9 of Aspose.Diagram for Java API.