Integer ii = null;
ArrayList<Character> charList = null;
ArrayList<Integer> oneDataList = ps.matchIntAllCapturingGroups(seq);
ArrayList<QuestionData> qdList = generateQuestions(oneDataList, howmany);
// step 1
Document document = new Document(PageSize.LETTER);
// step 2
ServletContext sc = ServletActionContext.getServletContext();
String FilePath = sc.getRealPath("/");
String outPath = (FilePath==null)?"temp/" + FNAME:FilePath + "temp/" + FNAME;
String pathForJavaScript = "./" + "temp/" + FNAME;
PdfWriter.getInstance(document, new FileOutputStream(outPath));
// step 3
document.open();
// step 4
document.add(new Paragraph("StrutsSchoolWeb - Random Multiplication Questions"));
document.add(new Paragraph(new Date().toString()));
document.add(Chunk.NEWLINE);
//document.add(new Paragraph("Seq:"+seq));
// for(QuestionData qd: qdList) {
// document.add(new Paragraph(qd.toStringQuestionAndAnswer()));
// }
PdfPTable table = null;
PdfPCell cell = null;
int columns = 0;
PdfPTable tableInner = null;
PdfPCell cellInner = null;
table = new PdfPTable(tableColumns);
table.setWidthPercentage(100);
table.setSpacingBefore(0);
for (QuestionData qd : qdList) {
columns++;
tableInner = new PdfPTable(3); //Grade 3 questions should be 3 cols only (2 digits and 1 op-code)
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
charList = QuestionData.getIntChars(qd.getFactor1());
if (charList.size() == 1) {
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
} else if (charList.size() >= 2) {
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
}
cellInner = new PdfPCell(new Paragraph(qd.getOperation()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
charList = QuestionData.getIntChars(qd.getFactor2());
if (charList.size() == 1) {
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
} else if (charList.size() >= 2) {
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
}
// Add top answer line
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setColspan(3);
cellInner.setBorder(PdfPCell.TOP);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
if (showAnswers == true) {
charList = QuestionData.getIntChars(qd.getAnswer());
switch (charList.size()) {
case 3:
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(2).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
break;
case 2:
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
break;
case 1:
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
break;
default:
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setColspan(3);
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
}
}
//Add 1 blank lines for calculations
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setColspan(3);
cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
// Add lower Answer line
cellInner = new PdfPCell(new Paragraph(" "));
cellInner.setColspan(3);
cellInner.setBorder(PdfPCell.TOP);
cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tableInner.addCell(cellInner);
cell = new PdfPCell(tableInner);
// cell.addElement(tableInner);
cell.setBorder(PdfPCell.BOX);
cell.setBorderWidthRight(borderWidth);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
table.addCell(cell);
if (columns > tableColumns) {
columns = 0;
}
}
int remaining = tableColumns - columns;
while (remaining > 0) {
cell = new PdfPCell();
cell.setBorder(PdfPCell.BOX);
cell.setBorderWidthRight(borderWidth);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
table.addCell(cell);
remaining--;
}
document.add(table);
// but the result looks like one big table
// step 4
document.close();
return (pathForJavaScript);
}