2

I want to get the height of following cells.

cell_logo

cell_title

cell_version

cell_dateTime

cell_appVersion

but cell_name.Height return 0 . How can I get the actual height of these cell s ?

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

Actually I want to set the table height equal to page size

Sachith
  • 191
  • 1
  • 7
  • 15
  • Possible duplicate of [unable to calculate itext PdfPTable/PdfPCell height properly](http://stackoverflow.com/questions/24448319/unable-to-calculate-itext-pdfptable-pdfpcell-height-properly). You need to ask the table for the height of a row as is done in [these examples](http://developers.itextpdf.com/examples/tables/repeating-rows). The height cell on its own doesn't matter; it's the height of the row to which the cell belongs that matters. See also [How to make a footer stick to bottom of every pdf page?](http://developers.itextpdf.com/question/how-make-footer-stick-bottom-every-pdf-page) – Bruno Lowagie Mar 07 '16 at 08:17

1 Answers1

6

Reading your code sample, I notice that you have read the answer to this question: Itextsharp: Adjust 2 elements on exactly one page

You are correctly setting the width of the table, which is mandatory if you want to calculate the height:

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

You now want to know the height of each cell. This doesn't work for you because you are looking at the wrong place. You shouldn't look at the height of the cell, you should look at the height of the row to which the cell belongs. The height of the cell doesn't really matter, it's the height of the row that matters.

This is explained in the answer to the question iTextSharp: How to find the first and second row height in a table?

float h1 = table.GetRowHeight(0);
float h2 = table.GetRowHeight(1);

Your ultimate goal is to set the height of the table so that it fits the page. If it's acceptable that this is accomplished by extending the last row, then you could use the answer to the question Itextsharp make footer stick at bottom of every pdf page

table.SetExtendLastRow(true, true);

If that's not acceptable, and if you want to define the height of each row individually, your task is more difficult. In that case, you need to read the answer to this question Setting height for table in iTextSharp

I marked your question as a possible duplicate, but I then reconsidered and decided to post an answer anyway, because the answer to your question might actually be not an exact duplicate, but it might require a combination of answers that were already given.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165