Setting textbox height

I am trying to generate a series of textboxes using Aspose.Words.Drawing.Shape that contain various lengths of text.

How do I set the height of the textbox so that it adjusts itself to the length of the text?

Hi Charles,

Thanks for your inquiry. You should specify the TextBox.FitShapeToText property to true so that Microsoft Word will grow the shape to fit text as follows:

Document doc = new Document(MyDir + "in.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
shape.TextBox.FitShapeToText = true;
doc.Save(MyDir + "Out.docx");

Please read the members of TextBox class from here:
https://reference.aspose.com/words/net/aspose.words.drawing/textbox/

Hope this helps you. Please let us know if you have any more queries.

Is there any way for me to determine the height of the adjusted textbox?

I am trying to generate several textboxes that are each positioned relative to the previous one. I need the adjusted height so I can set the Y-axis position of the next textbox.

Thanks!

Hi Charles,

Thanks for your inquiry. Please use the following code snippet to get the new adjusted height after using TextBox.FitShapeToText = true. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
shape.TextBox.FitShapeToText = true;
double newHeight = shape.GetShapeRenderer().SizeInPoints.Height;

That works to return a value, but for some reason the line

'shape.GetShapeRenderer().SizeInPoints.Height’

seems to prevent any of the shapes from actually rendering on the page. Any ideas?

Hi Charles,

Thanks for your inquiry. The Shape.GetShapeRenderer method creates and returns an object that can be used to render this shape into an image. If you are facing any issue while using this method, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Here is a code sample of what I am doing.

The textboxes are all clumped together because when I try to use ‘shape.GetShapeRenderer().SizeInPoints.Height’ it always returns a 0.

I would like the text boxes to be spaced evenly from each other.

Any help you can offer would be great.

Hi Charles,

Thanks for sharing the detail. The ‘shape.GetShapeRenderer().SizeInPoints.Height’ returns 0 value because the height of GroupShape is zero. Please set the GroupShape properties before creating the TextBox Shape. Moreover, increase the size of GroupShape as shown in following code snippet.

Dim doc As New Document
Const shapeSpacing As Integer = 5
Dim topics As List(Of Topic) = GenerateTopicList()
Dim top As Integer = 74
Dim left As Integer = 0
Dim width As Integer = CInt(720 / topics.Count) - 5
Dim diagram As GroupShape = New GroupShape(doc)
diagram.Left = 0
diagram.Top = 0
diagram.Width = 500
diagram.Height = 300
diagram.CoordSize = New Size(500, 100)
doc.FirstSection.Body.FirstParagraph.AppendChild(diagram)
For Each t As Topic In topics
……………………

I made those changes and they kind of help.

I am able to space the text boxes out, but it isn’t the consistent spacing that I had hoped for.

I guess that I am now having trouble understanding exactly how the coordinate system is supposed to work.

I tried setting

diagram.Width = 720
diagram.Height = 540
diagram.CoordSize = New Size(720, 540)

(along with some other slight changes) thinking that it would give me a spacing equivilant of Words 72pts/1" on a page with .5" margins.

However, this causes the textboxes to clump again. While playing around with it I set diagram.CoordSize = New Size(720, 270) and noticed that the spacing is improved. I also noticed that the value returned by shape.GetShapeRenderer().SizeInPoints.Height had doubled.

So, a couple questions:

  1. What value does shape.GetShapeRenderer().SizeInPoints.Height actually return? It clearly isn’t a ‘point’ value as it seems relative to the CoordSize settings.
  2. Why does the first textbox in a column have the same …SizeInPoints.Height value as the ones below it, even though the ones underneath are clearly larger?
  3. What exactly does the CoordSize property control? How does it relate to the diagram.Width and diagram.Height properties?
  4. Is there a way I can just have a 1:1 ratio for shape/groupshape settings? (i.e. .Top, .Bottom, .Width, .Height, etc)

Also, when I try to get the height of the first textbox, it returns .75, yet for all of the others it will return 15.149. Is this a bug?

I have attached an updated copy of my sample program.

Hi Charles,

Thanks for your inquiry. In your case, the ShapeRenderer.SizeInPoints.Height return incorrect value. I have logged this issue as WORDSNET-8438 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Charles,

Thanks for your patience.

It is to inform you that our development team has completed the work on the issue (WORDSNET-8438) and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug in Aspose.Words. So, we have closed this issue as ‘Not a Bug’.

Please call Document.UpdatePageLayout method after inserting the TextBox to get the required output. See the following highlighted code below.

Document doc = new Document();
double top = 10;
string text = "First testing with standard sized textboxes.";
for (int i = 0; i < 5; i++)
{
    Shape TextBox = new Shape(doc, ShapeType.TextBox);
    TextBox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    TextBox.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    TextBox.WrapType = WrapType.Square;
    // TextBox.Left = left;
    TextBox.Top = top;
    TextBox.Width = 320;
    TextBox.AppendChild(new Paragraph(doc));
    Paragraph para = TextBox.FirstParagraph;
    para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    Run run = new Run(doc);
    run.Font.Name = "Arial";
    run.Text = text;
    para.AppendChild(run);
    text += text;
    TextBox.TextBox.FitShapeToText = true;
    doc.FirstSection.Body.LastParagraph.AppendChild(TextBox);
    doc.UpdatePageLayout();
    ShapeRenderer r = TextBox.GetShapeRenderer();
    double newHeight = r.SizeInPoints.Height;
    TextBox.Height = newHeight;
    top = top + newHeight + 10;
    ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Emf)
    {
        Scale = 1.5f
    };
    // Save the rendered image to disk.
    r.Save(MyDir + "Out" + i + ".emf", imageOptions);
}
foreach (Shape shp in doc.GetChildNodes(NodeType.Shape, true))
{
    double height = shp.Height;
    MessageBox.Show(height.ToString());
}
doc.Save(MyDir + "Out.docx");

