How to Connect Shapes with Connectors in Java

How to Connect Shapes with Connectors in Java

Connectors in Aspose.Slides FOSS are line shapes that attach to connection sites on other shapes. When you move a connected shape, the connector endpoint moves with it. The most common connector type is BENT_CONNECTOR3, which routes around obstacles with a single elbow bend.


Prerequisites

<dependency>
  <groupId>org.aspose.slides.foss</groupId>
  <artifactId>aspose-slides-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Connection Site Indexes

Every shape has four numbered connection sites:

IndexPosition
0Top center
1Left center
2Bottom center
3Right center

Connect Two Shapes

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);

    // Add two rectangles
    IAutoShape box1 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 200, 200, 100
    );
    IAutoShape box2 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 450, 200, 200, 100
    );

    box1.addTextFrame("Start");
    box2.addTextFrame("End");

    // Add a bent connector (initial bounds are overwritten by the connection)
    IConnector conn = slide.getShapes().addConnector(
        ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10
    );

    // Connect right side of box1 (site 3) to left side of box2 (site 1)
    conn.setStartShapeConnectedTo(box1);
    conn.setStartShapeConnectionSiteIndex(3);
    conn.setEndShapeConnectedTo(box2);
    conn.setEndShapeConnectionSiteIndex(1);

    prs.save("connected.pptx", SaveFormat.PPTX);
}

The placeholder bounds (0, 0, 10, 10) passed to addConnector are ignored once the connection endpoints are set; PowerPoint re-routes the connector to the attached shapes.


Connector Types

import org.aspose.slides.foss.ShapeType;

// Straight line
ShapeType.STRAIGHT_CONNECTOR1

// Single elbow (L-shape)
ShapeType.BENT_CONNECTOR2

// Double elbow (Z-shape): most common
ShapeType.BENT_CONNECTOR3

// Curved connector
ShapeType.CURVED_CONNECTOR3

Style the Connector Line

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);

    IAutoShape box1 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 150, 180, 80
    );
    IAutoShape box2 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 500, 300, 180, 80
    );

    IConnector conn = slide.getShapes().addConnector(
        ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10
    );
    conn.setStartShapeConnectedTo(box1);
    conn.setStartShapeConnectionSiteIndex(2);   // bottom of box1
    conn.setEndShapeConnectedTo(box2);
    conn.setEndShapeConnectionSiteIndex(0);     // top of box2

    // Style: 2 pt width, blue color
    ILineFormat lf = conn.getLineFormat();
    lf.setWidth(2.0);
    lf.getFillFormat().getSolidFillColor().setColor(
        Color.fromArgb(255, 0, 0, 255)
    );

    prs.save("styled-connector.pptx", SaveFormat.PPTX);
}

Flowchart with Multiple Connectors

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);

    // Three-step flowchart
    IAutoShape step1 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 350, 50, 200, 70
    );
    IAutoShape step2 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 350, 220, 200, 70
    );
    IAutoShape step3 = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 350, 390, 200, 70
    );

    step1.addTextFrame("Step 1");
    step2.addTextFrame("Step 2");
    step3.addTextFrame("Step 3");

    // Connect step1 -> step2
    IConnector conn1 = slide.getShapes().addConnector(
        ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10
    );
    conn1.setStartShapeConnectedTo(step1);
    conn1.setStartShapeConnectionSiteIndex(2);   // bottom
    conn1.setEndShapeConnectedTo(step2);
    conn1.setEndShapeConnectionSiteIndex(0);     // top

    // Connect step2 -> step3
    IConnector conn2 = slide.getShapes().addConnector(
        ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10
    );
    conn2.setStartShapeConnectedTo(step2);
    conn2.setStartShapeConnectionSiteIndex(2);
    conn2.setEndShapeConnectedTo(step3);
    conn2.setEndShapeConnectionSiteIndex(0);

    prs.save("flowchart.pptx", SaveFormat.PPTX);
}

Read Connector Properties

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation("connected.pptx")) {
    for (IShape shape : prs.getSlides().get(0).getShapes()) {
        if (shape instanceof Connector) {
            IConnector conn = (IConnector) shape;
            IShape start = conn.getStartShapeConnectedTo();
            IShape end = conn.getEndShapeConnectedTo();
            System.out.println("Connector: "
                + (start != null ? start.getName() : "?")
                + " -> "
                + (end != null ? end.getName() : "?"));
        }
    }
}

See Also