Michael
Michael

Reputation: 8749

Aspose C# DOCX ignoring table Cell width and height

I am trying to specify the width and height of my tables.

The ultimate goal is for the first column to take up about 90% and the last column to take the remaining 10%. I have tried many different combinations, but word seems to ignore it.

My Document Builder code is here:

        var table = docBuilder.StartTable();
        docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
        docBuilder.CellFormat.ClearFormatting();
        docBuilder.Font.Bold = true;
        docBuilder.Font.Name = Stylings.TITLEFONT;
        docBuilder.Font.Size = Stylings.TITLESIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        docBuilder.Write("Description");

        var cell = docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.CellFormat.FitText = false;            
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        docBuilder.Font.Name = Stylings.TITLEFONT;
        docBuilder.Font.Size = Stylings.TITLESIZE1;
        docBuilder.Font.Bold = true;
        docBuilder.Write("Amount (inc GST)");
        docBuilder.EndRow();

        docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(description);

        docBuilder.InsertCell();
        docBuilder.RowFormat.HeightRule = HeightRule.AtLeast;
        docBuilder.RowFormat.Height = 5;
        docBuilder.CellFormat.FitText = false;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(total.ToString("C"));
        docBuilder.EndRow();

        docBuilder.InsertCell();
        docBuilder.RowFormat.HeightRule = HeightRule.Auto;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);

        docBuilder.InsertCell();
        docBuilder.CellFormat.FitText = false;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(total.ToString("C"));
        docBuilder.EndRow();

        //Table Formatting
        table.AutoFit(AutoFitBehavior.FixedColumnWidths);
        table.PreferredWidth = PreferredWidth.FromPercent(100);
        docBuilder.EndTable();

Upvotes: 0

Views: 1406

Answers (1)

Tilal Ahmad
Tilal Ahmad

Reputation: 939

Please use following sample code snippet to create table with different relative sized cells.

I work with Aspose as developer evangelist.

// Insert a table row made up two cells which have different preferred widths.
Table table = builder.StartTable();

// Insert a relative ( 90 percent) sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
builder.CellFormat.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "Arial";
builder.Font.Size = 10;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("Description");

// Insert a relative ( 10 percent) sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
builder.CellFormat.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "Arial";
builder.Font.Size = 10;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("Amount (inc GST)");
builder.EndRow();

// Insert a relative ( 90 percent) sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
builder.CellFormat.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "Arial";
builder.Font.Size = 10;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("Aspose.Words for .NET 17.10");

// Insert a relative ( 10 percent) sized cell.
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
builder.CellFormat.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "Arial";
builder.Font.Size = 10;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("1000.00");
builder.EndRow();

Upvotes: 2

Related Questions