Latest Aspose.Words for .NET renders table rows containing only hidden text incorrectly

A customer uses Word templates with optional fields collapsed in hidden table rows.
This is an example document:
FD94794.docx (196.1 KB)
When you open it in Microsoft Word, the zaaknummer row immediately follows the datum row. When Aspose.Words renders the document as image or converts it to PDF, there is two rows whitespace between these two visible rows. We found this problem in Aspose.Words for .NET version 24.6 and it still exists in latest version 24.12. Version 23.5 still rendered the Word document correctly.

For the new Aspose.Words versions having this problem, I developed following correction code:

    /// If any table row is found having auto height and containing only hidden text,
    /// the row height is set to 0.
    /// </summary>
    /// <param name="doc">Aspose.Words document to investigate</param>
    /// <returns>true if any row height was changed</returns>
    public static bool CorrectHiddenTableRowHeights(Aspose.Words.Document doc)
    {
      int iCorrectedRows = 0;
      try
      {
        int iTableRows = 0;
        Stopwatch sw = new Stopwatch();
        sw.Start();
        foreach (Aspose.Words.Tables.Row row in doc.GetChildNodes(Aspose.Words.NodeType.Row, true))
        {
          iTableRows++;
          if ((row.RowFormat.HeightRule == Aspose.Words.HeightRule.Auto) && (row.RowFormat.Height == 0.0) && RowContainsNoOrOnlyHiddenText(row))
          {
            row.RowFormat.HeightRule = Aspose.Words.HeightRule.Exactly;
            iCorrectedRows++;
          }
        }
        sw.Stop();
        Trace.WriteLine(string.Format("CorrectHiddenTableRowHeights took {0:N0} ms to investigate {1:N0} table row(s) and corrected {2:N0} row(s).", 
          sw.ElapsedMilliseconds, iTableRows, iCorrectedRows));
      }
      catch (Exception ex)
      {
        Trace.Write(ex);
      }
      return iCorrectedRows > 0;
    }

    /// <summary>
    /// Returns true if the given row contains no text or only hidden text.
    /// </summary>
    private static bool RowContainsNoOrOnlyHiddenText(Aspose.Words.Tables.Row row)
    {
      try
      {
        foreach (Aspose.Words.Paragraph par in row.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
        {
          foreach (Aspose.Words.Run run in par.GetChildNodes(Aspose.Words.NodeType.Run, true))
          {
            if (!run.Font.Hidden)
              return false;
          }
        }
        return true;
      }
      catch (Exception ex)
      {
        Trace.Write(ex);
      }
      return false;
    }

After applying the correction, the Word document is again rendered correctly, but of course it would be better when Aspose.Words does so from the box.

@gertjaap

It seems that you are experiencing an issue with Aspose.Words for .NET where table rows containing only hidden text are rendered incorrectly, resulting in unwanted whitespace between visible rows. This problem has been identified in versions 24.6 and 24.12, while version 23.5 rendered the document correctly.

To address this issue, you can use the following correction code, which sets the height of any table row that has an automatic height and contains only hidden text to zero. This should help eliminate the extra whitespace when rendering the document:

/// <summary>
/// If any table row is found having auto height and containing only hidden text,
/// the row height is set to 0.
/// </summary>
/// <param name="doc">Aspose.Words document to investigate</param>
/// <returns>true if any row height was changed</returns>
public static bool CorrectHiddenTableRowHeights(Aspose.Words.Document doc)
{
    int iCorrectedRows = 0;
    try
    {
        int iTableRows = 0;
        Stopwatch sw = new Stopwatch();
        sw.Start();
        foreach (Aspose.Words.Tables.Row row in doc.GetChildNodes(Aspose.Words.NodeType.Row, true))
        {
            iTableRows++;
            if ((row.RowFormat.HeightRule == Aspose.Words.HeightRule.Auto) && (row.RowFormat.Height == 0.0) && RowContainsNoOrOnlyHiddenText(row))
            {
                row.RowFormat.HeightRule = Aspose.Words.HeightRule.Exactly;
                iCorrectedRows++;
            }
        }
        sw.Stop();
        Trace.WriteLine(string.Format("CorrectHiddenTableRowHeights took {0:N0} ms to investigate {1:N0} table row(s) and corrected {2:N0} row(s).", 
            sw.ElapsedMilliseconds, iTableRows, iCorrectedRows));
    }
    catch (Exception ex)
    {
        Trace.Write(ex);
    }
    return iCorrectedRows > 0;
}

/// <summary>
/// Returns true if the given row contains no text or only hidden text.
/// </summary>
private static bool RowContainsNoOrOnlyHiddenText(Aspose.Words.Tables.Row row)
{
    try
    {
        foreach (Aspose.Words.Paragraph par in row.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
        {
            foreach (Aspose.Words.Run run in par.GetChildNodes(Aspose.Words.NodeType.Run, true))
            {
                if (!run.Font.Hidden)
                    return false;
            }
        }
        return true;
    }
    catch (Exception ex)
    {
        Trace.Write(ex);
    }
    return false;
}

After applying this correction, the document should render correctly without the extra whitespace between the rows. However, it would be ideal for Aspose.Words to handle this issue natively in future releases.

If you have any further questions or need additional assistance, feel free to ask!

Even though there is a workaround, I would still like the bug in Aspose.Words to be logged and eventually resolved.

@gertjaap
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27695

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.