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

Free Support Forum - aspose.com

Centering Text on Straight Line Connector

My issue is specifically with a connector that is straight (For connectors that have angles, the text is centered correctly). The problem is that I cannot get the connector text to appear in the center of the connector line. Below is the connector code.

        Shape connectorShape1 = new Shape();
        connectorShape1.getLine().getEndArrow().setValue(4);
        connectorShape1.getLine().getLineWeight().setValue(0.01388);
        connectorShape1.getText().getValue().setWholeText("% Trustee");
        connectorShape1.getChars().add(shapeText);
      
        long connecter1Id = firstPage.addShape(connectorShape1, "Dynamic connector");
        connectorShape1.setConnectorsType(ConnectorsTypeValue.STRAIGHT_LINES);

// The below does NOT work.
Control connectorControl = new Control();
connectorControl.getX().getUfe().setF(“GUARD(Width0.5)”);
connectorControl.getY().getUfe().setF(“GUARD(Width0.5)”);
connectorShape1.getControls().add(connectorControl);

// The below does not work either.
// connectorShape1.getTextXForm().getTxtPinX().getUfe().setF(“GUARD(Width0.5)”);
// connectorShape1.getTextXForm().getTxtPinY().getUfe().setF(“GUARD(Height0.5)”);

If anyone has gotten this to work, I would appreciate some guidance.

@richardcromer

Can you please share complete code snippet along with source Diagram and Stencil files? We will test the scenario in our environment and address it accordingly.

@richardcromer

We have logged an issue as DIAGRAMJAVA-50838 in our issue tracking system for the sake of further investigation. We will look into its details and keep you informed about its rectification status. Please be patient and spare us some time.

We are sorry for the inconvenience.

@richardcromer
Please try the latest fix 21.11.1. We have fixed this issue in the fix.
https://forum.aspose.com/uploads/discourse_instance3/57350
Thanks.

Greetings,
Version 21.11 does NOT correct the problem for me. In fact, now the straight line does NOT appear. It is back to an angled line. I verified this by running the same application (exact same code) using aspose 21.9 (which does support a straight line connector – just that the text is not centered) , and against the 21.11 which does NOT draw a straight line connector at all.

Below is my entire visiocreator.java file:
package com.example.jsfdemo.controllers;

import com.aspose.diagram.*;
import jakarta.annotation.PostConstruct;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpSession;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

