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.