如何绘制表格(具有特定布局)?

问题描述 投票:0回答:1

enter image description here

我需要在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();

实际上我运行了我的代码并且得到了 enter image description here

itext7
1个回答
0
投票

我应该明确设置每个单元格的高度。

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();
© www.soinside.com 2019 - 2024. All rights reserved.