ITextSharp: how to find the first and second row height in a table?

I want to get the heights of the first and second columns to see if I need to call document.NewPage()

or not. But I cannot find a way to do this without adding the table to the document.

Example:

PdfPRow firstRow = new PdfPRow(cells1.ToArray());
table.Rows.Add(firstRow);
PdfPRow secondRow = new PdfPRow(cells2.ToArray());
table.Rows.Add(secondRow);

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

if (currentY - h1 - h2 < 30) document.NewPage();

document.Add(table);

      

+3


source to share


4 answers


See my answer here . Basically, you cannot know the dimensions of a table before displaying it. However, you can render the table in a document that you simply discard and then reprocess later.



+4


source


Interesting question, so +1. And already marked as an answer, but ...

"But I can't find a way to do this without adding a table to the document."

Perhaps <... Wrap PdfPTable

in an object ColumnText

and use the ColumnText.Go () overload to get the total height of any arbitrary / number of lines you want without adding PdfPTable

in Document

. Here's a simple helper method:

public static float TotalRowHeights(
  Document document, PdfContentByte content, 
  PdfPTable table, params int[] wantedRows) 
{
  float height = 0f;
  ColumnText ct = new ColumnText(content);
// respect current Document.PageSize    
  ct.SetSimpleColumn(
    document.Left, document.Bottom, 
    document.Right, document.Top
  );
  ct.AddElement(table);
// **simulate** adding the PdfPTable to calculate total height
  ct.Go(true);
  foreach (int i in wantedRows) {
    height += table.GetRowHeight(i);
  }
  return height;
}

      



And a simple example using 5.2.0.0:

using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  PdfPTable table = new PdfPTable(4);
  for (int i = 1; i < 20; ++i) {
    table.AddCell(i.ToString());
  }
  int[] wantedRows = {0, 2, 3};
  document.Add(new Paragraph(string.Format(
    "Simulated table height: {0}",
    TotalRowHeights(document, writer.DirectContent, table, wantedRows)
  )));
// uncomment block below to verify correct height is being calculated
/* 
  document.Add(new Paragraph("Add the PdfPTable"));
  document.Add(table);
  float totalHeight = 0f;
  foreach (int i in wantedRows) {
    totalHeight += table.GetRowHeight(i);
  }
  document.Add(new Paragraph(string.Format(
    "Height after adding table: {0}", totalHeight
  )));
*/
  document.Add(new Paragraph("Test paragraph"));
}

      

In case of use, lines 1, 3 and 4 are used, but only for demonstrating any combination / number of lines will work.

+2


source


If you don't set the width of the table, table.GetRowHeight (0) will always return zero.

// added
table.TotalWidth = 400f;     
//
PdfPRow firstRow = new PdfPRow(cells1.ToArray());

table.Rows.Add(firstRow);
PdfPRow secondRow = new PdfPRow(cells2.ToArray());
table.Rows.Add(secondRow);

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

if (currentY - h1 - h2 < 30) document.NewPage();

document.Add(table);

      

+2


source


There is another way: First, create a table.

    this.table = new PdfPTable(relativeColumnWidths);
    this.table.SetTotalWidth(absoluteColumnWidths);
    this.rowCells.Clear();

      

Now you can fill the list with table cells:

    Paragraph pText = new Paragraph(text, this.font);
    PdfPCell cell = new PdfPCell(pText);
    this.rowCells.Add(cell);

      

When you're ready to create a new line:

    PdfPRow row = new PdfPRow(this.rowCells.ToArray());
    this.table.Rows.Add(row);

      

It's nothing special. But if you set the table width again, you can calculate the row height correctly:

    this.table.SetTotalWidth(this.table.AbsoluteWidths);
    this.rowCells.Clear();
    float newRowsHeight = this.table.GetRowHeight(this.table.Rows.Count - 1);

      

If a row does not match your criteria, you can simply remove it from the table row collection. The overall height of the table will also be calculated correctly.

+2


source







All Articles