InsertHtml table formatting problem with Word 2007

We are having a problem with using InsertHtml() to insert a <table> into a Word 2003 document that we need to be able to open in both Word 2003 and 2007. The table looks fine when opened in Word 2003, but when opened in Word 2007, a lot of extra padding is added above and below the text in each row. After opening the document, Word 2007 does “auto-fit” of the contents and then it looks correct. However, we need the document to have read-only protection, and this setting prevents Word 2007 from doing its “auto-fit” of the contents. We have tried changing the save type to docx with the same results. I have attached the source HTML and the resulting doc file. We are using Aspose.Words 6.4.0.0.

Hi

Thanks for your request. It seems, the problem occurs because there is colspan in some cells in your table. When you convert HTML to DOC and there is table with rowspan or colspan Aspose.Words represents them as merged cells, but each of merged cell contains the same content. I linked your request to the appropriate issue. You will be notified as soon as it is resolved.
As a workaround, you can try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert HTML
string html = File.ReadAllText(@"Test202\tableProblem.htm");
builder.InsertHtml(html);
// Remove content from merged cells
// Get collection of cells in the docuemnt
NodeCollection cells = doc.GetChildNodes(NodeType.Cell, true);
foreach(Cell cell in cells)
{
    // Check whether cell is merged with previouse
    if (cell.CellFormat.HorizontalMerge == CellMerge.Previous ||
        cell.CellFormat.VerticalMerge == CellMerge.Previous)
    {
        // Remove content from the cell
        cell.RemoveAllChildren();
    }
}
// Save output docuemnt
doc.Save(@"Test202\out.doc");

Hope this helps.
Best regards.

Hi, thanks for the quick reply…
The workaround worked for the standalone test document, but when used on our production document (which has the table embedded in an enclosing table cell) it still left a few points of space above and below the text in the rows, which was enough to cause it not to fit in the enclosing table cell. We had to deploy the solution yesterday, so I did not have time to further investigate.
Thanks

Hi

It is strange, because the problem does not occur on my side at all. On my side the issue occurs only if I convert the document to PDF.
Best regards.

Hi,

I have a similar problem. I add the table as HTML (with many rowspan and colspan). I attach a sample file. Following the recommendations I add a table in the following way:

Builder.InsertHtml(sTable);
Table table = Builder.CurrentStory.Tables[Builder.CurrentStory.Tables.Count - 1];
if (table != null)
{
    ReplaceCellsSpacesWhenValueIsNumeric(table);
}

-----

public static void ReplaceCellsSpacesWhenValueIsNumeric(Table table)
{
    foreach(Row r in table.Rows)
    {
        foreach(Cell c in r.Cells)
        {
            //Check whether cell is merged with previouse
            if (c.CellFormat.HorizontalMerge == CellMerge.Previous ||
                c.CellFormat.VerticalMerge == CellMerge.Previous)
            {
                //Remove content from the cell
                c.RemoveAllChildren();
            }

            if (c.CellFormat.HorizontalMerge != CellMerge.Previous &&
                c.CellFormat.VerticalMerge != CellMerge.Previous)
            {
                if (c.Paragraphs.Count> 0)
                {
                    foreach(Paragraph p in c.Paragraphs)
                    {
                        foreach(Run run in p.Runs)
                        {
                            if (run.Text.Contains(HARD_SPACE_REPLACEMENT))
                            {
                                run.Text = run.Text.Replace(HARD_SPACE_REPLACEMENT, HARD_SPACE);
                            }
                        }
                    }
                }
            }
        }
    }
}

Unfortunately, the effect is far from the expected

XPS:

PDF:

DOCX:

Best regards,

Krzysztof Radzimski
Administration And Business Consulting

Hi

Thanks for your request. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed.
Best regards,

Hi,

Is version 9.1 the problem is not resolved?
InsertTable.html - Sample table.

Hi

Thanks for your request. Unfortunately, the issue is still unresolved. You will be notified as soon as it is fixed.

Best regards,

Hi,

I have version 9.7, but the problem still not resolved (from version 9.3).
Please see the sample program (in attachments)

  • test.zip - source html file
  • debug.zip - binaries
  • Screen1.png - generated with aspose.word docx file
  • screen2.png - source html file

Aspose.Words does not support pasting HTML tables with merged rows.

Sample code

class Program
{
    static void Main(string[] args)
    {
        License lic = new License();
        lic.SetLicense(new MemoryStream(Properties.Resources.Aspose_Total));

        DocumentBuilder b = new DocumentBuilder(new Document());

        b.Document.Sections[0].PageSetup.Orientation = Orientation.Landscape;

        b.InsertHtml(Properties.Resources.test);

        Table table = b.CurrentStory.Tables[b.CurrentStory.Tables.Count - 1];
        if (table != null)
        {
            RepairTable(table);
        }
        string fileName = Path.Combine(Path.GetTempPath(), "test.docx");

        b.Document.Save(fileName, SaveFormat.Docx);

        Process.Start(fileName);
    }

    static void RepairTable(Table table)
    {
        foreach(Row r in table.Rows)
        {
            foreach(Cell c in r.Cells)
            {
                // Check whether cell is merged with previouse
                if (c.CellFormat.HorizontalMerge == CellMerge.Previous ||
                    c.CellFormat.VerticalMerge == CellMerge.Previous)
                {
                    // Remove content from the cell
                    c.RemoveAllChildren();
                }
            }
        }
    }

Hello

Thank you for additional information. Yes you are right, this problem is still unresolved. We will be sure to inform you of any developments regarding this problem.
Best regards,

Hi,

I know I’m right. But I have more than 3,000 Polish local government sector clients. Question - when this problem will be solved?

Hello

Thanks for your inquiry. Unfortunately, it is difficult to provide you any reliable estimate regarding this problem at the moment. Once our developers review the issue and provide an estimate, I will let you know.
Best regards,

Hi,

how to create such a table using the API?
This might be a solution for me

Hi

Thanks for your inquiry. Please follow the link to learn how to create table using DocumentBuilder:
https://docs.aspose.com/words/net/programming-with-documents/
Best regards,

Hi,

The question is whether such a complex table can be created using the API? In this example, there is no information about merging cells. Ask for more information about this topic.

Hi

Thanks for your request. Please see the following links:
HorizontalMerge
https://reference.aspose.com/words/net/aspose.words.tables/cellformat/horizontalmerge/
VerticalMerge
https://reference.aspose.com/words/net/aspose.words.tables/cellformat/verticalmerge/
Best regards,

The issues you have found earlier (filed as WORDSNET-1739) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)

The issues you have found earlier (filed as WORDSNET-3466) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.