9

i downloaded the last version of iTextSharp dll. I generated a PdfPTable object and i have to set it's height. Despite to set width of PdfPTable, im not able to set height of it. Some authors suggest to use 'setFixedHeight' method. But the last version of iTextSharp.dll has not method as 'setFixedHeight'. It's version is 5.5.2. How can i do it?

mustafaalkan64
  • 207
  • 1
  • 3
  • 13

3 Answers3

26

Setting a table's height doesn't make sense once you start thinking about it. Or, it makes sense but leaves many questions unanswered or unanswerable. For instance, if you set a two row table to a height of 500, does that mean that each cell get's 250 for a height? What if a large image gets put in the first row, should the table automatically respond by splitting 400/100? Then what about large content in both rows, should it squish those? Each of these scenarios produces different results that make knowing what a table will actually do unreliable. If you look at the HTML spec you'll see that they don't even allow setting a fixed height for tables.

However, there's a simple solution and that's just setting the fixed height of the cells themselves. As long as you aren't using new PdfPCell() you can just set DefaultCell.FixedHeight to whatever you want.

var t = new PdfPTable(2);
t.DefaultCell.FixedHeight = 100f;

t.AddCell("Hello");
t.AddCell("World");
t.AddCell("Hello");
t.AddCell("World");

doc.Add(t);

If you are manually creating cells then you need to set the FixedHeight on each:

var t = new PdfPTable(2);

for(var i=0;i<4;i++){
    var c = new PdfPCell(new Phrase("Hello"));
    c.FixedHeight = 75f;
    t.AddCell(c);
}

doc.Add(t);

However, if you want normal table-ness and must set a fixed height that chops things that don't fit you can also use a ColumnText. I wouldn't recommend this but you might have a case for it. The code below will only show six rows.

var ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(100, 100, 200, 200);

var t = new PdfPTable(2);
for(var i=0;i<100;i++){
    t.AddCell(i.ToString());
}
ct.AddElement(t);
ct.Go();
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
6

you can use any one of following

cell.MinimumHeight = 20f;

or

cell.FixedHeight = 30f;

malik saifullah
  • 169
  • 1
  • 4
0

The premise is that you have downloaded the jar iText jar,you can try this code,you can achive this function,In a A4 paper out a row of three columns of dataeg:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class ceshi {

    public static final String DEST = "D:\\fixed_height_cell.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ceshi().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(3);// Set a row and the three column of
                                            // A4 paper
        table.setWidthPercentage(100);
        PdfPCell cell;
        for (int r = 1; r <= 2; r++) {// Set display two lines
            for (int c = 1; c <= 3; c++) {// Set to display a row of three  columns
                cell = new PdfPCell();
                cell.addElement(new Paragraph("test"));
                cell.setFixedHeight(285);// Control the fixed height of each cell
                table.addCell(cell);
            }
        }
        document.add(table);
        document.close();
    }
}
nihaoqiulinhe
  • 259
  • 3
  • 5
  • In his question the OP clearly mentioned that he uses the *"last version of iTextSharp dll" [...] It's version is 5.5.2.* Thus, he was using an iText version for **.Net** in a version **5.5.x**. Your code, on the other hand, uses an iText version for **Java** in a version **before 5.0.0**, probably 2.1.7, the unofficial 4.2.0, or some spin-off of either. – mkl Dec 15 '16 at 11:14
  • Considering he asked in 2014, I doubt he still is looking for such ideas... ;) – mkl Dec 16 '16 at 05:41
  • en,I donot how to use php,maybe someone can convert this code to PHP code. – nihaoqiulinhe Dec 16 '16 at 08:39