I want to replace all text boxes in my rtf document with tables. The text boxes are inserted using MS Word. I have tried the following code in C#:
Document rtfFile = new Document(@"C:\Tools\docwithTextBox.rtf");
DocumentBuilder builder = new DocumentBuilder(rtfFile);
var nodes = rtfFile.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
if (shape.ShapeType == ShapeType.TextBox)
{
var width = shape.Width;
var height = shape.Height;
string text = shape.GetText();
builder.MoveTo(shape);
shape.Remove();
Aspose.Words.Tables.Table table = builder.StartTable();
Cell cell = builder.InsertCell();
builder.Write(text);
builder.RowFormat.Height = height;
builder.RowFormat.HeightRule = HeightRule.Exactly;
table.PreferredWidth = PreferredWidth.FromPoints(width);
builder.EndRow();
builder.EndTable();
}
}
rtfFile.Save(@"C:\Tools\docwithTextBox.rtf");
I am facing the following issues with the above approach:
The tables are not added at the position of the corresponding text boxes. Though I am calling
builder.MoveTo()theleftvalue of the new table doesn't match that of the textbox.The height of some of the tables doesn't match with the height of the corresponding
shape(TextBox. The width is correctly preserved.The string returned by the
shape.GetText()method doesn't preserve the formatting. For example, even if the text inside the textbox is bold or italic, theshape.GetText()method returns an unformatted text. How do we preserve the formatting before inserting into the table?
Please let me know how can I fix this issue. Any help will be highly appreciated.
Thanks,