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

Free Support Forum - aspose.com

Add table and textbox with border

Hi,

In word docuemnt, Ive to create table like the one in the attachment. How to create textbox in Aspose words?

I've tried

// Create textbox shape.

Shape textbox = new Shape(doc, ShapeType.TextBox);

textbox.Width = 300;

textbox.Height = 300;

but the textbox is not displaying borders. How to add borders?

Thanks

Hi,

I got the following code that adds border to shape (textbox) only if I directly add textbox to word document, but I am adding shape to table cell, then it is not displaying shape textbox in that table cell. What am I doing wrong?

Shape textBox = new Shape(doc, ShapeType.TextBox);
textBox.Width = 200;
textBox.Height = 150;

textBox.Stroke.LineStyle = ShapeLineStyle.Single;
textBox.Stroke.Weight = 6;
textBox.Stroke.Color = Color.Black;

//Create Paragraph and run
Paragraph cell2Paragraph = new Paragraph(doc);
cell2Paragraph.AppendChild(new Run(doc, "Data"));

//Insert Paragraph into the TextBox
textBox.AppendChild(cell2Paragraph);

Cell dataCell = new Cell(doc);

dataCell.AppendChild(cell2Paragraph);

row.AppendChild(dataCell);

Row is of type TableRow. When I generate word document with the above code, it's not generating textbox with borderv

Hi,


Thanks for your inquiry. You need to create a new Paragraph in the newly created Cell and then append your Shape to this Paragraph as follows:

Shape textBox = new Shape(doc, ShapeType.TextBox);

textBox.Width = 200;

textBox.Height = 150;

textBox.Stroke.LineStyle = ShapeLineStyle.Single;

textBox.Stroke.Weight = 6;

textBox.Stroke.Color = Color.Black;

//Create Paragraph and run

Paragraph cell2Paragraph = new Paragraph(doc);

cell2Paragraph.AppendChild(new Run(doc, "Data"));

//Insert Paragraph into the TextBox

textBox.AppendChild(cell2Paragraph);

Cell dataCell = new Cell(doc);

dataCell.Paragraphs.Add(new Paragraph(doc));

dataCell.FirstParagraph.AppendChild(textBox);


I hope, this helps.

Best Regards,

Thanks, Thats working fine but how to add text in that textbox?

Suppose if I entered long text into that textbox, it is not automatically increasing its size and not allowing me to insert text?

Hi,


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();

DocumentBuilder builder = new DocumentBuilder(doc);

Shape textBox = new Shape(doc, ShapeType.TextBox);

// You must have to specify the width

textBox.Width = 200;

textBox.TextBox.FitShapeToText = true;

Paragraph para = new Paragraph(doc);

para.Runs.Add(new Run(doc, "Long text goes here Long text goes here Long text goes here Long text goes here Long text goes here Long text goes here ..."));

textBox.AppendChild(para);

doc.FirstSection.Body.FirstParagraph.AppendChild(textBox);

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best Regards,