We are using Aspose.Word for Net to generate fresh word DOCX file with rows from API/DB… When we generate the rows we use a function for Header cells, a function for content cell and anothe function to denote empty row message. The table looks aligned when there are rows present. But when there are no rows, the table looks unaligned(please see attached picture). .Even though I match the cellwidths to be same between the Content rows and the “Empty” row, the descrepancy exists.
AddAndConfigureHeaderCell(row, context, "Address Type", 50, ref cellToCloneFrom);
AddAndConfigureHeaderCell(row, context, "Address", 80, ref cellToCloneFrom);
AddAndConfigureHeaderCell(row, context, "City, State", 50, ref cellToCloneFrom);
AddAndConfigureHeaderCell(row, context, "Zip Code", 40, ref cellToCloneFrom);
Row cRow = null;
Cell cellToCloneFrom2 = null;
if (plant.HasOtherAddresses)
{
foreach (var address in addresses)
{
// create row
cRow = new Row(doc);
cRow.RowFormat.AllowBreakAcrossPages = false;
table.AppendChild(cRow);
string addressLinesText = address.AddressLine1;
if (!string.IsNullOrWhiteSpace(address.AddressLine2))
{
addressLinesText += ControlChar.LineBreak + address.AddressLine2;
}
string cityState = string.Join(", ", new List<string> { address.City, FsisCommonLookup.USStates.FindByKey(address.UsStateId.Value).AlphaCode2 });
string addressTypeS = (from a in FsisCommonLookup.AddressTypes.AsSelectListItems()
where (AddressType)(Convert.ToInt32(a.Value)) == address.AddressType
select new { a.Text }).Single().Text;
AddAndConfigureContentCell(cRow, context, addressTypeS, 50, ref cellToCloneFrom2);
AddAndConfigureContentCell(cRow, context, addressLinesText, 80, ref cellToCloneFrom2);
AddAndConfigureContentCell(cRow, context, cityState, 50, ref cellToCloneFrom2);
AddAndConfigureContentCell(cRow, context, address.PostalCode, 40, ref cellToCloneFrom2);
}
}
else
{
AddAndConfigureEmptyRow(table, context, 220, ref cellToCloneFrom2);
}
table.PreferredWidth = PreferredWidth.FromPercent(100);
table.RelativeHorizontalAlignment = HorizontalAlignment.Center;
private static void AddAndConfigureEmptyRow(Table table, ReportGeneratorContext context, int cellWidth, ref Cell cellToCloneFrom, string cellContent = "No records were found.")
{
var doc = (Document)table.Document;
var row = new Row(doc);
Paragraph para = new Paragraph(doc);
//para.ParagraphFormat.KeepWithNext = true;
row.RowFormat.AllowBreakAcrossPages = false;
table.AppendChild(row);
if (cellToCloneFrom == null)
{
cellToCloneFrom = new Cell(doc);
cellToCloneFrom.CellFormat.Width = cellWidth;
// Add a paragraph to the cell as well as a new run with some text.
cellToCloneFrom.AppendChild(para);
cellToCloneFrom.FirstParagraph.AppendChild(new Run(doc, cellContent));
//Formatting
cellToCloneFrom.FirstParagraph.Runs[0].Font.Color = context.ContentCellFormat.FontColor;
cellToCloneFrom.FirstParagraph.Runs[0].Font.Name = context.ContentCellFormat.FontName;
cellToCloneFrom.FirstParagraph.Runs[0].Font.Size = context.ContentCellFormat.FontSize;
cellToCloneFrom.FirstParagraph.Runs[0].Font.Italic = true;
if (context.ContentCellFormat.BackgroundColor.HasValue)
cellToCloneFrom.CellFormat.Shading.BackgroundPatternColor = context.ContentCellFormat.BackgroundColor.Value;
cellToCloneFrom.CellFormat.SetPaddings(context.ContentCellFormat.PaddingLeft, context.ContentCellFormat.PaddingTop, context.ContentCellFormat.PaddingRight, context.ContentCellFormat.PaddingBottom);
// Add the cell to the row.
row.AppendChild(cellToCloneFrom);
}
else
{
row.AppendChild(cellToCloneFrom.Clone(false));
row.LastCell.AppendChild(para);
row.LastCell.FirstParagraph.AppendChild(new Run(doc, cellContent));
row.LastCell.FirstParagraph.Runs[0].Font.Color = context.ContentCellFormat.FontColor;
row.LastCell.FirstParagraph.Runs[0].Font.Name = context.ContentCellFormat.FontName;
row.LastCell.FirstParagraph.Runs[0].Font.Size = context.ContentCellFormat.FontSize;
row.LastCell.FirstParagraph.Runs[0].Font.Italic = true;
}
}
/// <summary>
/// Add a new content cell to the given content row and configure it
/// </summary>
/// <param name="row"></param>
/// <param name="context"></param>
/// <param name="cellContent"></param>
/// <param name="cellWidth"></param>
/// <param name="cellToCloneFrom"></param>
private static void AddAndConfigureContentCell(Row row, ReportGeneratorContext context, string cellContent, int cellWidth, ref Cell cellToCloneFrom)
{
Document doc = (Document)row.Document;
Paragraph para = new Paragraph(doc);
if (cellToCloneFrom == null)
{
cellToCloneFrom = new Cell(doc);
cellToCloneFrom.CellFormat.Width = cellWidth;
// Add a paragraph to the cell as well as a new run with some text.
cellToCloneFrom.AppendChild(para);
cellToCloneFrom.FirstParagraph.AppendChild(new Run(doc, cellContent));
//Formatting
cellToCloneFrom.FirstParagraph.Runs[0].Font.Color = context.ContentCellFormat.FontColor;
cellToCloneFrom.FirstParagraph.Runs[0].Font.Name = context.ContentCellFormat.FontName;
cellToCloneFrom.FirstParagraph.Runs[0].Font.Size = context.ContentCellFormat.FontSize;
if (context.ContentCellFormat.BackgroundColor.HasValue)
cellToCloneFrom.CellFormat.Shading.BackgroundPatternColor = context.ContentCellFormat.BackgroundColor.Value;
cellToCloneFrom.CellFormat.SetPaddings(context.ContentCellFormat.PaddingLeft, context.ContentCellFormat.PaddingTop, context.ContentCellFormat.PaddingRight, context.ContentCellFormat.PaddingBottom);
// Add the cell to the row.
row.AppendChild(cellToCloneFrom);
}
else
{
row.AppendChild(cellToCloneFrom.Clone(false));
row.LastCell.AppendChild(para);
row.LastCell.FirstParagraph.AppendChild(new Run(doc, cellContent));
row.LastCell.FirstParagraph.Runs[0].Font.Color = context.ContentCellFormat.FontColor;
row.LastCell.FirstParagraph.Runs[0].Font.Name = context.ContentCellFormat.FontName;
row.LastCell.FirstParagraph.Runs[0].Font.Size = context.ContentCellFormat.FontSize;
}
}
image.png (4.4 KB)