微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – itextsharp pdfpcell垂直对齐问题

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

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);

解决方法

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

我建议尝试使用更小的图像(或更小的相同图像)来查看我是否正确.

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

IIRC,段落将占用整个单元格,但是块将遵循单元格垂直和水平对齐设置.请参阅my answer here中的评论.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