文本和图像在同一单元格与itextsharp

本文关键字:单元格 itextsharp 图像 文本 | 更新日期: 2023-09-27 18:02:47

我创建了一个有48张图片的表。我把它放在单元格的右边。所以我想把图像的编号放在同一个单元格里。在左边

这是我的部分代码:

for (int i = x; i <= 48; i += 4)
{
    int number = i + 4;
    imageFilePath = "images/image" + number + ".jpeg";
    jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
    jpg.ScaleAbsolute(166.11023622047f, 124.724409448829f);
    jpg.BorderColor = BaseColor.BLUE;
    string numberofcard = i.ToString();
    cell = new PdfPCell(jpg);
    cell.FixedHeight = 144.56692913386f;
    cell.Border = 0;
    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    table5.AddCell(cell);
}

如何在单元格的左上角插入图像的数量?

文本和图像在同一单元格与itextsharp

下面是一个例子。

您需要创建一个自定义的IPdfPCellEvent实现。

private class MyEvent : IPdfPCellEvent {
    string number;
    public MyEvent(string number) {
         this.number = number;
    }
    public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        ColumnText.ShowTextAligned(
            canvases[PdfPTable.TEXTCANVAS],
            Element.ALIGN_LEFT,
            new Phrase(number),
            position.Left + 2, position.Top - 16, 0);
    }
}

可以看到,我们在绝对位置添加了Phrase和内容number。在本例中:单元格左边框以外有2个用户单位,单元格顶部以下有16个用户单位。

然后在代码片段中添加:

cell.CellEvent = new my_event(n);

其中nnumber的字符串值。现在,每次渲染单元格时,都会绘制这个数字。