iTextPdf 中单元格右下角的图像

Image in bottom right corner of cell in iTextPdf

我正在努力寻找一种方法将图像放入 iTextPdf 中 table 单元格的右下角。

我想在这里有图片:

我正在玩 IPdfPCellEvent,但到目前为止运气不好。

现在末尾有一堆换行符,所以文本 wrapping/overflow 不是问题。

您可以使用这样的 IPdfPCellEvent 实现:

public class ImageDecorator : IPdfPCellEvent
{
    Image image;

    public ImageDecorator(Stream inputImageStream)
    {
        image = Image.GetInstance(inputImageStream);
        image.ScaleToFit(100, 100);
    }

    public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
    {
        PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
        canvas.AddImage(image, image.ScaledWidth, 0, 0, image.ScaledHeight, position.Right - image.ScaledWidth, position.Bottom);
    }
}

这样使用

using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
using (FileStream fs = new FileStream(dest, FileMode.Create))
{
    ImageDecorator imageDecorator = new ImageDecorator(inputImageStream);

    Document doc = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
    doc.Open();

    string loremIpsum = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
    PdfPTable table = new PdfPTable(2);

    PdfPCell cell = new PdfPCell();
    cell.CellEvent = imageDecorator;
    cell.AddElement(new Paragraph(loremIpsum));
    table.AddCell(cell);

    cell = new PdfPCell();
    cell.CellEvent = imageDecorator;
    cell.AddElement(new Paragraph(loremIpsum));
    table.AddCell(cell);

    doc.Add(table);

    doc.Close();
}

我明白了