2016-03-07 185 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