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

Free Support Forum - aspose.com

How to make teh width of teh insrted text box in word document fit to the document using Aspose.Word

Hi Team,

We are inserting an textbox under certain text using the Style of paragraph.

We are adding some text to the text box and also we are defining the width and height of text box. Please refer the below code for details.

public static void insertTextBox(Aspose.Words.Document doc, string styleName, string documentType, string reqText)
    ArrayList paragraphsWithStyle = new ArrayList();
    // Get all paragraphs from the document.
    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

        foreach (Paragraph paragraph in paragraphs)
            if (paragraph.ParagraphFormat.Style.Name == styleName)
                DocumentBuilder reqTextBuilder = new DocumentBuilder(doc);
                Shape textBox = new Shape(reqTextBuilder.Document, ShapeType.TextBox);
                <b> textBox.Height = 10;
                textBox.Width = 450;</b>

                textBox.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
                textBox.WrapType = WrapType.TopBottom;
                textBox.AppendChild(new Paragraph(doc));
                textBox.TextBox.FitShapeToText = true;
                textBox.Stroke.LineStyle = ShapeLineStyle.Single;
                //textBox.Stroke.Weight = 6;
                textBox.Stroke.Color = Color.White;
                Paragraph para = textBox.FirstParagraph;

                if (documentType == "FA")
                    para.ParagraphFormat.Style.Font.Size = 9;
                if (documentType == "8D")
                    para.ParagraphFormat.Style.Font.Size = 9;
                if (documentType == "LSR")
                    para.ParagraphFormat.Style.Font.Size = 10;

                para.ParagraphFormat.Style.Font.Name = "Arial";

                // Add some text to the paragraph.
                Run run = new Run(doc);
                run.Text = Convert.ToString(reqText).Trim();

                if (Convert.ToString(reqText).Trim() != null && Convert.ToString(reqText).Trim() != "")
    catch (Exception ex)
        Logger.Write("Inside InsertTextBox methos CATCH block : " + ex.Message);


Here we are passing the “StyleName” present in the document and also the text.
We are using the below code to give the textbox height and width.

Shape textBox = new Shape(reqTextBuilder.Document, ShapeType.TextBox);
textBox.Height = 10;
textBox.Width = 450;

However by changing the width also, it os not affecting the width of text box in the generated word document.

Please refer the below screen shot for details.

We want that the text box should be fit to the word document width. Some think like the below image( we have manually changed the document for this image)

Could you please helps us on this to achieve the same?

Please revert in case of any issue.

Hi Rajesh,

Thanks for your inquiry. Please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Pleasecreate a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document
  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated

Hi Rajesh,

Can you try specifying width of textbox as follows and see how it goes on your end:

textBox.Width = doc.FirstSection.PageSetup.PageWidth - doc.FirstSection.PageSetup.LeftMargin -doc.FirstSection.PageSetup.RightMargin;

Also, please make sure that you’re using latest version of Aspose.Words i.e. 15.6.0.

Best regards,