Java tutorial
package org.tvd.thptty.management.report; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import org.tvd.exel.model.TVDRow; import org.tvd.thptty.management.temporary.WebKeys; import org.tvd.thptty.management.util.ActionUtil; import org.tvd.thptty.management.util.TYServiceUtil; import org.tvd.thptty.model.TYClass; import org.tvd.thptty.model.TYStudent; import org.tvd.thptty.model.TYStudentPoint; import org.tvd.thptty.model.TYSubject; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.List; import com.itextpdf.text.ListItem; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.liferay.portal.model.User; public class Report { public void createFile(int kind) { Document document = new Document(); document.setPageSize(PageSize.A4); if (kind == 1) { document.setMargins(37, 37, 37, 37); } else if (kind == 2) { } try { File file = new File(getFullPathFile()); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); addMetaData(document); //addTitlePage(document); addContent(document, kind); } catch (Exception e) { e.printStackTrace(); } finally { document.close(); } } // iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties private void addMetaData(Document document) { document.addTitle(title); document.addSubject(subject); document.addKeywords(keywords); document.addAuthor(author); document.addCreator(creator); } @SuppressWarnings("unused") private void addTitlePage(Document document) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header preface.add(new Paragraph("Title of the document", catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-3$ smallBold)); addEmptyLine(preface, 3); preface.add(new Paragraph("This document describes something which is very important ", smallBold)); addEmptyLine(preface, 8); preface.add(new Paragraph("This document is a preliminary " + "version and not subject to your license agreement or any other agreement with vogella.com ;-).", redFont)); document.add(preface); // Start a new page document.newPage(); } private void addContent(Document document, int kind) throws DocumentException { //Anchor anchor = new Anchor(tableTitle, tahomaLargeFont); //anchor.setName("Bang Diem"); // Second parameter is the number of the chapter //Chapter catPart = new Chapter(new Paragraph(anchor), 1); //Paragraph subPara = new Paragraph(createdBy, tahomaLargeFont); Paragraph header = new Paragraph(tableTitle, tahomaLargeFont); header.setAlignment(Element.ALIGN_LEFT); header.add(new Paragraph(createdBy, tahomaLargeFont)); addEmptyLine(header, 1); //Section subCatPart = catPart.addSection(subPara); Paragraph paragraph = new Paragraph(); // Add a table if (kind == 1) { paragraph.add(createSubjectPointTable()); } else if (kind == 2) { paragraph.add(createStatisticsStudentPointTable()); } header.add(paragraph); // Now add all this to the document document.add(header); } private PdfPTable createSubjectPointTable() throws BadElementException { int countCell = cellTitles.length; PdfPTable table = new PdfPTable(countCell); table.setWidthPercentage(100); for (int i = 0; i < countCell; i++) { Phrase phrase = new Phrase(cellTitles[i], tahomaFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cx); } table.setHeaderRows(1); Phrase phrase = null; java.util.List<TYStudent> students = ActionUtil.getStudentsInClass(courses, tyClass.getClassId()); for (int i = 0; i < students.size(); i++) { //index phrase = new Phrase(String.valueOf(i + 1), tahomaFont); table.addCell(phrase); //student full name TYStudent student = students.get(i); student.setFullName(student.getStudentFirstName() + " " + student.getStudentLastName()); phrase = new Phrase(student.getFullName(), tahomaFont); table.addCell(phrase); //speak point float points[] = TYServiceUtil.getPointStudentByPTPF(courses, semester, student.getStudentId(), tySubject.getSubjectId(), WebKeys.SPEAK_POINT, 1); String pointString = TYServiceUtil.floatsToPointString(points); phrase = new Phrase(pointString, smallBold); table.addCell(phrase); //15' point points = TYServiceUtil.getPointStudentByPTPF(courses, semester, student.getStudentId(), tySubject.getSubjectId(), WebKeys.WRITE_POINT, 1); pointString = TYServiceUtil.floatsToPointString(points); phrase = new Phrase(pointString, smallBold); table.addCell(phrase); //45' point points = TYServiceUtil.getPointStudentByPTPF(courses, semester, student.getStudentId(), tySubject.getSubjectId(), WebKeys.WRITE_POINT, 2); pointString = TYServiceUtil.floatsToPointString(points); phrase = new Phrase(pointString, smallBold); table.addCell(phrase); //90' point points = TYServiceUtil.getPointStudentByPTPF(courses, semester, student.getStudentId(), tySubject.getSubjectId(), WebKeys.WRITE_POINT, 3); pointString = TYServiceUtil.floatsToPointString(points); phrase = new Phrase(pointString, smallBold); table.addCell(phrase); //avg point TYStudentPoint studentPointSubject = TYServiceUtil.getStudentAVGPointBySubject(courses, semester, student.getStudentId(), tySubject.getSubjectId()); float avgPointSubject = 0; if (studentPointSubject != null) avgPointSubject = TYServiceUtil.getCutFloat(studentPointSubject.getPoint(), 1); pointString = "" + avgPointSubject; phrase = new Phrase(pointString, smallBold); table.addCell(phrase); } float totalWidth = PageSize.A4.getWidth(); float columnWidths[] = { (float) (0.05 * totalWidth), (float) (0.35 * totalWidth), (float) (0.1 * totalWidth), (float) (0.2 * totalWidth), (float) (0.1 * totalWidth), (float) (0.1 * totalWidth), (float) (0.1 * totalWidth) }; try { table.setWidthPercentage(columnWidths, PageSize.A4); } catch (DocumentException e) { e.printStackTrace(); } return table; } private PdfPTable createStatisticsStudentPointTable() throws BadElementException { PdfPTable table = new PdfPTable(15); table.setWidthPercentage(100); for (int i = 0; i < 3; i++) { Phrase phrase = new Phrase(cellTitles[i], tahomaBoldFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); cx.setVerticalAlignment(Element.ALIGN_MIDDLE); cx.setRowspan(3); table.addCell(cx); } for (int i = 3; i < 5; i++) { Phrase phrase = new Phrase(cellTitles[i], tahomaBoldFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); cx.setVerticalAlignment(Element.ALIGN_MIDDLE); cx.setColspan(6); table.addCell(cx); } for (int i = 5; i < 11; i++) { Phrase phrase = new Phrase(cellTitles[i], tahomaBoldFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); cx.setVerticalAlignment(Element.ALIGN_MIDDLE); cx.setColspan(2); table.addCell(cx); } for (int i = 11; i < 23; i++) { Phrase phrase = new Phrase(cellTitles[i], tahomaBoldFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); cx.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cx); } table.setHeaderRows(3); Phrase phrase = null; for (int i = 0; rows != null && i < rows.size(); i++) { for (int k = 0; k < rows.get(i).getListCells().size(); k++) { phrase = new Phrase(rows.get(i).getListCells().get(k).getCellName(), tahomaSmallFont); PdfPCell cx = new PdfPCell(phrase); cx.setHorizontalAlignment(Element.ALIGN_CENTER); cx.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cx); } } float totalWidth = PageSize.A4.getWidth(); float columnWidths[] = new float[15]; for (int i = 0; i < columnWidths.length; i++) { columnWidths[i] = (float) (1.0 / 15) * totalWidth; } try { table.setWidthPercentage(columnWidths, PageSize.A4); } catch (DocumentException e) { e.printStackTrace(); } return table; } @SuppressWarnings("unused") private void createList(Section subCatPart) { List list = new List(true, false, 10); list.add(new ListItem("First point")); list.add(new ListItem("Second point")); list.add(new ListItem("Third point")); subCatPart.add(list); } private void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } private BaseFont getTahomaBaseFont() { BaseFont tahoma = null; try { tahoma = BaseFont.createFont(FontPath.TAHOMA_FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return tahoma; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFullPathFile() { return filePath + "/" + fileName; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator; } public int getCourses() { return courses; } public void setCourses(int courses) { this.courses = courses; } public int getSemester() { return semester; } public void setSemester(int semester) { this.semester = semester; } public TYSubject getTySubject() { return tySubject; } public void setTySubject(TYSubject tySubject) { this.tySubject = tySubject; } public String[] getCellTitles() { return cellTitles; } public void setCellTitles(String[] cellTitles) { this.cellTitles = cellTitles; } public User getTeacher() { return teacher; } public void setTeacher(User teacher) { this.teacher = teacher; } public TYClass getTyClass() { return tyClass; } public void setTyClass(TYClass tyClass) { this.tyClass = tyClass; } public String getTableTitle() { return tableTitle; } public void setTableTitle(String tableTitle) { this.tableTitle = tableTitle; } public String getCreatedBy() { return createdBy; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } public java.util.List<TVDRow> getRows() { return rows; } public void setRows(java.util.List<TVDRow> rows) { this.rows = rows; } private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD); private Font tahomaFont = new Font(getTahomaBaseFont(), 11, Font.NORMAL); private Font tahomaSmallFont = new Font(getTahomaBaseFont(), 10, Font.NORMAL); private Font tahomaBoldFont = new Font(getTahomaBaseFont(), 11, Font.BOLD); private Font tahomaLargeFont = new Font(getTahomaBaseFont(), 14, Font.NORMAL); private String filePath = null; private String fileName = null; private String title = "Title"; private String subject = "Subject"; private String keywords = "Keywords"; private String author = "TVD"; private String creator = "TVD"; private String cellTitles[]; private User teacher; private TYClass tyClass; private TYSubject tySubject; private int courses; private int semester; private String tableTitle; private String createdBy; private java.util.List<TVDRow> rows; }