Free Support Forum - aspose.com

Align textblock in Shape

I use Aspose.Diagram for Java to create a VISIO diagram from a SVG document.

How can I align textblock to top in the Diagram Shape. By default it is centered and it is possible to align only a “text in textblock”. Maybe I can change a size of a textblock to be the same as its Shape size and “text in textblock” alignment will be enough for this?

Didn’t find any working example in the forum or API reference.

@iteraplan,

The TextXForm class helps to set the position of text inside the shape in the same way as we can set Text Block Format section in the shapesheet with Microsoft Visio application. Please refer to this help topic: Format Text in the Visio Shape’s Text Block Section

Thanks for reply, using help topic reference (align text to the left section) didn’t help. All letters are aligned to left and looking weird (see attached VISIO - iteraplan-InformationFlowDiagram-2018-06-20__15_33_16.zip (11.9 KB)
).

The code used for that shapes is here and text alignment is in the end of the createVisio() method:

public Object createVisio(IFDVisioContext ctx) throws Exception {
    AffineTransform tr = ctx.getParentTransform();
    AffineTransform nt = new AffineTransform();
    nt.concatenate(tr);
    double xCentr = (getPosition().getX() + getRectangle().getCenterX());
    double yCentr = (getPosition().getY() + getRectangle().getCenterY());
    nt.translate(xCentr, -yCentr);
    double translateX = nt.getTranslateX();
    double translateY = nt.getTranslateY();
    long shapeId = ctx.getDiagram().addShape(translateX, translateY, ctx.getMasterNodeName(), ctx.getCurrentPage().getID());
    Shape legendItemShape = ctx.getDiagram().getPages().getPage(ctx.getCurrentPage().getID()).getShapes().getShape(shapeId);
    legendItemShape.setWidth(getRectangle().getWidth() / IteraplanVisioUtils.VISIO_SCALE);
    legendItemShape.setHeight(getRectangle().getHeight() / IteraplanVisioUtils.VISIO_SCALE);
    // Borders
    if (getBorderLinePattern() != null) {
      legendItemShape.getLine().getLinePattern().setValue(getBorderLinePattern());
    } else {
      // No border
      legendItemShape.getLine().getLineColorTrans().setValue(1.0);
    }
    // Legend item text
    if (!getName().isEmpty() && getName() != null) {
      // Text value
      legendItemShape.getText().getValue().clear();
      legendItemShape.getText().getValue().add(new Txt(getName()));
      // Font size
      legendItemShape.getChars().add(new Char());
      legendItemShape.getChars().get(0).setIX(0);
      legendItemShape.getChars().get(0).getSize().setValue(getIsTitle() ? 0.17 : 0.13);
      // Legend item text color
      StyleSheet textStyle = new StyleSheet();
      textStyle.getFill().setFillForegnd(IteraplanVisioUtils.getColorValue(getTextColor()));
      legendItemShape.setTextStyle(textStyle);
      // Align text to left (as in Aspose tutorial)
      legendItemShape.getTextXForm().getTxtLocPinX().setValue(legendItemShape.getTextXForm().getTxtWidth().getValue());
      legendItemShape.getTextXForm().getTxtPinX().setValue(0);
      double angleDeg = 0;
      double angleRad = (Math.PI / 180) * angleDeg;
      legendItemShape.getTextXForm().getTxtAngle().setValue(angleRad);
    } else {
      legendItemShape.getText().getValue().clear();
    }
    // Legend item color
    String[] colors = getColor().split(";");
    // Single value coloring
    legendItemShape.getFill().setFillForegnd(IteraplanVisioUtils.getColorValue(colors[0]));

    return legendItemShape;
  }

@iteraplan,

Please send us your source Visio drawing and the fully compilable code, because there are unknown objects in your source code. We will investigate your scenario in our environment, and share our findings with you.

@imran.rafique, sorry, but not fully understand what do you mean under “source Visio drawing”? Is it a document used for Diagram diagram = new Diagram(dataDir + fileName);, master .vssx or an output after diagram.save()?

Simplified code to a function working with shape text:

// Text is set to rectangular shape
  private void setShapeText(Shape shape) {
    // Text value
    shape.getText().getValue().clear();
    shape.getText().getValue().add(new Txt("Hello world"));
    // Font size
    shape.getChars().add(new Char());
    shape.getChars().get(0).setIX(0);
    shape.getChars().get(0).getSize().setValue(0.17);
    // Text color
    StyleSheet textStyle = new StyleSheet();
    textStyle.getFill().setFillForegnd(new ColorValue("#aaaaaa", MeasureConst.COLOR));
    shape.setTextStyle(textStyle);
    // Align text to left (as in Aspose tutorial)
    shape.getTextXForm().getTxtLocPinX().setValue(shape.getTextXForm().getTxtWidth().getValue());
    shape.getTextXForm().getTxtPinX().setValue(0);
    double angleDeg = 0;
    double angleRad = (Math.PI / 180) * angleDeg;
    shape.getTextXForm().getTxtAngle().setValue(angleRad);
  }

Thanks for replying!

@iteraplan,

Please send us the input Visio drawing and stencil file (if any VSS or VSSX).
Diagram diagram = new Diagram(dataDir + fileName);

I use this stencil file: StencilTmp.zip (12.0 KB). As an input Visio file I use temporarily created .vsdx: svg2vsdx_result8739937649103134879.zip (8.9 KB)

@iteraplan,

We are unable to replicate the problem of misaligned text in our environment as we could view in your output drawing (iteraplan-InformationFlowDiagram-2018-06-20__15_33_16.vsdx). Please send us the complete code, so that we could replicate the same problem in our environment and also let us know the version of Aspose.Diagram for Java API. Your response is awaited.

@imran.rafique, attached complete sandbox project where the problem is replicated: SandBox.zip (200.2 KB). I use Aspose Diagram 18.3.

! Please, include your “Aspose.Diagram.lic” file to the DIR “/src/main/resources/templ” to build and run the project.
! Run Runner.java to get the resulting .vsdx file (it will be added to the root directory, file name - ifd1.vsdx)

Currenly there is ifd1.vsdx in zipped sandbox folder, which represents the problem: text in red shapes looks strange, but should be aligned to left and be readable.

Waiting for your results, thank you!

@iteraplan,

We have tested your application project in our environment, and it shows a null pointer error on loading an SVG into the Document instance before the call of Aspose.Diagram API.
Java

Document document = factory.createDocument("svg", svgAsStream);

Please review and let us know in case ambiguity or queries.

@imran.rafique, after zipping a project, tool created additional “Sandbox” folder. Skip it and open inner one, so app will use correct directories. Thanks!

@iteraplan,

We have investigated your scenario, and could find that the SVG file is available on the specified path. The input stream instance is also not null. Please take a look over this snapshot: https://i.imgur.com/jVa4JJz.png

@imran.rafique, thanks for investigation. It is normal for getXMLParserClassName() to return null, the default parser is the JRE included parser then.

You can just skip this, resuming Runner.main() and review the resulting VISIO document. Also forgot to guide you to Node.java, where text is set and aligned to the shape.

@iteraplan,

When we skip the call of getXMLParserClassName() method, the compilation errors are being generated for other lines of code. Please simplify the code, and elaborate a bit more about the usage of Node.java file.

@imran.rafique, attaching working version SandBox.zip (190.4 KB)
.
If Runner.java still produces some errors, please send detailed error description or screenshot.

More deeply about how everything works:
After reading SVG content and minor preprocessesisng (replacing fill=“transparent”) it is saved to a document (Runner.java, line 27).
Then SVG document is transcoded to context object with hierarchy (VisioTranscoder.java, line 28).
That context wiil be used in IFDBuilder.java when calling buildVisioDiagram() to build Visio items for all elements of SVG document.
Pay attention on creation of Node object (IFDBuilder.java, line 131) and also for building its Visio shape with text (Node.java, line 51). There setShapeText() method is used for text alignment.

@iteraplan,

We are unable to run your application project in our environment. There are object reference errors, please send us a ZIP of all Jar files and simplify the code.

@imran.rafique, please, provide full description of errors, because everything is running on my environments, both Eclipse and IDEA.

@iteraplan

We have tried to Build shared SandBox project in NetBeans but the Build fails because declared dependencies under Dependencies folder are not downloaded fine; consequently, further errors occur while parsing Java classes in different packages including the Java classes under com.edvantis.iteraplan and other folders. Therefore, please share the JAR files for declared dependencies and also remove unnecessary classes to narrow down the problem so that we may proceed further to address your concerns efficiently.

@Farhan.Raza, thank you for clarification. Have opened my project in NetBeans and reproduced the issue you have mentioned before. Have fixed this by setting correct jre version. Also all JARs are included in target/lib.

Please, import the project to your NetBeans instance and try running it again: https://drive.google.com/file/d/17_sSQK2r8jfufsv57t7YyRgjmN7bbnPH/view?usp=sharing

Select Runner.java as main class and also don’t forget to attach your Aspose.Diagram.lic file to \src\main\resources\templ. Resulting drawing will be added to the project root.

@iteraplan

Thank you for updating the data.

We are investigating the scenario in our environment and will get back to you with our findings soon.