Did you actually test this on my sample code? When I try it the output document is completely blank.

Hi Charles,

Thanks for your inquiry. In your case, I suggest you please do not add shapes in GroupShape. Please check the following modified code. I have attached the modified console application and output document with his post for your kind reference.

Friend Function
GenerateChart() As Document
Dim doc As New Document
Const shapeSpacing As
Integer = 5
Dim topics As List(Of Topic) = GenerateTopicList()
Dim groupShapeHieght As
Integer = 0
Dim top As Integer = 74
Dim left As Integer = 0
Dim width As Integer = CInt(720 /
topics.Count) - 5

doc.FirstSection.PageSetup.Orientation = Orientation.Landscape
For Each t As Topic In topics
Dim topicTextBox As Shape = CreateTextBox(doc, t.Name, left, top,
width)

topicTextBox.StrokeColor = Color.Gray
'diagram.ChildNodes.Add(topicTextBox)

doc.FirstSection.Body.LastParagraph.AppendChild(topicTextBox)

doc.UpdatePageLayout()
Dim height As Integer = GetTextBoxHeight(t.Name, topicTextBox, doc)
Dim childLeft As Integer = left
Dim childTop As Integer = top + height + shapeSpacing
Dim sections As List(Of Section) = t.Sections
For Each s As Section In sections

Dim sectionTextBox As Shape =
CreateTextBox(doc, s.Name, childLeft, childTop, width)

'diagram.ChildNodes.Add(sectionTextBox)

doc.FirstSection.Body.LastParagraph.AppendChild(sectionTextBox)

doc.UpdatePageLayout()

childTop += GetTextBoxHeight(s.Name, sectionTextBox, doc) + shapeSpacing
Next

left += width + shapeSpacing
Next
Return doc
End Function
Public Function CreateTextBox(ByVal doc As Aspose.Words.Document, ByVal text As String, ByVal left As Double, ByVal top As Double, ByVal width As Double) As Shape
Dim TextBox As Shape = New Shape(doc, ShapeType.TextBox)
TextBox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
TextBox.RelativeVerticalPosition = RelativeVerticalPosition.Page
TextBox.Left = left
TextBox.Top = top
TextBox.Width = width
TextBox.WrapType = WrapType.Square
TextBox.ZOrder = 2
TextBox.TextBox.FitShapeToText = True
TextBox.AppendChild(New Paragraph(doc))
Dim para As Paragraph = TextBox.FirstParagraph
para.ParagraphFormat.Alignment = ParagraphAlignment.Center
Dim run As Run = New Run(doc)
run.Font.Name = "Arial"
run.Text = text
para.AppendChild(run)
para.AppendChild(New Run(doc, ControlChar.CrLf))
Return TextBox
End Function