Set text into a rectangle

I’m in the process of creating a table. It consists of rectangles in which the text will appear.
Drawing the rectangles worked fine. But when I set the text for a rectangle, it appears above the rectangle. I’d like the text to appear inside the rectangle, though – just like you see in Visio.

As an example, I’ve limited my table to just one cell. Can you tell me what I need to do to make the text appear inside the rectangle rather than on top of it?

Diagram diagram = new Diagram();

Aspose.Diagram.Page page = diagram.Pages[0];

// DIN-A4 
page.PageSheet.PageProps.PageWidth.Value = 8.27;   // Inch
page.PageSheet.PageProps.PageHeight.Value = 11.69; // Inch

double cellWidth = 3.5;
double cellHeight = 0.25;

// Startposition top left
double startX = 0.5;
double startY = 11.2;

double x1 = 0.5 + cellWidth;
double y1 = 11.2 - cellHeight;

double x2 = x1 + cellWidth;
double y2 = y1 - cellHeight;

long shapeId = page.DrawRectangle(x1, y1, x2, y2);

Aspose.Diagram.Shape shape = page.Shapes.GetShape(shapeId);


shape.Text.Value.Add(new Txt("Row 1, Column 1"));

// Line Grid
shape.Line.LineWeight.Value = 0.01;
shape.Line.LineColor.Value = "RGB(0,0,0)";

shape.Fill.FillForegnd.Value = "RGB(255,255,255)";

if (shape.Chars.Count == 0)
{
	shape.Chars.Add(new Aspose.Diagram.Char());
}
shape.Chars[0].Size.Ufe.F = "10 pt";

diagram.Save("C:\\TableCell.vsdx", SaveFileFormat.Vsdx);

@sgruth
Thanks for the sample codes.
Please make the height value of the rectangle box positive

       double x1 = 0.5 + cellWidth;
       double y1 = 11.2 - cellHeight;

       double x2 = x1 + cellWidth;
       double y2 = y1 - cellHeight;  //For example, change this line of code to this: double y2 = y1 + cellHeight;

or adjust the Shape’s TextXForm:

        shape.TextXForm.TxtPinX.Value = shape.XForm.Width.Value * 0.5;
        shape.TextXForm.TxtPinY.Value = shape.XForm.Height.Value * 0.5;
        shape.TextXForm.TxtWidth.Value = shape.XForm.Width.Value;
        shape.TextXForm.TxtHeight.Value = shape.XForm.Height.Value;
        shape.TextXForm.TxtLocPinX.Value = shape.TextXForm.TxtWidth.Value * 0.5;
        shape.TextXForm.TxtLocPinY.Value = -shape.TextXForm.TxtHeight.Value * 0.5;

Thanks.

thanks alot, it works. :+1:

@sgruth
It is great that you were able to resolve this issue on your end. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Diagram’ forum.