【问题标题】:itextsharp pdfpcell vertical align problemitextsharp pdfpcell 垂直对齐问题
【发布时间】:2011-06-20 19:21:21
【问题描述】:

如何让两个单元格垂直对齐。目前,第二个单元格呈现在第一个单元格下方。第一个单元格是图像,第二个单元格是文本。这是我的代码。

    private Document pdoc;
    Font font99 = FontFactory.GetFont("HELVETICA", 60);
    PdfPTable pdfRatingTable = new PdfPTable(2);
    PdfPCell pRatCell = null;
    pdfRatingTable.WidthPercentage = 100;
    pdfRatingTable.SetWidths(new int[] { 75, 25 });

    hImage = iTextSharp.text.Image.GetInstance(MapPath("~/Images/fyler3_Rating.jpg"));
    NewWidth = 338;
    MaxHeight = 18;
    if (hImage.Width <= NewWidth)
    {
        NewWidth = hImage.Width;
    }
    NewHeight = hImage.Height * NewWidth / hImage.Width;
    if (NewHeight > MaxHeight)
    {
        NewWidth = hImage.Width * MaxHeight / hImage.Height;
        NewHeight = MaxHeight;
    }

    ratio = hImage.Width / hImage.Height;
    hImage.ScaleAbsolute(NewWidth, NewHeight);
    pRatCell = new PdfPCell(hImage);
    pRatCell.Border = 0;
    pRatCell.PaddingLeft = 20f;
    pRatCell.HorizontalAlignment = Element.ALIGN_LEFT;
    pdfRatingTable.AddCell(pRatCell);

    pRatCell = new PdfPCell(new Phrase(new Chunk("405", font99)));
    pRatCell.HorizontalAlignment = Element.ALIGN_LEFT;
    pRatCell.Border = 0;
    pRatCell2.VerticalAlignment = Element.ALIGN_TOP;
    pdfRatingTable.AddCell(pRatCell);
    pdoc.Add(pdfRatingTable);

【问题讨论】:

    标签: c# itextsharp


    【解决方案1】:

    您的图片看起来比表格中单元格的最大宽度宽(加上相当大的填充),所以下一个单元格出现在下一行。

    我建议尝试使用小得多的图像(或较小比例的相同图像)来看看我是否正确。


    或者“下方”是指它们都在同一行中,但是文本出现在单元格的底部,而图像在中间,周围有一个 20 点的垫子,所以文本完全是图片下方?

    IIRC,Paragraph 将占据整个单元格,但 Chunk 将遵循单元格的垂直和水平对齐设置。请参阅my answer here 中的 cmets。

    【讨论】:

    • 嗨,马克,我什至将图像调整为更小一点,但右侧的单元格似乎仍位于下一行。即使我用 pRatCell = new PdfPCell(new Phrase(new Chunk("This is a test")));第二个单元格仍然出现在下一行。我为桌子设置的宽度看起来不错吗?
    • 我建议您打开单元格边框,以确保它确实在下一行,而不仅仅是更低(我希望使用 20pt 的垫子)。
    • 感谢设置边界有助于弄清楚。我需要在第一个单元格的顶部进行一些填充以将其与下一个单元格对齐。再次感谢。
    猜你喜欢
    • 2014-09-21
    • 2011-02-26
    • 2012-10-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    相关资源
    最近更新 更多