How to Set Height of PdfPTable in iTextSharp

后端 未结 3 1279
别跟我提以往
别跟我提以往 2020-12-07 01:47

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

3条回答
  •  萌比男神i
    2020-12-07 02:06

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

提交回复
热议问题