Insert new Run with Formatting

Hello there,

I am inserting some text in a document at run-time by creating a new node of type Run as:

string imageText = GetImageText(ref builder);

Table objTable = new Table(builder.Document);
objTable.Rows.Add(new Row(builder.Document));
builder.CurrentParagraph.ParentNode.InsertAfter(objTable, builder.CurrentParagraph);
Cell objCell = new Cell(builder.Document);
PageSetup objPageSetup = builder.Document.Sections[0].PageSetup;
objCell.CellFormat.Width = objPageSetup.PageWidth - objPageSetup.LeftMargin - objPageSetup.RightMargin;
objCell.EnsureMinimum();
objTable.Rows[0].Cells.Add(objCell);

builder.MoveTo(objCell.ChildNodes[0]);
objShape = builder.InsertImage(objImage);
builder.InsertNode(new Run(builder.Document.Document, imageText));

But in the output document(attached), this new node text is formatted differently as compared to the document. (You will see this text on the left side of middle image.)

How can I insert it with formatting in line with the current document?

Thank you!

Thank you!

Hi Nutan,

Thanks for your request. Why do not you use builder.Write() method instead of builder.InsertNode(new Run(builder.Document.Document, imageText)). This will fix the problem with formatting.
Best regards,

Thanks a lot Alexey.

just replacing builder.Write() with builder.InsertNode() didn’t solve the problem.
However, when I called builder methods to insert new Table, Row, Cell, text… it solved the problem

string imageText = GetImageText(ref builder);
if (imageText != "")
{
    builder.StartTable();
    builder.InsertCell();
    PageSetup objPageSetup = builder.Document.Sections[0].PageSetup;
    builder.CellFormat.Width = objPageSetup.PageWidth - objPageSetup.LeftMargin - objPageSetup.RightMargin;
    objShape = builder.InsertImage(objImage);
    builder.Write(imageText);
    builder.EndRow();
    builder.EndTable();
}

Thank you!