Saving with .DOC format does not display all columns in a dynamically created table

Using Aspose.Words for .NET, we are inserting a table and dynamically adding columns. In one scenario we add as many as 7 columns. We give the option to save the document in both PDF and DOC formats. When viewing a 7 column report in the PDF formatted file, everthing fits on one page and looks as intended. However, when opening the DOC formatted report, only 2 of the 7 columns in the table display. We found a work around by saving the file in RTF format but using a .DOC extension, however, we would like to know why the word formatted document doesnt come out consistently with respect to the PDF and RTF formats. This also works correctly when streaming the file into a .PNG format. Only the .doc format works incorrectly.

Hi

Thanks for your inquiry. Could you please attach sample document and provide me code that allows me to reproduce the problem on my side. I will investigate the issue and provide you more information.
Best regards.

private void Test()
{
    Document masterDoc = new Document("MasterDocument.doc");
    var builderOriginal = new DocumentBuilder(masterDoc);
    builderOriginal.MoveToBookmark("MasterBookmark");
    builderOriginal.Writeln();
    // Content of srcdoc will be inserted after this node
    Node insertAfterNode = builderOriginal.CurrentParagraph.PreviousSibling;
    var childCloneDoc = GetChildDocument().Clone();
    childCloneDoc.MailMerge.ExecuteWithRegions(GetChildDataTable());
    // We need to make sure that the specified node is either pargraph or table.
    if (insertAfterNode.NodeType != NodeType.Paragraph && insertAfterNode.NodeType != NodeType.Table)
        throw new ArgumentException("The destination node should be either paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(childCloneDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
    // ' Loop through all sections in the source document.
    foreach(Section srcSection in childCloneDoc.Sections)
    {
        // ' Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach(Node srcNode in srcSection.Body)
        {
            Paragraph para = srcNode as Paragraph;
            if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                break;
            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.ImportNode(srcNode, true);
            // Insert new node after the reference node.
            dstStory.InsertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
    masterDoc.Save("NewMaster.doc", SaveFormat.Coc, SaveType.OpenInWord, Response);
}
private Document GetChildDocument()
{
    DataTable childDataTable = GetChildDataTable();
    Document childDoc = new Document("ChildInsertTable.DOC");
    DocumentBuilder builder = new DocumentBuilder(childDoc);
    builder.MoveToBookmark("SomeBookMark");
    // Get table
    Table myTable = (Table) builder.CurrentNode.GetAncestor(NodeType.Table);
    // build header row
    foreach(DataColumn col in childDataTable.Columns)
    {
        Cell hCell = (Cell) myTable.FirstRow.FirstCell.Clone(true);
        myTable.FirstRow.AppendChild(hCell);
        hCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        builder.MoveTo(hCell.FirstParagraph);
        builder.Write(col.ColumnName);
    }
    var lastColumn = childDataTable.Columns[childDataTable.Columns.Count - 1];
    foreach(DataColumn col in childDataTable.Columns)
    {
        // Clone first cell of the second row to build header of the table
        Cell bCell = (Cell) myTable.Rows[1].FirstCell.Clone(true);
        // Insert cell into the second row
        myTable.Rows[1].AppendChild(bCell);
        // Move document builder cursor to the cell
        builder.MoveTo(bCell.FirstParagraph);
        // If it is first column we should insert tableStart mergefield - created the whole merge row
        bool isFirstColumn = col.Equals(childDataTable.Columns[0]);
        if (isFirstColumn)
        {
            bCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.InsertField(String.Format("MERGEFIELD \"TableStart:{0}\"", childDataTable.TableName), "");
        }
        // Insert mergefield
        if (isFirstColumn)
            builder.InsertField(String.Format("MERGEFIELD \"{0}\"", col.ColumnName), "");
        else
        {
            bCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            // #,##0
            builder.InsertField(String.Format("MERGEFIELD \"{0}\"", col.ColumnName), "");
        }
        // If column is last we should insert TableEnd
        if (col.Equals(lastColumn))
            builder.InsertField(String.Format("MERGEFIELD \"TableEnd:{0}\"", childDataTable.TableName), "");
    }
    // Here we remove first empty column
    myTable.Rows[0].Cells[0].Remove();
    myTable.Rows[1].Cells[0].Remove();
    return childDoc;
}
private DataTable GetChildDataTable()
{
    var childTable = new DataTable("childTable");
    childTable.Columns.Add(new DataColumn("Description", typeof(string)));
    for (int i = 0; i <7; i++)
        childTable.Columns.Add(new DataColumn(i.ToString(), typeof(double)));
    // Add data
    return childTable;
}

Hi

Thank you for additional information. Please try calling UpdateTableLayout before saving the document:

masterDoc.UpdateTableLayout();
masterDoc.Save(@"out.doc");

Hope this helps.
Best regards.