在使用itextsharp创建的PDF表中设置单元格高度 [英] Set cell height in PDF table created with itextsharp

查看:329
本文介绍了在使用itextsharp创建的PDF表中设置单元格高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在我的itextsharp PDF文档中创建一个表:

I used a following code to create a table in my itextsharp PDF document:

foreach (var subComp in competency.SubCompetensies)
{
    cell = new PdfPCell(new Phrase(0, subComp.DescriptionMin, _nfDescr));
    cell.Padding = 5;
    cell.Rowspan = 2;

    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(0, subComp.Name, _nfSubComp));
    cell.Colspan = 10;
    cell.Padding = 5;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(subComp.DescriptionMax, _nfDescr));
    cell.Padding = 5;
    cell.Rowspan = 2;

    table.AddCell(cell);

    for (int i = 1; i < 11; i++)
    {
        cell = new PdfPCell(new Phrase(0, i.ToString(), _nfScale));
        cell.FixedHeight = 15f;
        cell.Padding = 3;
        cell.PaddingLeft = 5;
        table.AddCell(cell);
    }
}

结果如下:

正如您所看到的,每行中具有数字的单元格的高度是不同的。似乎忽略了cell.FixedHeight属性。

As you can see height of cells with numbers is different in every row. It seems that cell.FixedHeight property is ignored.

有没有办法设置这些单元格的固定高度?

Is there any way to set fixed height of these cells?

推荐答案

好吧,我找到了解决方案。至于我,它似乎很小变态,但我的截止日期并不关心我的代码是多么精致。

Well, I found a solution. As for me, it seems little pervert, but my deadline doesn't care about how exquisite my code is.

可能会有人觉得它很有用。

May be somene will find it useful.

为什么要忽略FixedHeight?

因为最后绘制数字的单元格会消耗所有连续的自由空间。

Because the cells with the numbers drawn last they consume all the free space in a row.

可能的解决方案

我只能看到两个方式:


  • 显然,连续更改单元格的顺序

  • 渲染中心部分我自己划船

我决定选择第二种方式。
我没有使用rowspans / colspans创建十三个单元格并将它们添加到一行,而是只添加三个单元格:

I desided to choose the second way. Instead of creating therteen cells with rowspans/colspans and adding them to one row, I add only three cells:

 cell = new PdfPCell(new Phrase(0, subComp.DescriptionMin, _nfDescr));
                            cell.MinimumHeight = 50;
                            cell.Padding = 5;
                            cell.BorderColor = BaseColor.WHITE;
                            cell.BackgroundColor = new BaseColor(233, 240, 242);
                            table.AddCell(cell);

                            cell = new PdfPCell();
                            cell.CellEvent = new CellEvents(subComp);
                            cell.BorderColor = BaseColor.WHITE;
                            table.AddCell(cell);

                            cell = new PdfPCell(new Phrase(subComp.DescriptionMax, _nfDescr));
                            cell.Padding = 5;
                            cell.BorderColor = BaseColor.WHITE;
                            cell.BackgroundColor = new BaseColor(233, 240, 242);
                            table.AddCell(cell);

我将自定义单元格事件添加到第二次出售。在设置单元格高度之后和布局渲染之前触发它。以下是事件处理程序的代码:

I add custom cell event to the second sell. It is fired after the cell height is set and before layout rendering. Here is the code of event handler:

 private class CellEvents : IPdfPCellEvent
{
    public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
    {

        int scoreCellHeight = 20;

        var pdfContentByte = canvases[0];
        pdfContentByte.SaveState();

        pdfContentByte.Rectangle(pos.Left, pos.Bottom + scoreCellHeight, pos.Width, pos.Height - scoreCellHeight);
        pdfContentByte.SetColorFill(new BaseColor(233, 240, 242));
        pdfContentByte.Fill();


        ColumnText ct = new ColumnText(pdfContentByte);
        ct.SetSimpleColumn(new Phrase(_model.Name, _nfSubComp), pos.Left, pos.Bottom + 20, pos.Left + pos.Width, pos.Bottom + pos.Height - 5, 10, Element.ALIGN_CENTER);
        ct.Go();

        float scaleWidth = pos.Width / 10;

        for (int i = 1; i < 11; i++)
        {
            float scaleLeft = pos.Left + (i - 1) * pos.Width / 10;

            pdfContentByte.Rectangle(scaleLeft, pos.Bottom, scaleWidth, scoreCellHeight);

            pdfContentByte.SetColorFill(i % 2 == 1
                ? new BaseColor(Color.LightBlue)
                : new BaseColor(233, 240, 242));
            pdfContentByte.Fill();


            ct.SetSimpleColumn(new Phrase(i.ToString(), _nfScale), scaleLeft, pos.Bottom,
                    scaleLeft + scaleWidth, pos.Bottom + 7, 0, Element.ALIGN_CENTER);
            ct.Go();
        }
        canvases[0].RestoreState();
    }
}

我已经跳过了类构造函数和代码,绘制标记(屏幕截图上的红色数字)。

I've skipped some details like class constructor and the code, that draws marker (red figures on the screenshot).

结果:

我想,这种解决方法不是最佳选择。但我不得不画一个红色标记,所以我不得不处理细胞渲染事件。

I guess, this workaround is not optimal. But I had to draw a red marker, so I had to handle cell render event, anyway.

希望,有人可以展示正确的解决方案。

Hope, someone can show correct solution.

这篇关于在使用itextsharp创建的PDF表中设置单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