Positioning multiple FitShapeToText TextBoxes

I’m creating several TextBox shapes in a document. If I explicitly set the Height of the first TextBox then I know what Top value to give the second TextBox so that they show with a gap between them.
Unfortunately in my case the text content of each textbox is different so I need to use FitShapeToText so that all the text shows. But if I do this I no longer know the Height of the first TextBox and therefore do not know what Top value to give the second TextBox.
Is there a way of adding the TextBoxes so that they will flow, one after the other? At the moment they all have the same Top value and just stack on top of each other.

This message was posted using Aspose.Live 2 Forum

Hi

Thanks for your request. Maybe you should just use TopBottom wrap type as shown in the code below:

// Create and empty document and DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert one more paragrpah into the document.
builder.Writeln();
// Move cursor to the beginning of thedocument.
builder.MoveToDocumentStart();
// Create textbox.
Shape textbox = new Shape(doc, ShapeType.TextBox);
textbox.Width = 100;
textbox.TextBox.FitShapeToText = true;
// Make textbox TopBottom, so if we insert another textbox
// into the next paragpaph, it will be always below this textbox.
textbox.WrapType = WrapType.TopBottom;
// Create paragraph and insert it into the textbox.
Paragraph par = new Paragraph(doc);
textbox.AppendChild(par);
// Insert textbox into the document.
builder.InsertNode(textbox);
// Insert some text into the textbox.
builder.MoveTo(textbox.FirstParagraph);
builder.Writeln("This is some text in this textbox");
builder.Write("This is some more text");
// Move cursor to the main content and insert one more textbox.
builder.MoveToDocumentEnd();
Shape textbox1 = (Shape) textbox.Clone(true);
builder.InsertNode(textbox1);
builder.MoveTo(textbox1.FirstParagraph);
builder.Writeln("This is some new text in the second textbox");
doc.Save(@"Test001\out.doc");

Best regards.

Thank you Alexy for such a quick response.

Your code works for two, but how do I extend it code to work with 3, 4 etc text boxes. I tried adding…

builder.MoveToDocumentEnd();
Shape textbox2 = (Shape) textbox.Clone(true);
builder.InsertNode(textbox2);
builder.MoveTo(textbox2.FirstParagraph);
builder.Writeln("This is some new text in the third textbox");

… but get an exception with Unexpected subdocument type.

The layout we actually need is for the document to have two columns of a variable number of textboxes. ie display ten textboxes one after the other on the left hand side of the page, and then nine one after each other on the right hand side of the page.

Thanks for your help

Hi

Thanks for your request. You can try using code like the following to insert few textboxes:

// Create and empty document and DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create textbox.
Shape textbox = new Shape(doc, ShapeType.TextBox);
textbox.Width = 100;
textbox.TextBox.FitShapeToText = true;
// Make textbox TopBottom, so if we insert another textbox
// into the next paragpaph, it will be always below this textbox.
textbox.WrapType = WrapType.TopBottom;
// Create paragraph and insert it into the textbox.
Paragraph par = new Paragraph(doc);
textbox.AppendChild(par);
// Create another paragraph and insert textbox into it.
Paragraph mainPar = new Paragraph(doc);
mainPar.AppendChild(textbox);
Paragraph refParagraph = builder.CurrentParagraph;
for (int i = 0; i <5; i++)
{
    // Insert paragraph with textbox into the document.
    Paragraph mainPar1 = (Paragraph) mainPar.Clone(true);
    refParagraph.ParentNode.InsertAfter(mainPar1, refParagraph);
    refParagraph = mainPar1;
    // Insert some text into the textbox.
    builder.MoveTo(((Shape) mainPar1.FirstChild).FirstParagraph);
    builder.Writeln(i.ToString());
    builder.Writeln("This is some text in this textbox");
    builder.Write("This is some more text");
}
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards.