How to set PdfPTable height in iTextSharp

I downloaded the latest version of iTextSharp dll. I created a PdfPTable object and I have to set its height. Despite the given width of the PdfPTable, I cannot set its height. Some authors suggest using the setFixedHeight method. But the latest version of iTextSharp.dll does not have a "setFixedHeight" method. This is version 5.5.2. How can i do this?

+5
source share
3 answers

Setting the table height does not make sense as soon as you start thinking about it. Or it makes sense, but leaves many questions unanswered or unanswered. For example, if you set a two-row table to a height of 500, does this mean that each cell gets 250 for height? What if a large image falls in the first row, if the table automatically responds by splitting 400/100? Then what about the large content on both lines, should it squish them? Each of these scenarios gives different results, which give an idea that the table will indeed be unreliable. If you look at the HTML specification , you will see that they do not even allow you to set a fixed height for tables.

However, there is a simple solution and just setting a fixed height for the cells themselves. Until you use new PdfPCell() , you can simply 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 manually create cells, you need to set FixedHeight for 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 a normal table and need to set a fixed height that interrupts anything that doesn't fit, you can also use ColumnText . I would not recommend this, but you may have a case. In the code below, only six lines will be displayed.

 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(); 
+17
source

you can use any of the following

cell.MinimumHeight = 20f;

or

cell.FixedHeight = 30f;

+5
source

It is assumed that you downloaded the iText jar jar, you can try this code, you can execute this function. In an A4 document, output a row of three dataeg ​​columns:

 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(); } } 
0
source

Source: https://habr.com/ru/post/1200190/


All Articles