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

时间:2016-03-07 07:06:45

标签: c# pdf height itextsharp cell

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

  

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

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

1 个答案:

答案 0 :(得分:6)

阅读您的代码示例,我注意到您已阅读此问题的答案:Itextsharp: Adjust 2 elements on exactly one page

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

table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;            
table.LockedWidth = true; 

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

问题iTextSharp: How to find the first and second row height in a table?

的答案中对此进行了解释
float h1 = table.GetRowHeight(0);
float h2 = table.GetRowHeight(1);

您的最终目标是设置表格的高度,使其适合页面。如果通过扩展最后一行来实现这一点是可以接受的,那么你可以使用问题的答案Itextsharp make footer stick at bottom of every pdf page

table.SetExtendLastRow(true, true);

如果这是不可接受的,并且如果您想单独定义每一行的高度,那么您的任务就更难了。在这种情况下,您需要阅读此问题的答案Setting height for table in iTextSharp

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