@Named
@ViewScoped
public class VisioCreator implements Serializable {
private final static Logger LOGGER = Logger.getLogger(VisioCreator.class.getName());

private Integer maxChildren;
private Integer maxLevels;
private String message;
private String fileName;
private Boolean fileExists;
private Path path;
private String downloadFormattedFile;
private StreamedContent fileToDownload;
private static final String DOWNLOAD_DIRECTORY = "c:\\downloadtester\\";


@PostConstruct
public void init() {
    maxChildren = 5;
    maxLevels = 3;
    fileName = "";
    fileExists = false;

    com.aspose.diagram.License license = new com.aspose.diagram.License();

    FacesContext context = FacesContext.getCurrentInstance();
    jakarta.faces.context.ExternalContext external = context.getExternalContext();
    String libDir = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/lib/");

    try {
        license.setLicense(libDir + "Aspose.Diagram.Java.lic");
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        this.setFileName("OH");
        createVisioFile();
    } catch (Exception e)
    {
        LOGGER.info(e.getMessage());
    }
}

public void createVisioFile() throws Exception {
    // The path to the documents' directory.
    String dataDir = DOWNLOAD_DIRECTORY + "Diagrams/";
    // Create directory if it is not already present.
    File file = new File(dataDir);
    if (!file.exists())
        file.mkdir();

    // initialize a new Diagram
    Diagram diagram = new Diagram();

    PageCollection pages = diagram.getPages();
    Page firstPage = pages.getPage(0);
    int pageId = firstPage.getID();

    String stencilsDir = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/stencils/");
    LOGGER.info("******* The Stencils Directory is : " + stencilsDir);

    String corporation = "Rectangle";
    String fiduciary = "Hexagon";
    String individual = "Circle";
    String partnership = "Triangle";

    // First, We must add the master to the diagram (from the appropriate stencil file)
    diagram.addMaster(stencilsDir + "Basic Shapes.vssx",individual);
    diagram.addMaster(stencilsDir + "Basic Shapes.vssx",partnership);
    diagram.addMaster(stencilsDir + "Basic Shapes.vssx",fiduciary);
    diagram.addMaster(stencilsDir + "Basic Shapes.vssx",corporation);
    diagram.addMaster(stencilsDir + "Blocks.vssx","Dynamic connector");

    int PageIndex = 0;

    double width = 1, height = 1, pinX = 4.0, pinY = 9.5;
    // Add a new process shape
    try {
        Char shapeText = new Char();
        shapeText.getColor().setValue("#000000");
        shapeText.getSize().setValue(0.07);

        long ownerId = diagram.addShape(pinX, pinY - 2, width, height, fiduciary, PageIndex);
        Shape shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(ownerId);
        shape.getText().getValue().add(new Txt("Trust 1\n Grantor\n FEIN: XX-XXXXX"));
        shape.getFill().getFillForegnd().setValue("#add8e6");
        shape.getChars().add(shapeText);


        long individualId1 = diagram.addShape(pinX-2, pinY-1, width, height, individual, PageIndex);
        shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(individualId1);
        shape.getText().getValue().add(new Txt("Person 1 \n Role \n NonResident \n SSN: 000-XX-0000"));
        shape.getFill().getFillForegnd().setValue("#ff0000");
        shape.getChars().add(shapeText);


        long individualId2 = diagram.addShape(pinX+1, pinY, width, height, individual, PageIndex);
        shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(individualId2);
        shape.getText().getValue().add(new Txt("Person 2 \n Role \n Resident \n SSN: 000-99-0000"));
        shape.getFill().getFillForegnd().setValue("#ff0000");
        shape.getChars().add(shapeText);


        long partnershipId1 = diagram.addShape(pinX-2, pinY-3, width, height, partnership, PageIndex);
        shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(partnershipId1);
        shape.getText().getValue().add(new Txt("LLC 1 \n Domicile State \n FEIN: XXX-XXXXX \n SOSID: XX-XXXX"));
        shape.getFill().getFillForegnd().setValue("#ffff00");
        shape.getChars().add(shapeText);

// partnership1Text.getStyle().setValue(StyleValue.BOLD | StyleValue.ITALIC);

        long partnershipId2 = diagram.addShape(pinX, pinY-5, width, height, partnership, PageIndex);
        shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(partnershipId2);
        shape.getText().getValue().add(new Txt("LLC 2 \n Domicile State \n FEIN: XXX-XXXXX \n SOSID: XX-XXXX"));
        shape.getFill().getFillForegnd().setValue("#ffff00");
        shape.getChars().add(shapeText);


        long corporationId1 = diagram.addShape(pinX+3, pinY-3, width, height, corporation, PageIndex);
        shape = diagram.getPages().getPage(PageIndex).getShapes().getShape(corporationId1);
        shape.getText().getValue().add(new Txt("Corporation 1 \n Domicile State \n S Corp \n FEIN: XXX-XXXXX \n CCN: XXXXXX"));
        shape.getFill().getFillForegnd().setValue("#90EE90");
        shape.getChars().add(shapeText);


        // initialize connector shape
        Shape connectorShape1 = new Shape();
        connectorShape1.getLine().getEndArrow().setValue(4);
        connectorShape1.getLine().getLineWeight().setValue(0.01388);
        connectorShape1.getText().getValue().setWholeText("% Trustee");
        // set text position
        connectorShape1.getChars().add(shapeText);


        Shape connectorShape2 = new Shape();
        connectorShape2.getLine().getEndArrow().setValue(4);
        connectorShape2.getLine().getLineWeight().setValue(0.01388);
        connectorShape2.getText().getValue().setWholeText("% Beneficiary");
        // set text position
        connectorShape2.getTextXForm().getTxtPinX().getUfe().setF("Width0.5");
        connectorShape2.getTextXForm().getTxtPinY().getUfe().setF("Height0.5");
        connectorShape2.getChars().add(shapeText);

        Shape connectorShape3 = new Shape();
        connectorShape3.getLine().getEndArrow().setValue(4);
        connectorShape3.getLine().getLineWeight().setValue(0.01388);
        connectorShape3.getText().getValue().setWholeText("% Sole Proprietor");
        // set text position
        connectorShape3.getTextXForm().getTxtPinX().getUfe().setF("Width0.5");
        connectorShape3.getTextXForm().getTxtPinY().getUfe().setF("Height0.5");
        connectorShape3.getChars().add(shapeText);


        Shape connectorShape4 = new Shape();
        connectorShape4.getLine().getEndArrow().setValue(4);
        connectorShape4.getLine().getLineWeight().setValue(0.01388);
        connectorShape4.getText().getValue().setWholeText("% GP/LP \n Interest");
        // set text position
        connectorShape4.getTextXForm().getTxtPinX().getUfe().setF("Width0.5");
        connectorShape4.getTextXForm().getTxtPinY().getUfe().setF("Height0.5");
        connectorShape4.getChars().add(shapeText);

        Shape connectorShape5 = new Shape();
        connectorShape5.getLine().getEndArrow().setValue(4);
        connectorShape5.getLine().getLineWeight().setValue(0.01388);
        connectorShape5.getText().getValue().setWholeText("% Sole Proprietor");
        // set text position
        connectorShape5.getTextXForm().getTxtPinX().getUfe().setF("Width0.5");
        connectorShape5.getTextXForm().getTxtPinY().getUfe().setF("Height0.5");
        connectorShape5.getChars().add(shapeText);

        Shape connectorShape6 = new Shape();
        connectorShape6.getLine().getEndArrow().setValue(4);
        connectorShape6.getLine().getLineWeight().setValue(0.01388);
        connectorShape6.getText().getValue().setWholeText("% GP/LP \n Interest");
        // set text position
        connectorShape6.getTextXForm().getTxtPinX().getUfe().setF("Width0.5");
        connectorShape6.getTextXForm().getTxtPinY().getUfe().setF("Height0.5");
        connectorShape6.getChars().add(shapeText);

        // add connectors to page
        long connecter1Id = firstPage.addShape(connectorShape1, "Dynamic connector");
        connectorShape1.setConnectorsType(ConnectorsTypeValue.STRAIGHT_LINES);

        Control connectorControl = new Control();
        connectorControl.getX().getUfe().setF("GUARD(Width0.5)");
        connectorControl.getY().getUfe().setF("GUARD(Width0.5)");
        connectorShape1.getControls().add(connectorControl);

// connectorShape1.getTextXForm().getTxtPinX().getUfe().setF(“GUARD(Width0.5)”);
// connectorShape1.getTextXForm().getTxtPinY().getUfe().setF(“GUARD(Height0.5)”);

        long connecter2Id = firstPage.addShape(connectorShape2, "Dynamic connector");
        long connecter3Id = firstPage.addShape(connectorShape3, "Dynamic connector");
        long connecter4Id = firstPage.addShape(connectorShape4, "Dynamic connector");
        long connecter5Id = firstPage.addShape(connectorShape5, "Dynamic connector");
        long connecter6Id = firstPage.addShape(connectorShape6, "Dynamic connector");

        // connect sub-shapes
        firstPage.connectShapesViaConnector(ownerId, ConnectionPointPlace.TOP, individualId2, ConnectionPointPlace.BOTTOM,
                connecter1Id);
        firstPage.connectShapesViaConnector(ownerId, ConnectionPointPlace.LEFT, individualId1, ConnectionPointPlace.RIGHT,
                connecter2Id);
        firstPage.connectShapesViaConnector(partnershipId2, ConnectionPointPlace.TOP, partnershipId1, ConnectionPointPlace.BOTTOM,
                connecter3Id);
        firstPage.connectShapesViaConnector(partnershipId2, ConnectionPointPlace.TOP, ownerId, ConnectionPointPlace.BOTTOM,
                connecter4Id);
        firstPage.connectShapesViaConnector(partnershipId1, ConnectionPointPlace.TOP, individualId1, ConnectionPointPlace.BOTTOM,
                connecter5Id);
        firstPage.connectShapesViaConnector(partnershipId2, ConnectionPointPlace.TOP, corporationId1, ConnectionPointPlace.BOTTOM,
                connecter6Id);


        // save in the VSDX format
        FacesContext fCtx = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
        String sessionId = session.getId();
        message = "Hello, !  The session Id is " + sessionId;
        downloadFormattedFile = formattedDownloadFile(getFileName(), "vsdx");
        path = Paths.get(DOWNLOAD_DIRECTORY + sessionId + "_" + downloadFormattedFile);

        diagram.save(path.toString(), SaveFileFormat.VSDX);

        if (outputFileExists(path.toString()))
        {
            fileExists = true;
        }
    } catch (Exception e)
    {
        System.out.println(e.getMessage());
    }

}

private String formattedDownloadFile(String fileName, String ext)
{
    Optional<String> extension = getExtensionByStringHandling(fileName);
    if (!extension.isPresent() || (extension.isPresent() && (extension.get() != ext)))
        return fileName + "." + ext;
    else
        return fileName;

}

private Optional<String> getExtensionByStringHandling(String filename) {
    return Optional.ofNullable(filename)
            .filter(f -> f.contains("."))
            .map(f -> f.substring(filename.lastIndexOf(".") + 1));
}

public void createFile() {
   System.out.printf("maxChildren = %d, and maxLevels = %d\n",maxChildren,maxLevels);

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
    String sessionId = session.getId();
    message = "Hello, !  The session Id is " + sessionId;
    Path path = Paths.get(DOWNLOAD_DIRECTORY + sessionId + "_" + getFileName());

    try (BufferedWriter writer = Files.newBufferedWriter(path))
    {
        writer.write("This the first file I am attempting to download!!");
        fileExists = true;
    }
    catch (IOException e)
    {
        LOGGER.log(Level.SEVERE,"IOException Occurred attempting to create the file",e);
    }

    if (outputFileExists(path.toString()))
    {
        try {
            final InputStream fileContentsStream = new FileInputStream(path.toString());
            fileToDownload = DefaultStreamedContent.builder()
                    .name(fileName)
                    .contentType("text/plain")
                    .stream(() -> fileContentsStream)
                    .build();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    }
    LOGGER.info("****After creating the Streamed Content it is " + fileToDownload);
    LOGGER.info("****The file to download's stream is " + fileToDownload.getStream());

}


public StreamedContent getFileToDownload() {
    try {
        final InputStream fileContentsStream = new FileInputStream(path.toString());
        fileToDownload = DefaultStreamedContent.builder()
                .name(downloadFormattedFile)
                .contentType("application/zip")
                .stream(() -> fileContentsStream)
                .build();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return fileToDownload;
}


public Boolean getFileExists() {
    return fileExists;
}

private boolean outputFileExists(String path)
{
    LOGGER.info("****** The path of the visio file is " + path);
    File tempFile = new File(path);
    return tempFile.exists();
}
public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getMessage() {
    return message;
}

public Integer getMaxChildren() {
    return maxChildren;
}

public void setMaxChildren(Integer maxChildren) {
    this.maxChildren = maxChildren;
}

public Integer getMaxLevels() {
    return maxLevels;
}

public void setMaxLevels(Integer maxLevels) {
    this.maxLevels = maxLevels;
}

}

@richardcromer

Could you please confirm that you tried using 21.11.1 hotfix which was shared above? We believe that the issue has been addressed in this hotfix.