Java tutorial
/** * * This class uses the iText PDF Core library ( http://itextpdf.com/ ) for creating our PDF output * */ package CTD.planer2.util; import static java.util.concurrent.TimeUnit.MINUTES; import java.io.FileOutputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import CTD.planer2.App; import CTD.planer2.function.CPlaner; import CTD.planer2.objects.Person; import CTD.planer2.objects.Plan; import CTD.planer2.objects.Room; import CTD.planer2.objects.Units; import CTD.planer2.style.WeekDayPlan; import com.itextpdf.text.BadElementException; import com.itextpdf.text.Chapter; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; /** * export information of plan into PDF document * @author CrashTestDummies * */ public class ExportToPdf { /** * filename of generated pdf document */ private static String filename; /** * instance of plan which will get exported */ static Plan planExport; /** * instance of room to be exported */ static Room roomToExport; /** * another string which contains just a filename */ private static String FILE = filename; /** * pdf font information */ private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); /** * Export selected weekdayplan * * @param plan */ public void export(WeekDayPlan plan) { filename = CPlaner.selDay.toString() + ".pdf"; try { Document document = new Document(); FILE = filename; PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Export a given plan to PDF file * * @param plan */ public void export(Room room) { filename = CPlaner.selRoom.toString() + ".pdf"; roomToExport = room; try { Document document = new Document(); FILE = filename; PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Export a given plan to PDF file * * @param plan */ public void export(Plan plan) { if (CPlaner.selClass != null) { filename = CPlaner.selClass.toString() + ".pdf"; } else if (CPlaner.selPerson != null) { filename = CPlaner.selPerson.toString() + ".pdf"; } else { filename = CPlaner.selRoom.toString() + ".pdf"; } FILE = filename; planExport = plan; try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * In this function, we add the content to the pdf-file * * @param document * @throws DocumentException */ private static void addContent(Document document) throws DocumentException { // Big header Chapter catPart = new Chapter(new Paragraph(""), 1); catPart.setNumberDepth(0); if (roomToExport == null && CPlaner.selDay == null) { String[] tmp = FILE.split("\\."); catPart.add(new Paragraph(tmp[0], catFont)); } else if (CPlaner.selDay == null) { catPart.add(new Paragraph(roomToExport.toString(), catFont)); } else { catPart.add(new Paragraph(CPlaner.selDay.toString(), catFont)); } addEmptyLine(catPart, 1); // Add a table if (CPlaner.selDay != null) { createDayTable(catPart); } else { if (roomToExport == null && CPlaner.selDay == null) { createTable(catPart); } else { createTable(catPart, roomToExport); } } document.add(catPart); } /** * We create the table we want to add to our pdf * * @param subCatPart * @throws BadElementException */ private static void createTable(Section subCatPart) throws BadElementException { // Get the number of days per Week from the Settings int daysPerWeek = App.theSettings.getDaysPerWeek(); PdfPTable table = new PdfPTable(daysPerWeek + 1); String[] WeekDay = { "Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" }; for (int i = 0; i < daysPerWeek + 1; i++) { PdfPCell c1 = new PdfPCell(new Phrase(WeekDay[i])); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); if (i == daysPerWeek) { table.setHeaderRows(1); } } // We take the first day to get the number of units and so the number of // times we have to run this. for (int j = 0; j < planExport.getWeekday(1).size(); j++) { // For each day available, we run this loop once. for (int k = 1; k < daysPerWeek + 1; k++) { // Read one unit per day and write it in a new cell of the pdf List<Units> tmp = planExport.getWeekday(k); // If it's the first day, we also want a first cell per line // containing the time this unit starts if (k == 1) { SimpleDateFormat df = new SimpleDateFormat("HH:mm"); String time = df.format(tmp.get(j).getTime()); table.addCell(time); String room = " "; // Check whether a room is given if (tmp.get(j).getRooms().size() < 1) { room = " "; } else { room = tmp.get(j).getRooms().get(0).toString(); } String teacher = " "; if (tmp.get(j).getPerson() == null) { teacher = " "; } else { teacher = tmp.get(j).toString(); } String subject = " "; if (tmp.get(j).getSubjects().size() < 1) { subject = " "; } else { subject = tmp.get(j).getSubjects().get(0).toString(); } // If the plan is for a room, we just need the time and the // teachers name if (CPlaner.selRoom != null) { table.addCell(teacher); // If the plan is for a class, we need the time, the // room and the teachers name } else if (CPlaner.selClass != null) { table.addCell(room + ", " + teacher); // For a teachers plan, we need the room and the subject } else { table.addCell(room + ", " + subject); } } else { // If it's not the first day, we don't need to print the // start time again String room = " "; // Check again whether a room is given if (tmp.get(j).getRooms().size() < 1) { room = " "; } else { room = tmp.get(j).getRooms().get(0).toString(); } String teacher = " "; if (tmp.get(j).getPerson() == null) { teacher = " "; } else { teacher = tmp.get(j).toString(); } String subject = " "; if (tmp.get(j).getSubjects().size() < 1) { subject = " "; } else { subject = tmp.get(j).getSubjects().get(0).toString(); } // If the plan is for a room, we just need the time and the // teachers name if (CPlaner.selRoom != null) { table.addCell(teacher); // If the plan is for a class, we need the time, the // room and the teachers name } else if (CPlaner.selClass != null) { table.addCell(room + ", " + teacher); // For a teachers plan, we need the room and the subject } else { table.addCell(room + ", " + subject); } } } } // Add the whole table we've just created to the pdf file subCatPart.add(table); } /** * We create the table we want to add to our pdf * * @param subCatPart * @throws BadElementException */ private static void createTable(Section subCatPart, Room room) throws BadElementException { // Get the number of days per Week from the Settings int daysPerWeek = App.theSettings.getDaysPerWeek(); PdfPTable table = new PdfPTable(daysPerWeek + 1); String[] WeekDay = { "Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" }; for (int i = 0; i < daysPerWeek + 1; i++) { PdfPCell c1 = new PdfPCell(new Phrase(WeekDay[i])); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); if (i == daysPerWeek) { table.setHeaderRows(1); } } double calc = 0; // Simplify the Date since we just need the time SimpleDateFormat df = new SimpleDateFormat("HH:mm"); Date start = null; Date uSize = null; try { start = df.parse(App.theSettings.getStartingTimeH() + ":" + App.theSettings.getStartingTimeM()); uSize = new Date(MINUTES.toMillis(App.theSettings.getUnitsSize())); } catch (ParseException e) { } // We take the first day to get the number of units and so the number of // times we have to run this. for (int j = 0; j < Tools.calcRows(); j++) { calc = j == 0 ? start.getTime() : (calc + (uSize.getTime())); // For each day available, we run this loop once. for (int k = 1; k < daysPerWeek + 1; k++) { Units u = room.getUnitByRowCol(k, j); // If it's the first day, we also want a first cell per line // containing the time this unit starts if (k == 1) { table.addCell(df.format(calc)); // Check whether a room is given String teacher = " "; if (u != null) { teacher = u.toString(); } table.addCell(teacher); } else { // If it's not the first day, we don't need to print the // start time again String teacher = " "; if (u != null) { teacher = u.toString(); } table.addCell(teacher); } } } // Add the whole table we've just created to the pdf file subCatPart.add(table); } private static void createDayTable(Section subCatPart) throws BadElementException { List<Person> personList = App.theSemester.getPersonList(); PdfPTable table = new PdfPTable(personList.size() + 1); PdfPCell c1 = new PdfPCell(new Phrase(" ")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); for (Person p : personList) { c1 = new PdfPCell(new Phrase(p.toString())); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); int k = 1; // We take the first person to get the number of units and so the // number // of // times we have to run this. for (int j = 1; j < p.getEntity().size(); j++) { // For each person available, we run this loop once. List<Units> tmp = p.getEntity(); if (k == 1) { SimpleDateFormat df = new SimpleDateFormat("HH:mm"); String time = df.format(tmp.get(j).getTime()); table.addCell(time); String classes = " "; // Check whether a class is given if (tmp.get(j).getClasses().size() < 1) { classes = " "; } else { classes = tmp.get(j).getClasses().get(0).toString(); } table.addCell(classes); } else { // If it's not the first day, we don't need to print the // start time again String classes = " "; // Check whether a class is given if (tmp.get(j).getClasses().size() < 1) { classes = " "; } else { classes = tmp.get(j).getClasses().get(0).toString(); } table.addCell(classes); } k = k + 1; } } // Add the whole table we've just created to the pdf file subCatPart.add(table); } /** * Just a function to add a number empty lines * * @param paragraph * @param number */ private static void addEmptyLine(Chapter chapter, int number) { for (int i = 0; i < number; i++) { chapter.add(new Paragraph(" ")); } } /** * add meta information to pdf file * * @param document */ private static void addMetaData(Document document) { document.addTitle(" "); document.addSubject("Using iText PDF, licensed under AGPL"); document.addKeywords(" "); document.addAuthor(" "); document.addCreator(" "); } }