How to Set Height of PdfPTable in iTextSharp

2020-02-01 18:39发布

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?

3条回答
相关推荐>>
2楼-- · 2020-02-01 19:11

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();
查看更多
等我变得足够好
3楼-- · 2020-02-01 19:15

you can use any one of following

cell.MinimumHeight = 20f;

or

cell.FixedHeight = 30f;

查看更多
唯我独甜
4楼-- · 2020-02-01 19:19

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();
    }
}
查看更多
登录 后发表回答