How to make the allignment of the insrted text box in word document allign to the paragraph in document using Aspose.Word

Hi Team,
We are inserting a 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);
try
{
foreach (Paragraph paragraph in paragraphs)
{
if (paragraph.ParagraphFormat.Style.Name == styleName)
{
DocumentBuilder reqTextBuilder = new DocumentBuilder(doc);

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

textBox.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
textBox.WrapType = WrapType.TopBottom;
textBox.AppendChild(new Paragraph(doc));
textBox.TextBox.FitShapeToText = true;
textBox.Stroke.LineStyle = ShapeLineStyle.Single;

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

para.AppendChild(run);

if (Convert.ToString(reqText).Trim() != null && Convert.ToString(reqText).Trim() != “”)
{
reqTextBuilder.InsertParagraph();
reqTextBuilder.InsertNode(textBox);
}
break;
}
}
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 = doc.FirstSection.PageSetup.PageWidth -
doc.FirstSection.PageSetup.LeftMargin -
doc.FirstSection.PageSetup.RightMargin;

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

Please refer the below screen shot for details.

image 1.png (2.9 KB)

We want that the text in the text box should be align to the the paragraph. Please see the below image for details.

image 2.png (2.2 KB)

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

Please revert in case of any issue.
Regards,
Rajesh

Hi Rajesh,

Thanks for your inquiry. It would be great if you please share following detail for our reference. We will then provide you more information about your query along with code.


  • Please attach your input Word document.
  • Please
    share 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
    like.

Hi Tahir,

Thanks for looking in to this.

PFA for the small web application where “Generate Report” button will generate the report.

We are getting the generated word document as below.


Where we need the word document text box like this.



Hope this will help you.

Looking forward to hear from you soon.

Please revert in case of any issue.
Regards,
Rajesh

Hi Rajesh,

Thanks for sharing the detail. Please use TextBox.InternalMarginLeft property to specify the inner left margin in points for a shape. By setting the value of this property to 0.0, you will get the required output. Please check the following highlighted code snippet. I have attached the output document with this post for your kind reference.

Shape textBox = new Shape(reqTextBuilder.Document, ShapeType.TextBox);
textBox.Height = 10;
// textBox.Width = 450;
textBox.Width = doc.FirstSection.PageSetup.PageWidth - doc.FirstSection.PageSetup.LeftMargin - doc.FirstSection.PageSetup.RightMargin;
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;
textBox.TextBox.InternalMarginLeft = 0.0;
textBox.TextBox.InternalMarginRight = 0.0;