如何在C#中获取itextSharp表格单元格的高度?

Sac*_*ith 2 c# pdf height cell itextsharp

我想获得跟随细胞的高度.

cell_logo

cell_title

cell_version

cell_dateTime

cell_appVersion

但是cell_name.Height返回0.我怎样才能得到这些细胞的实际高度?

PdfPTable table = new PdfPTable(1);
        table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;            
        table.LockedWidth = true;        

        PdfPCell cell_logo = new PdfPCell(imgLog);
        cell_logo.HorizontalAlignment = 1;
        cell_logo.BackgroundColor = new BaseColor(System.Drawing.Color.White);
        cell_logo.PaddingBottom = 20;
        cell_logo.PaddingTop = 50;

        PdfPCell cell_title = new PdfPCell(docName);
        cell_title.HorizontalAlignment = 1;
        cell_title.BackgroundColor = new BaseColor(System.Drawing.Color.White);
        cell_title.PaddingBottom = 50;

        PdfPCell cell_versions = new PdfPCell(ssVersions);
        cell_versions.BackgroundColor = new BaseColor(System.Drawing.Color.White);            
        cell_versions.PaddingTop = 5;
        cell_versions.PaddingBottom = 5;

        PdfPCell cell_dateTime = new PdfPCell(time);
        cell_dateTime.BackgroundColor = new BaseColor(System.Drawing.Color.White);
        cell_dateTime.PaddingTop = 5;
        cell_dateTime.PaddingBottom = 5;

        PdfPCell cell_appVersion =  new PdfPCell(SSCGVersion);
        cell_appVersion.BackgroundColor = new BaseColor(System.Drawing.Color.White);
        cell_appVersion.MinimumHeight = doc.PageSize.Height - doc.TopMargin - doc.BottomMargin - cell_logo.Height - cell_title.Height - cell_versions.Height - cell_dateTime.Height;


        table.AddCell(cell_logo);
        table.AddCell(cell_title);
        table.AddCell(cell_versions);
        table.AddCell(cell_dateTime);
        table.AddCell(cell_appVersion);          

        doc.Add(table); 
Run Code Online (Sandbox Code Playgroud)

实际上我想将表格高度设置为等于页面大小

Bru*_*gie 6

阅读你的代码示例,我注意到你已经阅读了这个问题的答案:Itextsharp:在一个页面上调整2个元素

您正确设置表格的宽度,如果要计算高度,这是必需的:

table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;            
table.LockedWidth = true; 
Run Code Online (Sandbox Code Playgroud)

您现在想知道每个单元格的高度.这对你不起作用,因为你看错了地方.您不应该查看单元格的高度,您应该查看单元格所属行的高度.细胞的高度并不重要,重要的是行的高度.

这在iTextSharp问题的答案中有解释:如何在表格中找到第一行和第二行的高度?

float h1 = table.GetRowHeight(0);
float h2 = table.GetRowHeight(1);
Run Code Online (Sandbox Code Playgroud)

您的最终目标是设置表格的高度,使其适合页面.如果通过扩展最后一行来实现这一点是可以接受的,那么你可以使用Itextsharp在每个pdf页面底部制作页脚的问题的答案

table.SetExtendLastRow(true, true);
Run Code Online (Sandbox Code Playgroud)

如果这是不可接受的,并且如果您想单独定义每行的高度,则您的任务更加困难.在这种情况下,您需要阅读此问题的答案在iTextSharp中设置表格的高度

我将您的问题标记为可能重复,但我重新考虑并决定发布答案,因为您的问题的答案实际上可能不是完全重复,但可能需要已经给出的答案组合.