I am trying to make a chart like this:
Capture.PNG (39.5 KB)
I am able to create a chart like this, but I can’t seem to find a way to set the property of connector line between two SmartArtNode nodes. It’s set to default blue color. I want to change color and change the connector to dash style connector line, like in the above image.
I am using following code to define a parent child relation between two SmartArtNodes:
Presentation pres = new Presentation();
ISlide slide = pres.getSlides().get_Item(0);
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
SmartArtNode childNode= (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes()).addNodeByPosition(0);
childNode.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
I have attached a pptx too, showing how I want connector lines to be.
@tanujd_203,
I have observed your requirements and suggest you to please try using following sample code on your end to serve the purpose.
public static void TestSmartLines()
{
Presentation pres = new Presentation();
ISlide slide = pres.getSlides().get_Item(0);
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
SmartArtNode childNode= (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes()).addNodeByPosition(0);
ISmartArtShapeCollection shapes = childNode.getShapes();
for(int i=0;i<shapes.size();i++)
{
ISmartArtShape sh = shapes.get_Item(i);
if(sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1){
sh.getLineFormat().setDashStyle(LineDashStyle.SystemDot);
sh.getLineFormat().setWidth(3.5);
}
}
childNode.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
pres.save("c:\\Aspose data\\TestSmart.pptx", SaveFormat.Pptx);
}
I hope the shared information will be helpful.
Many Thanks,
Mudassir Fayyaz
Hi. I tried the above code that you shared to implement dotted lines between nodes, using the following code:
Presentation pres = new Presentation();
// Access the presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art IShape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
while (parentNode.getChildNodes().size() > 0) {
parentNode.getChildNodes().removeNode(0);
}
SmartArtNode chNode0_1 = (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes())
.addNodeByPosition(0);
ISmartArtShapeCollection shapes = chNode0_1.getShapes();
for (int i = 0; i < shapes.size(); i++) {
ISmartArtShape sh = shapes.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
chNode0_1.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
SmartArtNode chNode1 = (SmartArtNode) ((SmartArtNodeCollection) chNode0_1.getChildNodes()).addNodeByPosition(0);
chNode1.getTextFrame().setText("Shivshant Kumar\nGeneral Manager - HRIS");
ISmartArtShapeCollection shapes2 = chNode1.getShapes();
for (int i = 0; i < shapes2.size(); i++) {
ISmartArtShape sh = shapes2.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
// Save Presentation
pres.save("C:\\Users\\userDefault\\Desktop\\pptxFont.pptx", SaveFormat.Pptx);
This doesn’t change the format of all the connector lines. Can you please let me know the issue in this?
@tanujd_203,
I have observed your following comments:
Can you please share the desired result that you wish to achieve as earlier code that I have shared was in accordance with your requirements shared.
Many Thanks,
Mudassir Fayyaz
Capture.PNG (38.2 KB)
Look at this image. I have customized the line format for both children nodes, but it’s visible only in one children node.
I have used the code that I mentioned above. In case of any confusion, here it is:
Presentation pres = new Presentation();
// Access the presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art IShape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
while (parentNode.getChildNodes().size() > 0) {
parentNode.getChildNodes().removeNode(0);
}
SmartArtNode chNode0_1 = (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes())
.addNodeByPosition(0);
ISmartArtShapeCollection shapes = chNode0_1.getShapes();
for (int i = 0; i < shapes.size(); i++) {
ISmartArtShape sh = shapes.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
chNode0_1.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
SmartArtNode chNode1 = (SmartArtNode) ((SmartArtNodeCollection) chNode0_1.getChildNodes()).addNodeByPosition(0);
chNode1.getTextFrame().setText("Shivshant Kumar\nGeneral Manager - HRIS");
ISmartArtShapeCollection shapes2 = chNode1.getShapes();
for (int i = 0; i < shapes2.size(); i++) {
ISmartArtShape sh = shapes2.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
// Save Presentation
pres.save("C:\\Users\\userDefault\\Desktop\\pptxFont.pptx", SaveFormat.Pptx);
It would be great if I could get all the connector lines to be formatted the way I would like.
Hi Mudassir. Can you please update me on this? I really in a tough spot here since this is needed in the project we are working on.
Can you please test the code that I shared? Not all the connector lines are getting changed when we are changing the fill format.
If you can’t pour in more support, please let me know in that case too.
@tanujd_203,
I am sorry for your inconvenience. You need to add the line formats at end nodes addition. Actually, in your sample code you have tried setting the line format for lines between first two nodes before adding second node. Which actually is wrong. You need to add the line format after adding the concerned nodes. Please try using following sample code on your end to serve the purpose.
public static void TestConnetors()
{
Presentation pres = new Presentation();
// Access the presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art IShape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
while (parentNode.getChildNodes().size() > 0) {
parentNode.getChildNodes().removeNode(0);
}
SmartArtNode chNode0_1 = (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes())
.addNodeByPosition(0);
chNode0_1.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
SmartArtNode chNode1 = (SmartArtNode) ((SmartArtNodeCollection) chNode0_1.getChildNodes()).addNodeByPosition(0);
chNode1.getTextFrame().setText("Shivshant Kumar\nGeneral Manager - HRIS");
ISmartArtShapeCollection shapes = chNode0_1.getShapes();
for (int i = 0; i < shapes.size(); i++) {
ISmartArtShape sh = shapes.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
ISmartArtShapeCollection shapes2 = chNode1.getShapes();
for (int i = 0; i < shapes2.size(); i++) {
ISmartArtShape sh = shapes2.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
// Save Presentation
pres.save("C:\\Aspose Data\\pptxFont2.pptx", SaveFormat.Pptx);
}
Many Thanks,
Mudassir Fayyaz
Hi Mudassir,
Thank you so much for the solution! Worked like a charm.
Can you please tell me how can I change the color of text in the textframe?
I tried following code, but it made the text go away:
node.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
ITextFrame textFrame = node.getTextFrame();
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
IPortion portion = paragraph.getPortions().get_Item(0);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.getColor("#000000"));
@tanujd_203,
I have observed your comments. I have shared a piece of code. This code will help you to achieve your requirements. Please share feedback with us if there is still an issue.
final Presentation pres = new Presentation();
try
{
pres.getSlides().get_Item(0).getBackground().getFillFormat().getSolidFillColor().setColor(Color.toJava(Color.getLemonChiffon().Clone()));
IShapeCollection shapes = pres.getSlides().get_Item(0).getShapes();
IAutoShape autoShape = shapes.addAutoShape(ShapeType.Rectangle, 10, 10, 700, 240);
ITextFrame textFrame = autoShape.addTextFrame("11");
IPortionFormat pf = textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.toJava(Color.getBlack().Clone()));
pres.save(resourcesOutputPath + "/pres.pptx", SaveFormat.Pptx);
}
finally { if (pres != null) ((IDisposable)pres).dispose(); }
Thanks, this works fine!
But I would like to change the text color of the text frame of a ‘SmartArtNode’.
@tanujd_203,
I have observed your comments. I have shared a piece of code. This will help you to serve the purpose. Please share feedback with us if there is still an issue.
Presentation pres = new Presentation();
// Access the presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art IShape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
IPortionFormat pf = parentNode.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.toJava(Color.getBlack().Clone()));
while (parentNode.getChildNodes().size() > 0) {
parentNode.getChildNodes().removeNode(0);
}
SmartArtNode chNode0_1 = (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes())
.addNodeByPosition(0);
chNode0_1.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
pf = chNode0_1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.toJava(Color.getBlack().Clone()));
SmartArtNode chNode1 = (SmartArtNode) ((SmartArtNodeCollection) chNode0_1.getChildNodes()).addNodeByPosition(0);
chNode1.getTextFrame().setText("Shivshant Kumar\nGeneral Manager - HRIS");
pf = chNode1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.toJava(Color.getBlack().Clone()));
ISmartArtShapeCollection shapes = chNode0_1.getShapes();
for (int i = 0; i < shapes.size(); i++) {
ISmartArtShape sh = shapes.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.awt.Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
ISmartArtShapeCollection shapes2 = chNode1.getShapes();
for (int i = 0; i < shapes2.size(); i++) {
ISmartArtShape sh = shapes2.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.awt.Color.BLACK);
sh.getLineFormat().setWidth(2);
}
}
// Save Presentation
pres.save(resourcesOutputPath + "\\pptxFont2.pptx", SaveFormat.Pptx);ispose(); }
Thank you so much! Works like a charm! 
One more thing. I have a requirement that would need connector line to be white-colored or hidden.
Unfortunately, when i use java.awt.Color.WHITE, that doesn’t change the color. The color stays black.
Even changing the setWidth to 0, like sh.getLineFormat().setWidth(0) doesn’t make it invisible.
Can you please provide me a solution to this?
I am using the following code (though I have only edited the width and color of lineformat() in the above code that you shared):
Presentation pres = new Presentation();
// Access the presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art IShape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.OrganizationChart);
ISmartArtNode parentNode = smart.getAllNodes().get_Item(0);
parentNode.getTextFrame().setText("H S Bhartia\nCCMD");
IPortionFormat pf = parentNode.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
.getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.black);
while (parentNode.getChildNodes().size() > 0) {
parentNode.getChildNodes().removeNode(0);
}
SmartArtNode chNode0_1 = (SmartArtNode) ((SmartArtNodeCollection) parentNode.getChildNodes())
.addNodeByPosition(0);
chNode0_1.getTextFrame().setText("Raju Sunil Mistry\nChief Human Resources Officer");
pf = chNode0_1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.black);
SmartArtNode chNode1 = (SmartArtNode) ((SmartArtNodeCollection) chNode0_1.getChildNodes()).addNodeByPosition(0);
chNode1.getTextFrame().setText("Shivshant Kumar\nGeneral Manager - HRIS");
pf = chNode1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.black);
ISmartArtShapeCollection shapes = chNode0_1.getShapes();
for (int i = 0; i < shapes.size(); i++) {
ISmartArtShape sh = shapes.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.awt.Color.WHITE);
sh.getLineFormat().setWidth(0);
}
}
ISmartArtShapeCollection shapes2 = chNode1.getShapes();
for (int i = 0; i < shapes2.size(); i++) {
ISmartArtShape sh = shapes2.get_Item(i);
if (sh.getShapeType() == -1 || sh.getShapeType() == 0 || sh.getShapeType() == 1) {
sh.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.awt.Color.WHITE);
sh.getLineFormat().setWidth(0);
}
}
// Save Presentation
pres.save("C:\\Users\\Username\\Desktop\\pptxFont.pptx", SaveFormat.Pptx);
@tanujd_203,
Please add following line before setting fill color to serve the purpose.
sh.getLineFormat().getFillFormat().setFillType(FillType.Solid);
Nice! It worked! Thank you so much!
Just one last thing, how can a set some part of the text in bold format? For example, consider the line below:
node.getTextFrame().setText("User Name\nDesignation");
I would like the ‘User Name’ in bold. I tried using the html bold tags, but they didn’t work.
@tanujd_203,
When you set the text on text frame level, the default text formatting is used. When you wish to add some text proprieties to text then you need to add the text on Portion level. Every, text frame has got collection of paragraphs. The paragraphs are formed on basis of new line feeds or carriage returns. There are some properties like alignment and bullets that you can set on paragraph level. Inside every paragraph there is collection of portions. If there is a single text with same formatting then that paragraph will have one portion inside it. However, if you change the text color, height, underline, italicize or bolden text then you will need to keep them in separate portion. The rule is simple that you will have a new portion whenever there is change in textual properties. I hope this will be understandable. Please visit this documentation link for more about using paragraph portions in presentation.
Alright. Thanks for this useful info. This will come in handy!
Also, I have run into an issue. When I am creating large charts with 50+ nodes, the boxes get resized to fit into the slide, but the texts remain contant, and are appearing out of the boxes. Look at the image below:
Capture.PNG (49.0 KB)
How can I set the text to stay fixed inside the text frame?
@tanujd_203,
I have observed your comments. Can you please share source code so that we can further investigate to help you out. Also please share environment details with us.
@tanujd_203,
You can please try text frame auto fit property to accommodate text inside shape. You can please try using following sample code on your end. Please also visit this link for possible TextAutofitType values.
node.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Normal);
Hi Musdassir, thank you for your reply. The solution you provided is not working in my case. For normal sized charts, the text gets auto-fit, but in very large charts, that property doesn’t work.
Chart where the text autofits:
Capture1.PNG (49.3 KB)
Chart where text doesnt get autofit:
Capture2.PNG (44.7 KB)
Is there a limit? upto which the font size gets reduced?