2016-03-07 184 views
2

我想获得下面的单元格的高度。如何获取C#中itextSharp表格单元格的高度?

cell_logo

cell_title

cell_version

cell_dateTime

cell_appVersion

cell_name.Height回报。我怎样才能得到这些细胞的实际高度?

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

其实我想设置表格高度等于页面大小

+0

可能重复[无法正确计算itext PdfPTable/PdfPCell高度](http://stackoverflow.com/questions/24448319/unable-to-calculate-itext-pdfptable-pdfpcell-height-properly)。您需要按照[这些示例](http://developers.itextpdf.com/examples/tables/repeating-rows)中所述的方式询问表格的高度。高度单元本身并不重要;这是单元格所属行的高度。另请参阅[如何使页脚粘到每个pdf页面的底部?](http://developers.itextpdf.com/question/how-make-footer-stick-bottom-every-pdf-page) –

回答

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

我把你的问题标记为一个可能的重复,但我然后重新考虑并决定发表一个答案,因为你的问题的答案实际上可能不是确切的重复,但它可能需要已经给出的答案的组合。

+0

不错 - 一个考虑到几种不同的情况。 – kuujinbo