0
votes

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:

  1. The tables are not added at the position of the corresponding text boxes. Though I am calling builder.MoveTo() the left value of the new table doesn't match that of the textbox.

  2. The height of some of the tables doesn't match with the height of the corresponding shape(TextBox. The width is correctly preserved.

  3. 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, the shape.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,

1
Please ZIP and Upload your 1) input RTF file 2) Aspose.Words 18.6 generated output file showing the undesired behavior, 3) expected document showing the correct output (you can use MS Word to create expected document) and 4) simple console application (source code without compilation errors) to Dropbox and share the download link here for testing. I work with Aspose as Developer Evangelist. - Awais Hafeez
We are checking this scenario and will get back to you soon. - Awais Hafeez
For "nonNestedTexBox.rtf" document, you can build on this code to meet your requirement. - Awais Hafeez

1 Answers

0
votes

You can use the following code to replace TextBox shapes in Word document with Tables:

Document doc = new Document("D:\\Temp\\files\\Files\\1\\nonNestedTexBox.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList list = new ArrayList();
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    builder.MoveTo(shape.GetAncestor(NodeType.Paragraph));

    Table tab = builder.StartTable();
    Cell cell = builder.InsertCell();
    cell.EnsureMinimum();

    foreach (Node node in shape.ChildNodes)
    {
        cell.AppendChild(node);
    }

    cell.FirstParagraph.Remove();

    builder.EndRow();
    builder.EndTable();

    tab.TextWrapping = TextWrapping.Around;
    tab.LeftIndent = shape.Left + 10;
    tab.FirstRow.FirstCell.CellFormat.Width = shape.Width;
    tab.FirstRow.RowFormat.Height = shape.Height;

    tab.AutoFit(AutoFitBehavior.FixedColumnWidths);

    list.Add(shape);
}

foreach (Shape s in list)
{
    s.Remove();
}

doc.Save("D:\\Temp\\files\\Files\\1\\18.6.docx");

I work with Aspose as Developer Evangelist.