我需要在pdf中画一个表格,我使用itext 7 java。
Table table = new Table(4);
table.setTextAlignment(TextAlignment.CENTER);
table.setBorder(Border.NO_BORDER);
table.setPadding(0);
table.useAllAvailableWidth();
table.addCell(new Cell(2,1).add(new Paragraph("1")));
table.addCell(new Cell(2,1).add(new Paragraph("2")));
table.addCell(new Cell(1,1).add(new Paragraph("3")));
table.addCell(new Cell(1,1).add(new Paragraph("4")));
table.addCell(new Cell(2,1).add(new Paragraph("5")));
table.addCell(new Cell(2,1).add(new Paragraph("6")));
table.addCell(new Cell(2,1).add(new Paragraph("7")));
table.addCell(new Cell(2,1).add(new Paragraph("8")));
table.addCell(new Cell(2,1).add(new Paragraph("9")));
table.addCell(new Cell(2,1).add(new Paragraph("10")));
table.addCell(new Cell(2,1).add(new Paragraph("11")));
table.addCell(new Cell(2,1).add(new Paragraph("12")));
table.addCell(new Cell(2,1).add(new Paragraph("13")));
table.addCell(new Cell(2,1).add(new Paragraph("14")));
table.addCell(new Cell(2,1).add(new Paragraph("15")));
table.addCell(new Cell(2,1).add(new Paragraph("16")));
table.addCell(new Cell(2,1).add(new Paragraph("17")));
table.addCell(new Cell(2,1).add(new Paragraph("18")));
table.addCell(new Cell(2,1).add(new Paragraph("19")));
table.addCell(new Cell(2,1).add(new Paragraph("20")));
table.addCell(new Cell(1,1).add(new Paragraph("21")));
table.addCell(new Cell(1,1).add(new Paragraph("22")));
document.add(table);
document.close();
我应该明确设置每个单元格的高度。
PdfWriter writer = new PdfWriter("test-table.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
Table table = new Table(4);
table.setTextAlignment(TextAlignment.CENTER);
table.setBorder(Border.NO_BORDER);
table.setPadding(0);
table.useAllAvailableWidth();
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("1")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("2")));
table.addCell(new Cell(1,1).setHeight(20).add(new Paragraph("3")));
table.addCell(new Cell(1,1).setHeight(20).add(new Paragraph("4")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("5")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("6")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("7")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("8")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("9")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("10")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("11")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("12")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("13")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("14")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("15")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("16")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("17")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("18")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("19")));
table.addCell(new Cell(2,1).setHeight(40).add(new Paragraph("20")));
table.addCell(new Cell(1,1).setHeight(20).add(new Paragraph("21")));
table.addCell(new Cell(1,1).setHeight(20).add(new Paragraph("22")));
document.add(table);
document.close();