Free Support Forum -

Word - Text Box Internal Margin

We are attempting to manipulate a word document using Aspose.Words which requires the ability to programmatically inspect the values of the internal margins set on a text box.

A quick look at the document’s xml suggests that these two values map to the mso-wrap-distance-left mso-wrap-distance-right properties found in the shape tag.

Is there any way to access these values using Aspose.Words?

Hi Robert,

Thanks for your inquiry. The TextBox class defines attributes that specify how a text is displayed inside a shape. You can use the TextBox property to access text properties of a shape. Pleas read the members of TextBox class like InternalMarginXXX from here:

Following code example creates a textbox with some text and different formatting options in a new document. Hope this helps you.

// Create a blank document.

Document doc = new Document();<o:p></o:p>

// Create a new shape of type TextBox<o:p></o:p>

Shape textBox = new Shape(doc, ShapeType.TextBox);<o:p></o:p>

// Set some settings of the textbox itself.<o:p></o:p>

// Set the wrap of the textbox to inline<o:p></o:p>

textBox.WrapType = WrapType.None;<o:p></o:p>

// Set the horizontal and vertical alignment of the text inside the shape.<o:p></o:p>

textBox.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;<o:p></o:p>

textBox.VerticalAlignment = VerticalAlignment.Top;<o:p></o:p>

// Set the textbox height and width.<o:p></o:p>

textBox.Height = 50;<o:p></o:p>

textBox.Width = 200;<o:p></o:p>

textBox.TextBox.InternalMarginLeft = 0.0;<o:p></o:p>

textBox.TextBox.InternalMarginTop = 0.0;<o:p></o:p>

// Set the textbox in front of other shapes with a lower ZOrder<o:p></o:p>

textBox.ZOrder = 2;<o:p></o:p>

// Let’s create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.<o:p></o:p>

textBox.AppendChild(new Paragraph(doc));<o:p></o:p>

Paragraph para = textBox.FirstParagraph;<o:p></o:p>

para.ParagraphFormat.Alignment = ParagraphAlignment.Left;<o:p></o:p>

// Add some text to the paragraph.<o:p></o:p>

Run run = new Run(doc);<o:p></o:p>

run.Text = “Content in textbox”;<o:p></o:p>


// Append the textbox to the first paragraph in the body.<o:p></o:p>


// Save the output<o:p></o:p>

doc.Save(MyDir + “Shape.CreateTextBox Out.doc”);<o:p></o:p>

If this does not help you, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will then provide you more information along with code.