Aspose C#DOCX忽略表格单元格的宽度和高度

时间:2017-11-07 15:06:48

标签: aspose.words

我正在尝试指定表格的宽度和高度。

最终目标是第一列占据约90%,最后一列占据剩下的10%。 我尝试了很多不同的组合,但是单词似乎忽略了它。

我的文档构建器代码在这里:

        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();

1 个答案:

答案 0 :(得分:2)

请使用以下示例代码段创建包含不同相对大小的单元格的表格。

我与Aspose一起担任开发人员传播者。

// 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();