1

所以我正在尝试使用 OpenPDF 创建一个时间表。我目前面临的问题是如何为单元格创建具有不同列跨度和行跨度(取决于计划的持续时间)的表。我已经搜索过并没有发现任何面临或类似于此类问题的问题。我已经创建了一个时间表,将在其中放置时间表。我正在努力实现这样的目标。在尝试添加时间表(行跨度为 4 的单元格)后,时间单元格(8:30 - 10:00,...)开始像这样错位。

这是我写的代码:

Document document = new Document();

    try {
        PdfWriter.getInstance(document, new FileOutputStream("TimeTable.pdf"));

        document.open();
        String[] times = new String[]{"7:00 - 8:30", "8:30 - 10:00", "10:00 - 11:30", 
        "11:30 - 13:00", "13:00 - 14:30", "14:30 - 16:00", 
        "16:00 - 17:30", "17:30 - 19:00", "19:00 - 20:30"};

        // 7 columns. 28 rows. 1st row is time, M, TH, T, F, W, S

        DMSchedule schedule = new DMSchedule(0, "M", "08:00", "10:00", 0, "S201", "CS101", 0);

        PdfPTable table = new PdfPTable(7);

        PdfPCell cell = new PdfPCell(new Paragraph("TIME"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("M"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("TH"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("T"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("F"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("W"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("S"));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

        int rowStartingPos = 4;
        int timeCounter = 0;

        for (int row = 2; row <= 28; row++) { // started at 2 because 1st row is already occupied by TIME, M, TH, T, F, W, S 
            limit = 6;
            for (int column = 1; column <= 6; column++) {

                if ((row == 2 || row == 5 || row == 8 || 
                    row == 11 || row == 14 || row == 17 || 
                    row == 20 || row == 23 || row == 26) && column == 1) {
                    cell = new PdfPCell(new Paragraph(times[timeCounter]));
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell.setRowspan(3);
                    table.addCell(cell);
                    timeCounter++;
                }

                if (row == rowStartingPos) {
                    if (column == 1) {
                        cell = new PdfPCell(new Paragraph("CS101:\tnull(M)\t08:00 - 10:00"));
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setRowspan(4);
                        table.addCell(cell);
                    }
                }

                table.addCell("\t");

            }
        }

        document.add(table);

    } catch (DocumentException | IOException de) {
        System.err.println(de.getMessage());
    }

    document.close();

我希望你能帮助我。谢谢!

4

0 回答 0