无法在PDF表格中添加行。

huangapple go评论86阅读模式
英文:

Unable to add rows in a PDF table

问题

public class report {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\report.pdf"));
            document.open();

            PdfPTable table = new PdfPTable(4); // 4列
            table.setWidthPercentage(100); // 宽度100%
            table.setSpacingBefore(10f); // 表格前间距
            table.setSpacingAfter(10f); // 表格后间距

            // 设置列宽
            float[] columnWidths = {1f, 1f, 1f , 1f};
            table.setWidths(columnWidths);

            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            cell1.setBorderColor(BaseColor.RED);
            cell1.setBackgroundColor(BaseColor.GRAY);
            cell1.setPaddingLeft(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            cell2.setBorderColor(BaseColor.RED);
            cell2.setBackgroundColor(BaseColor.GRAY);
            cell2.setPaddingLeft(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
            cell3.setBorderColor(BaseColor.RED);
            cell3.setBackgroundColor(BaseColor.GRAY);
            cell3.setPaddingLeft(10);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
            cell4.setBorderColor(BaseColor.WHITE);
            cell4.setBackgroundColor(BaseColor.GRAY);
            cell4.setPaddingLeft(10);
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);

            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
英文:

I tried adding rows under the column. It must display 2 rows which consists of 4 columns each.

The below code provides 1 row and 4 column respectively.

I tried adding cell1.setRowSpan(1) under each cell but that doesn't seems to work.

public class report {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\report.pdf"));
            document.open();

            PdfPTable table = new PdfPTable(4); // 4 columns.
            table.setWidthPercentage(100); //Width 100%
            table.setSpacingBefore(10f); //Space before table
            table.setSpacingAfter(10f); //Space after table

            //Set Column widths
            float[] columnWidths = {1f, 1f, 1f , 1f};
            table.setWidths(columnWidths);

            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            cell1.setBorderColor(BaseColor.RED);
            cell1.setBackgroundColor(BaseColor.GRAY);
            cell1.setPaddingLeft(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            cell2.setBorderColor(BaseColor.RED);
            cell2.setBackgroundColor(BaseColor.GRAY);
            cell2.setPaddingLeft(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
            cell3.setBorderColor(BaseColor.RED);
            cell3.setBackgroundColor(BaseColor.GRAY);
            cell3.setPaddingLeft(10);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
            cell4.setBorderColor(BaseColor.WHITE);
            cell4.setBackgroundColor(BaseColor.GRAY);
            cell4.setPaddingLeft(10);
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);

            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {`enter code here`
            e.printStackTrace();
        }
    }
}

答案1

得分: 1

你已经定义了你的表格有4列:

PdfPTable table = new PdfPTable(4); // 4列。

而且你已经添加了4个单元格:

table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);

所以,只需继续添加更多的单元格。你添加的第5个单元格会自动成为第二行的第一个单元格。

如果你想看看我的意思,可以重复你的四个 addCell() 语句:

无法在PDF表格中添加行。

这只是重复了整个第一行。

英文:

You have already defined your table to have 4 columns:

PdfPTable table = new PdfPTable(4); // 4 columns.

And you have already added 4 cells:

table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);

So, just keep adding more cells. The 5th cell you add will automatically become the first cell of row 2.

You can repeat your four addCell() statements if you want to see what I mean:

无法在PDF表格中添加行。

This just repeats the entire first row.

huangapple
  • 本文由 发表于 2020年4月8日 03:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定