Creating tables with styling

Having some problems converting to a pdf and keeping styling inside tables. I’m trying to create boxes that have vertically and horizontally centered text, but here is what I’m getting.

Relevant code:

Table builderTable = builder.StartTable();

builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.RowFormat.Height = tempVariable.AdvancedTable.RowHeight;
Cell cell = builder.InsertCell();
builder.InsertField($"MERGEFIELD TableStart:{tempVariable.VariableName}");
this.SetCellStyling(tempVariable.AdvancedTable.Rows.First().Cells.First(), cell, builder);
for (var columnIndex = 0; columnIndex < columnNames.Length; columnIndex++)
{
    builder.InsertField($"MERGEFIELD {columnNames[columnIndex]}");
    string lastItem = columnNames[columnNames.Length - 1];
    if (columnNames[columnIndex] != lastItem)
    {
        cell = builder.InsertCell();
        this.SetCellStyling(tempVariable.AdvancedTable.Rows.First().Cells.Skip(columnIndex + 1).First(), cell, builder);
    }
}

builderTable.AllowAutoFit = false;
builderTable.PreferredWidth = PreferredWidth.FromPercent(tempVariable.AdvancedTable.BuilderTablePreferredWidth);
builderTable.Alignment = TableAlignment.Right;
builderTable.TextWrapping = TextWrapping.Around;
builder.InsertField($"MERGEFIELD TableEnd:{tempVariable.VariableName}");
builder.EndTable();
ds.Tables.Add(table);
private void SetCellStyling(AdvancedCell cell, Cell asposeCell, DocumentBuilder builder)
{
    if (cell.HorizontalTextAlignment != HorizontalTextAlignmentType.None)
    {
        builder.ParagraphFormat.Alignment = this.GetHorizontalStyle(cell.HorizontalTextAlignment);
    }

    if (cell.VerticalTextAlignment != VerticalTextAlignmentType.None)
    {
        asposeCell.CellFormat.VerticalAlignment = this.GetVerticalStyle(cell.VerticalTextAlignment);
    }

    this.SetBorder(cell, asposeCell);
}

private ParagraphAlignment GetHorizontalStyle(HorizontalTextAlignmentType alignment)
{
    switch (alignment)
    {
        case HorizontalTextAlignmentType.Left: return ParagraphAlignment.Left;
        case HorizontalTextAlignmentType.Right: return ParagraphAlignment.Right;
        case HorizontalTextAlignmentType.Center: return ParagraphAlignment.Center;
    }

    return ParagraphAlignment.Left;
}

I’ve verified that the variables passed in have the correct styling setting passed in and set in Aspose.
Any ideas on why it’s not vertically or horizontally centered inside the table?

@tbhorton Unfortunately, I cannot reproduce the problem on my side. Since I do not have you data I have simplified the code like the following for testing:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table builderTable = builder.StartTable();

builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.RowFormat.Height = 100;
Cell cell = builder.InsertCell();
SetCellStyling(cell, builder);
for (var columnIndex = 0; columnIndex < 5; columnIndex++)
{
    builder.Write(columnIndex.ToString());
    cell = builder.InsertCell();
    this.SetCellStyling(cell, builder);
}
builder.Write("test");
builder.EndRow();
builder.EndTable();
builderTable.AllowAutoFit = false;
builderTable.Alignment = TableAlignment.Right;
builderTable.TextWrapping = TextWrapping.Around;

doc.Save(@"C:\Temp\out.docx");
private void SetCellStyling(Cell asposeCell, DocumentBuilder builder)
{
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    asposeCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
}

As a result I see that both vertical and horizontal alignment is set properly: out.docx (7.3 KB)

Could you please save the document as DOCX and attach it here for our reference? We will check it and provide you more information.

Hi, thanks for the quick reply. Attaching parsed template. We have now hardcoded the cells to all be centered horizontally. Vertically was looking fairly close to centered. It’s the boxes about mid way down on the right where it says

Or
Employer Identification Number

ParsedTemplate.docx (103.8 KB)

@tbhorton Please try resetting space before and first line indent of the paragraph to zero.

Ah, that was it. We had a problem with the indentation in the template which was pushing the text over to the right. And we will change the spacing and use vertical alignment of the table instead.

Thanks so much for your help!

1 Like