簡體   English   中英

iTextSharp PdfPCell將圖像對齊到單元格的底部

[英]iTextSharp PdfPCell align image to bottom of cell

我正在嘗試將圖像對齊到單元格的右下角。 我基本上創建了一個每行有兩個單元格的表。 單元格包含文本和圖像,我希望將其與單元格的右下角對齊。 請參考圖片

這是我的代碼

 PdfPTable table = new PdfPTable(2);
 table.TotalWidth = 400f;
 table.LockedWidth = true;

 float[] widths = new float[] { 1.5f, 1.5f };
 table.SetWidths(widths);
 table.HorizontalAlignment = 0;

 table.SpacingBefore = 50f;
 table.SpacingAfter = 30f;

 iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/MyImage.png"));
 logoImage.ScaleAbsolute(40, 40);
 logoImage.Alignment = iTextSharp.text.Image.ALIGN_BOTTOM;
 logoImage.Alignment = iTextSharp.text.Image.RIGHT_ALIGN;

 foreach (EmployeeModel employee in employees)
 {
    PdfPCell cell = new PdfPCell();
    cell.FixedHeight = 140f;

    cell.PaddingLeft = 30f;
    cell.PaddingRight = 10f;
    cell.PaddingTop = 20f;
    cell.PaddingBottom = 5f;

    Paragraph p = new Paragraph(GetLabelCellText(Employee), NormalFont);
    p.Alignment = Element.ALIGN_LEFT;
    p.Alignment = Element.ALIGN_TOP;
    cell.AddElement(p);

    cell.AddElement(logoImage);

    table.AddCell(cell);
 }

如何將圖像放在每個單元格的右下角(當然不影響文本的位置)。

因為你創建一個問題是一個有點曖昧Table有兩列,但增加了細胞未經核實的employees集合有偶數個元素,這將throw如果不....

假設你確實想要單個單元格中的文本圖像,可能最簡單的方法是獲得所需的布局是實現IPdfPCellEvent

public class BottomRightImage : IPdfPCellEvent
{
    public Image Image { get; set;  }

    public void CellLayout(
        PdfPCell cell, 
        Rectangle position, 
        PdfContentByte[] canvases)
    {
        if (Image == null) throw new InvalidOperationException("image is null");

        PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
        Image.SetAbsolutePosition(
            position.Right - Image.ScaledWidth - cell.PaddingRight, 
            position.Bottom + cell.PaddingBottom
        );
        canvas.AddImage(Image);
    }
}

然后在PdfPCell上設置CellEvent屬性。 這是一個簡單的工作示例:

using (var stream = new MemoryStream())
{
    using (var document = new Document())
    {
        PdfWriter.GetInstance(document, stream);
        document.Open();
        var table = new PdfPTable(2)
        {
            HorizontalAlignment = Element.ALIGN_LEFT,
            TotalWidth = 400f,
            LockedWidth = true                        
        };

        var image = Image.GetInstance(imagePath);
        image.ScaleAbsolute(40, 40);
        var cellEvent = new BottomRightImage() { Image = image };

        var testString =
@"first name: {0}
last name: {0}
ID no: {0}";
        for (int i = 0; i < 2; ++i)
        {
            var cell = new PdfPCell()
            {
                FixedHeight = 140f,
                PaddingLeft = 30f,
                PaddingRight = 10f,
                PaddingTop = 20f,
                PaddingBottom = 5f
            };
            cell.CellEvent = cellEvent;

            var p = new Paragraph(string.Format(testString, i))
            {
                Alignment = Element.ALIGN_TOP | Element.ALIGN_LEFT
            };
            cell.AddElement(p);
            table.AddCell(cell);
        }
        document.Add(table);
    }
    File.WriteAllBytes(outputFile, stream.ToArray());
}

和PDF輸出:

在此輸入圖像描述

全黑方形圖像用於顯示PdfPCell填充被考慮在內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM