You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Blazor中使用QuestPDF如何垂直居中明细行文本?

QuestPDF明细行文本垂直居中问题解决

针对你在Blazor中使用QuestPDF时,明细行文本调用.AlignMiddle()无法垂直居中的问题,可通过以下两种方式修复:

方法一:直接设置单元格垂直对齐

QuestPDF的Cell组件提供了VerticalAlignment属性,直接指定该属性为Middle即可实现内容垂直居中,代码如下:

foreach (var detail in LstGenDetails)
{
    table.Cell()
        .VerticalAlignment(VerticalAlignment.Middle)
        .PaddingVertical(15)
        .BorderBottom(1)
        .BorderColor(Colors.Grey.Lighten2)
        .Text(detail.WasteClass);
}

方法二:在容器内确保内容对齐

如果需要保留Element容器的写法,需确保容器内的文本也纳入对齐上下文,调整代码如下:

foreach (var detail in LstGenDetails)
{
    table.Cell().Element(container =>
        container.PaddingVertical(15)
            .BorderBottom(1)
            .BorderColor(Colors.Grey.Lighten2)
            .AlignMiddle()
            .Text(detail.WasteClass)
    );
}

原因说明

原代码中.AlignMiddle()仅作用于容器本身,但文本元素未被纳入容器的对齐规则范围内;将Text方法移至容器配置链中,或直接使用Cell的VerticalAlignment属性,能确保内容继承垂直居中的设置。

内容的提问来源于stack exchange,提问作者Axium7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:52:36