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