因此,我尝试使用OpenPDF创建时间表。我目前面临的一个问题是如何为单元格创建一个具有不同colspans和rowpans(取决于计划的持续时间)的表。我已经搜索过,没有发现任何类似或类似的问题。我已经创建了一个timetable来放置时间表。我正在尝试实现this之类的东西。尝试添加计划(行距为4的单元格)后,时间单元格(8:30-10:00,...)开始放错位置,如this。
这是我编写的代码:
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();
我希望你能帮助我。谢谢!