List of usage examples for com.itextpdf.text.pdf PdfPCell setBorder
public void setBorder(final int border)
From source file:Controller.PrintOrderManagedBean.java
public void executePDF(String maDH) { try {//w ww.jav a 2 s . c o m DonHang donhang = new DonHang(); donhang.init(maDH); float CONVERT = 28.346457f;// 1 cm FacesContext faces = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse(); // setting some response headers response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); //response.setHeader("Content-disposition","inline; filename=kiran.pdf"); response.setHeader("Pragma", "public"); response.setContentType("application/pdf"); //response.setHeader("Content-Disposition", "attachment;filename=\"ContactList.pdf\""); response.addHeader("Content-disposition", "attachment;filename=\"DataListBean.pdf\""); //step 1: creation of a document-object Document document = new Document(PageSize.A4, 0.5f * CONVERT, 0.5f * CONVERT, 1.0f * CONVERT, 1.0f * CONVERT); //step 2: we create a writer that listens to the document // and directs a PDF-stream to a temporary buffer ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); PdfWriter writer = PdfWriter.getInstance(document, baos); BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(bf, 18, Font.BOLD); Font font11 = new Font(bf, 11, Font.NORMAL); Font font11_bo = new Font(bf, 11, Font.BOLD); Font font12 = new Font(bf, 12, Font.NORMAL); Font font10 = new Font(bf, 10, Font.NORMAL); Font font9 = new Font(bf, 9, Font.NORMAL); //step 3: we open the document document.open(); PdfPTable tab_Header1; tab_Header1 = new PdfPTable(1); tab_Header1.setWidthPercentage(100); tab_Header1.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell1; cell1 = new PdfPCell(new Phrase("CNG TY TNHH ABC FASHION", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell( new Phrase("?a ch: 146 Linh Trung,P. Linh Trung, Q. Th ?c, TP HCM", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S?T: 0909465621", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?N GIAO HNG", font12)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Bn:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn: CNG TY TNHH ABC FASHION", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell( new Phrase("?a ch: 146 Linh Trung,P. Linh Trung, Q. Th ?c, TP HCM", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi: 0909465621", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Mua:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn: " + donhang.getTenKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?a ch: " + donhang.getDiaChiKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi: " + donhang.getSoDTKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Vn chuyn:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn:....................................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?a ch:...............................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi:...............................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Danh sch hng ha:", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); ///////////////////////////////bn sn phm float[] crDonHang = { 1.0f * CONVERT, 4.0f * CONVERT, 1.0f * CONVERT, 2.0f * CONVERT, 2.0f * CONVERT }; PdfPTable tab_Header2; tab_Header2 = new PdfPTable(crDonHang.length); tab_Header2.setWidthPercentage(100); tab_Header2.setWidths(crDonHang); tab_Header2.setHorizontalAlignment(Element.ALIGN_CENTER); NumberFormat formatter = new DecimalFormat("#,###,###"); String[] crheader = { "STT", "Tn Hng", "S Lng", "Gi Bn(VN?)", "Thnh Ti?n(VN?)" }; for (int i = 0; i < crheader.length; i++) { PdfPCell cell = new PdfPCell(new Phrase(crheader[i], font11)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header2.addCell(cell); } int stt = 1; for (SanPhamDH sp : donhang.getListSP()) { PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(stt), font11)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(sp.getTenSP(), font11)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(sp.getSoluong(), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(formatter.format(Double.parseDouble(sp.getGiaSP())), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(formatter.format(Double.parseDouble(sp.getThanhTien())), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); stt++; } cell1 = new PdfPCell(tab_Header2); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n hng: " + formatter.format( Double.parseDouble(donhang.getTienTamTinh() == null ? "0" : donhang.getTienTamTinh())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n vn chuyn: " + formatter.format(Double .parseDouble(donhang.getTienVanChuyen() == null ? "0" : donhang.getTienVanChuyen())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n thanh ton: " + formatter.format(Double.parseDouble(donhang.getTongTien())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S ti?n thanh ton bng ch: " + DocTien.doctien(donhang.getTongTien().replaceAll(" ", "")) + "ng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase(" ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("TP H Ch Minh, ngy thng nm ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingRight(1.0f * CONVERT); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase(" ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); PdfPTable tab_Header3; tab_Header3 = new PdfPTable(3); tab_Header3.setWidthPercentage(100); tab_Header3.setHorizontalAlignment(Element.ALIGN_CENTER); cell1 = new PdfPCell(new Phrase("Ng?i nhn hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("Ng?i giao hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("Ng?i bn hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(tab_Header3); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBorder(0); tab_Header1.addCell(cell1); document.add(tab_Header1); document.close(); //step 6: we output the writer as bytes to the response output // the contentlength is needed for MSIE!!! response.setContentLength(baos.size()); // write ByteArrayOutputStream to the ServletOutputStream ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); baos.flush(); faces.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Controllers.ExportController.java
public static void exportExam(String path) { model = (DefaultTableModel) tableExam.getModel(); int selectRow = tableExam.getSelectedRow(); if (selectRow != -1) { int idExam = (int) model.getValueAt(selectRow, 0) - 1; exams = ExamModel.readExam();//from w ww .j ava 2 s.co m Exam ex = exams.getExam(idExam); try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); BaseFont f = BaseFont.createFont("/font/vuArial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font titleExamFont = new Font(f, 25.0f, Font.BOLD); Font titlePartFont = new Font(f, 18.0f, Font.BOLD); Font headFont = new Font(f, 13.0f, Font.BOLD); Font suggestionFont = new Font(f, 13.0f, Font.ITALIC); Font contentFont = new Font(f, 13.0f, Font.NORMAL); Paragraph align = new Paragraph(" "); Paragraph title = new Paragraph(ex.getNameExam(), titleExamFont); title.setAlignment(Paragraph.ALIGN_CENTER); document.add(title); document.add(align); ArrayList<Question> question = ex.getQuestions(); boolean haveMultipleChoice = false; boolean haveEssay = false; // Kim tra xem c phn t lun khng for (Question q : question) { if (q instanceof Essay) { haveEssay = true; } // Kim tra xem c phn trc nghim khng} else { haveMultipleChoice = true; } if (haveEssay && haveMultipleChoice) break; } int count; if (haveMultipleChoice) { count = 0; Paragraph titlePart = new Paragraph("Trc nghim", titlePartFont); document.add(align); document.add(titlePart); document.add(align); for (Question q : question) if (q instanceof MultipleChoice) { count++; Phrase numberQuestion = new Phrase("Cu " + count + ": ", headFont); Phrase contentQuestion = new Phrase(q.getContentQuestion(), contentFont); Paragraph questionParagraph = new Paragraph(); questionParagraph.add(numberQuestion); questionParagraph.add(contentQuestion); document.add(questionParagraph); MultipleChoice mc = (MultipleChoice) q; ArrayList<Answer> answers = mc.getAnswers(); boolean ok = true; for (int i = 0; i < answers.size(); ++i) { Answer answer = answers.get(i); if (answer.getContentAnswer().length() > 30) ok = false; } if (ok == true) { PdfPTable table = new PdfPTable(2); for (int i = 0; i < answers.size(); ++i) { Answer answer = answers.get(i); PdfPCell answerParagraph = new PdfPCell(new Paragraph( (char) (65 + i) + ". " + answer.getContentAnswer(), contentFont)); answerParagraph.setBorder(Rectangle.NO_BORDER); table.addCell(answerParagraph); } document.add(table); } else { PdfPTable table = new PdfPTable(1); for (int i = 0; i < answers.size(); ++i) { Answer answer = answers.get(i); PdfPCell answerParagraph = new PdfPCell(new Paragraph( (char) (65 + i) + ". " + answer.getContentAnswer(), contentFont)); answerParagraph.setBorder(Rectangle.NO_BORDER); table.addCell(answerParagraph); } document.add(table); } } } if (haveEssay) { count = 0; Paragraph titlePart = new Paragraph("T Lun", titlePartFont); document.add(align); document.add(titlePart); document.add(align); for (Question q : question) if (q instanceof Essay) { count++; Phrase numberQuestion = new Phrase("Cu " + count + ": ", headFont); Phrase contentQuestion = new Phrase(q.getContentQuestion(), contentFont); Paragraph questionParagraph = new Paragraph(); questionParagraph.add(numberQuestion); questionParagraph.add(contentQuestion); Essay es = (Essay) q; Phrase headerSuggestion = new Phrase("Gi : ", suggestionFont); Phrase contentSuggestion = new Phrase(es.getSuggest(), contentFont); Paragraph suggestion = new Paragraph(); suggestion.add(headerSuggestion); suggestion.add(contentSuggestion); document.add(questionParagraph); document.add(suggestion); } } document.close(); } catch (FileNotFoundException exp) { exp.printStackTrace(); } catch (DocumentException exp) { exp.printStackTrace(); } catch (IOException exp) { exp.printStackTrace(); } } }
From source file:cz.zcu.kiv.eegdatabase.logic.pdf.ReservationPDF.java
License:Apache License
public static PdfPTable formatReservation(Reservation reservation) { int padding = 5; GregorianCalendar created = new GregorianCalendar(); created.setTime(reservation.getCreationTime()); GregorianCalendar startTime = new GregorianCalendar(); startTime.setTime(reservation.getStartTime()); GregorianCalendar endTime = new GregorianCalendar(); endTime.setTime(reservation.getEndTime()); String personName = BookingRoomUtils.formatPersonName(reservation.getPerson()); Font title = FontFactory.getFont("Trebuchet MS", "utf-8", 15, Font.BOLD, new BaseColor(59, 70, 00)); Font header = FontFactory.getFont(FontFactory.TIMES_BOLD, 13, Font.BOLD, BaseColor.BLACK); Font value = FontFactory.getFont(FontFactory.TIMES, 13); PdfPTable table = new PdfPTable(2); PdfPCell cell; Phrase phrase;/*from w ww.ja v a 2s.c o m*/ phrase = new Phrase("Reservation by " + personName); phrase.setFont(title); cell = new PdfPCell(phrase); cell.setColspan(2); cell.setBorder(0); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidthTop(1); cell.setBorderWidthLeft(1); cell.setBorderWidthRight(1); cell.setPadding(padding); cell.setPaddingBottom(5); table.addCell(cell); phrase = new Phrase("Date: "); phrase.setFont(header); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthLeft(1); cell.setPadding(padding); table.addCell(cell); phrase = new Phrase(BookingRoomUtils.getDate(startTime)); phrase.setFont(value); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthRight(1); cell.setPadding(padding); table.addCell(cell); phrase = new Phrase("Start: "); phrase.setFont(header); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthLeft(1); cell.setPadding(padding); table.addCell(cell); phrase = new Phrase(BookingRoomUtils.getHoursAndMinutes(startTime)); phrase.setFont(value); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthRight(1); cell.setPadding(padding); table.addCell(cell); phrase = new Phrase("End: "); phrase.setFont(header); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthLeft(1); cell.setBorderWidthBottom(1); cell.setPadding(padding); table.addCell(cell); phrase = new Phrase(BookingRoomUtils.getHoursAndMinutes(endTime)); phrase.setFont(value); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); cell.setBorderWidthRight(1); cell.setBorderWidthBottom(1); cell.setPadding(padding); table.addCell(cell); table.setSpacingBefore(10); table.setSpacingAfter(10); return table; }
From source file:de.aidger.utils.pdf.ActivityReportConverter.java
License:Open Source License
/** * Writes the logos and the address of the institute. *//* w w w.j a va 2 s . c o m*/ private void writeLogo() { try { Font generatedByFont = new Font( BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 8); Image aidger = Image.getInstance(getClass().getResource("/de/aidger/res/pdf/AidgerLogo.png")); aidger.scaleAbsolute(80.0f, 20.0f); PdfPTable table = new PdfPTable(2); table.setTotalWidth(reader.getPageSize(1).getRight()); PdfPCell cell = new PdfPCell(new Phrase(_("Generated by: "), generatedByFont)); cell.setBorder(0); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setPaddingBottom(5); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); table.addCell(cell); cell = new PdfPCell(Image.getInstance(aidger)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); table.addCell(cell); table.writeSelectedRows(0, -1, 0, 25, contentByte); } catch (BadElementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.aidger.utils.pdf.ActivityReportConverter.java
License:Open Source License
/** * Creates the table of employments./*from ww w . jav a2 s . co m*/ */ private void createTable() { try { Font tableTitleFont = new Font( BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED), 10); String[] tableTitles = { "Zeitraum", "Veranstaltung", "Umfang" }; PdfPTable contentTable = new PdfPTable(1); PdfPTable titleTable = new PdfPTable(new float[] { 0.2f, 0.6f, 0.2f }); /* * Create the titles of the table entries. */ for (int i = 0; i < tableTitles.length; i++) { PdfPCell cell = new PdfPCell(new Phrase(tableTitles[i], tableTitleFont)); titleTable.addCell(cell); } PdfPCell cell = new PdfPCell(titleTable); cell.setPaddingTop(10.0f); cell.setPaddingBottom(2.0f); cell.setBorder(0); contentTable.addCell(cell); cell = new PdfPCell(addRows()); cell.setBorder(0); contentTable.addCell(cell); float xPos = 60, yPos = 500, width = reader.getPageSize(1).getWidth() - 120f; if (form.getFieldPositions("TableField") != null) { FieldPosition position = form.getFieldPositions("TableField").get(0); xPos = position.position.getLeft(); yPos = position.position.getTop(); width = position.position.getWidth(); } contentTable.setTotalWidth(width); contentTable.writeSelectedRows(0, -1, xPos, yPos, contentByte); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.aidger.utils.pdf.BalanceReportConverter.java
License:Open Source License
/** * Writes a semester table and adds the groups of that semester to it. * /*w w w. j a va 2 s . c o m*/ * @param semester * The name of the semester to be added. */ @SuppressWarnings("unchecked") private void createSemester(String semester) { balanceReportGroups = new ArrayList<ArrayList>(); try { Font semesterTitleFont = new Font( BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED), 13); Font semesterNameFont = new Font( BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 13); PdfPTable finalSemesterTable = new PdfPTable(1); PdfPTable semesterTitleTable = new PdfPTable(new float[] { 0.2f, 0.8f }); PdfPCell semesterCell = new PdfPCell(new Phrase(_("Semester"), semesterTitleFont)); semesterCell.setBorder(0); semesterTitleTable.addCell(semesterCell); semesterCell = new PdfPCell(new Phrase(semester, semesterNameFont)); semesterCell.setBorder(0); semesterTitleTable.addCell(semesterCell); semesterCell = new PdfPCell(semesterTitleTable); semesterCell.setBorder(0); semesterCell.setPaddingTop(7.0f); finalSemesterTable.addCell(semesterCell); PdfPTable contentTable = new PdfPTable(1); /* * As long as there are groups for this Balance report, create new * group tables. */ List<Course> courses = null; if (semester != null) { courses = (new Course()).getCoursesBySemester(semester); } else { courses = new ArrayList<Course>(); List<Course> unsortedCourses = new Course().getAll(); for (Course course : unsortedCourses) { if (course.getSemester() == null) { courses.add(new Course(course)); } } } List<Course> filteredCourses = balanceHelper.filterCourses(courses, filters); for (Course course : filteredCourses) { PdfPTable groupTable = null; if (balanceReportGroups.isEmpty()) { groupTable = createGroup(course); } else { boolean foundGroup = false; for (int i = 0; i <= balanceReportGroups.size() - 1; i++) { if (((ArrayList) balanceReportGroups.get(i)).get(1).equals(course.getGroup())) { foundGroup = true; break; } } if (!foundGroup) { groupTable = createGroup(course); } } } for (int i = 0; i <= balanceReportGroups.size() - 1; i++) { PdfPCell cell = new PdfPCell((PdfPTable) ((ArrayList) balanceReportGroups.get(i)).get(0)); cell.setBorder(0); cell.setPaddingBottom(8.0f); contentTable.addCell(cell); } PdfPCell contentCell = new PdfPCell(contentTable); contentCell.setPaddingLeft(10.0f); contentCell.setBorder(1); finalSemesterTable.addCell(contentCell); finalSemesterTable.setKeepTogether(true); document.add(finalSemesterTable); } catch (Exception e) { Logger.error(e.getMessage()); } }
From source file:de.aidger.utils.pdf.BalanceReportConverter.java
License:Open Source License
/** * Creates a row with the data of one course and returns it. * //from w w w . j av a 2 s . co m * @param course * The course of which the data shall be written to a row. * @return The row as a PdfPCell */ private PdfPCell addRow(BalanceCourse balanceCourse, float[] columnwidths, ArrayList<Integer> costUnits) { PdfPTable groupContentTable = new PdfPTable(columnwidths); ArrayList<Object> rowObjectVector = new ArrayList<Object>(); Font tableContentFont; try { tableContentFont = new Font(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 9); for (int i = 0; i < 6; i++) { rowObjectVector.add((balanceCourse).getCourseObject()[i]); } sums.set(4, (Double) sums.get(4) + (Double) rowObjectVector.get(4)); sums.set(5, (Double) sums.get(5) + (Double) rowObjectVector.get(5)); int i = 0; for (Integer costUnit : costUnits) { boolean foundCostUnit = false; for (BudgetCost budgetCost : balanceCourse.getBudgetCosts()) { int budgetCostId = budgetCost.getId(); /* * Set the budget cost of the cost unit to the one * specified. */ if (budgetCostId == costUnit) { rowObjectVector.add(budgetCost.getValue()); foundCostUnit = true; } } if (!foundCostUnit) { rowObjectVector.add(0.0); } if (sums.size() < rowObjectVector.size()) { sums.add(rowObjectVector.get(rowObjectVector.size() - 1)); } else { sums.set(rowObjectVector.size() - 1, (Double) sums.get(rowObjectVector.size() - 1) + (Double) rowObjectVector.get(rowObjectVector.size() - 1)); } } for (Object courseObject : rowObjectVector.toArray()) { PdfPCell cell = null; if (courseObject.getClass().equals(Double.class)) { /* * Round to the configured precision. */ int decimalPlace = Integer.parseInt(Runtime.getInstance().getOption("rounding", "2")); double rounded = new BigDecimal((Double) courseObject) .setScale(decimalPlace, BigDecimal.ROUND_HALF_EVEN).doubleValue(); cell = new PdfPCell( new Phrase(new BigDecimal(rounded).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString(), tableContentFont)); } else { cell = new PdfPCell(new Phrase(courseObject.toString(), tableContentFont)); } if (i != 0) { cell.setBorder(4 + Rectangle.BOTTOM); } else { cell.setBorder(0 + Rectangle.BOTTOM); i++; } cell.setPaddingBottom(5); groupContentTable.addCell(cell); } PdfPCell cell = new PdfPCell(groupContentTable); cell.setBorder(0); return cell; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:de.aidger.utils.pdf.BalanceReportConverter.java
License:Open Source License
/** * Creates a new group table with the title of the group. Adds the table and * its title to the group table vector./*from w ww . j a v a 2s .c o m*/ * * @param course * The course for which a group shall be created. * @return The PdfPTable of the group. */ @SuppressWarnings("unchecked") private PdfPTable createGroup(Course course) { balanceReportGroupCreator = new BalanceReportGroupCreator(course, calculationMethod); List<Course> courses = null; sums = new ArrayList<Object>(); try { if (course.getSemester() != null) { courses = (new Course()).getCoursesBySemester(course.getSemester()); } else { courses = new ArrayList<Course>(); List<Course> unsortedCourses = new Course().getAll(); for (Course currentCourse : unsortedCourses) { if (currentCourse.getSemester() == null) { courses.add(new Course(currentCourse)); } } } } catch (SienaException e) { UI.displayError(e.toString()); } List<Course> filteredCourses = balanceHelper.filterCourses(courses, filters); ArrayList<Long> addedCourses = new ArrayList<Long>(); addedCourses.add(course.getId()); for (Course filteredCourse : filteredCourses) { if (!addedCourses.contains(filteredCourse.getId()) && filteredCourse.getGroup().equals(course.getGroup())) { balanceReportGroupCreator.addCourse(filteredCourse); addedCourses.add(filteredCourse.getId()); } } ArrayList<Integer> costUnits = new ArrayList<Integer>(); ArrayList<BalanceCourse> balanceCourses = balanceReportGroupCreator.getBalanceCourses(); ArrayList<String> titles = new ArrayList<String>(); String[] courseTitles = { _("Title"), _("Part"), _("Lecturer"), _("Target Audience"), _("Planned AWS"), _("Basic needed AWS") }; for (int i = 0; i < courseTitles.length; i++) { titles.add(courseTitles[i]); } for (Object balanceCourse : balanceCourses) { for (BudgetCost budgetCost : ((BalanceCourse) balanceCourse).getBudgetCosts()) { int budgetCostId = budgetCost.getId(); String budgetCostName = budgetCost.getName(); if (!costUnits.contains(budgetCostId)) { costUnits.add(budgetCostId); titles.add(_("Budget costs from") + " " + budgetCostName); } } } int columnCount = titles.size(); Font tableTitleFont; Font tableContentFont; try { tableContentFont = new Font(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 9); tableTitleFont = new Font( BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED), 9); Font groupTitleFont = new Font( BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED), 11); Font groupNameFont = new Font( BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 11); PdfPTable groupTable = new PdfPTable(1); PdfPTable groupNameTable = new PdfPTable(new float[] { 0.2f, 0.8f }); PdfPCell groupTitle = new PdfPCell(new Phrase(_("Group"), groupTitleFont)); groupTitle.setBorder(2); groupNameTable.addCell(groupTitle); PdfPCell groupName = new PdfPCell(new Phrase(course.getGroup(), groupNameFont)); groupName.setBorder(2); groupNameTable.addCell(groupName); PdfPCell groupContent = new PdfPCell(groupNameTable); groupContent.setBorder(0); groupContent.setPaddingTop(3.0f); groupContent.setPaddingBottom(2.0f); groupTable.addCell(groupContent); float[] columnWidths = new float[columnCount]; columnWidths[0] = (200 / columnCount); columnWidths[1] = (50 / columnCount); columnWidths[2] = (100 / columnCount); columnWidths[3] = (150 / columnCount); columnWidths[4] = (100 / columnCount); for (int i = 5; i < columnCount; i++) { columnWidths[i] = (100 / columnCount); } PdfPTable groupContentTable = new PdfPTable(columnWidths); /* * Create the titles of the table entries. */ for (int i = 0; i < titles.size(); i++) { PdfPCell cell = new PdfPCell(new Phrase(titles.get(i), tableTitleFont)); if (i != 0) { cell.setBorder(6); } else { cell.setBorder(2); } groupContentTable.addCell(cell); } PdfPCell cell = new PdfPCell(groupContentTable); cell.setBorder(0); groupTable.addCell(cell); sums.add(_("Sum")); sums.add(""); sums.add(""); sums.add(""); sums.add(0.0); sums.add(0.0); for (Object balanceCourse : balanceCourses) { groupTable.addCell(addRow((BalanceCourse) balanceCourse, columnWidths, costUnits)); } PdfPTable emptyRow = new PdfPTable(columnWidths); PdfPTable sumRow = new PdfPTable(columnWidths); for (int i = 0; i < sums.size(); i++) { cell = new PdfPCell(new Phrase("")); if (i != 0) { cell.setBorder(Rectangle.TOP + Rectangle.LEFT); } else { cell.setBorder(Rectangle.TOP); } emptyRow.addCell(cell); if (i < 4) { cell = new PdfPCell(new Phrase(new Phrase(sums.get(i).toString(), tableContentFont))); } else { /* * Round to the configured precision. */ int decimalPlace = Integer.parseInt(Runtime.getInstance().getOption("rounding", "2")); double rounded = new BigDecimal((Double) sums.get(i)) .setScale(decimalPlace, BigDecimal.ROUND_HALF_EVEN).doubleValue(); cell = new PdfPCell( new Phrase(new BigDecimal(rounded).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString(), tableContentFont)); } if (i != 0) { cell.setBorder(Rectangle.TOP + Rectangle.LEFT); } else { cell.setBorder(Rectangle.TOP); } sumRow.addCell(cell); } cell = new PdfPCell(emptyRow); cell.setBorder(0); groupTable.addCell(cell); cell = new PdfPCell(sumRow); cell.setBorder(0); groupTable.addCell(cell); groupTable.setKeepTogether(true); balanceReportGroups.add(new ArrayList<Object>()); int i = balanceReportGroups.size() - 1; ((ArrayList) balanceReportGroups.get(i)).add(groupTable); ((ArrayList) balanceReportGroups.get(i)).add(course.getGroup()); return groupTable; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:de.aidger.utils.pdf.BudgetReportConverter.java
License:Open Source License
/** * Creates all the course budgets and writes them into a table. *//*w w w . j a va 2 s . co m*/ private void createCourses() { try { PdfPTable contentTable = new PdfPTable(1); PdfPCell contentCell = new PdfPCell(createTableHeader()); contentCell.setBorder(0); contentTable.addCell(contentCell); BudgetCreator budgetCreator = new BudgetCreator(); List<Course> courses = (new Course()).getAll(); /* * Get course budgets for all courses along with the filter * criteria. */ for (Course course : courses) { budgetCreator.addCourseBudget(new Course(course), filters); } ArrayList<CourseBudget> courseBudgets = budgetCreator.getCourseBudgets(); /* * Add a new row for every course that passed the filters. */ for (CourseBudget courseBudget : courseBudgets) { contentCell = new PdfPCell(addRow(budgetCreator.getObjectArray(courseBudget))); contentCell.setBorder(0); contentTable.addCell(contentCell); } document.add(contentTable); } catch (SienaException e) { UI.displayError(e.toString()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.aidger.utils.pdf.BudgetReportConverter.java
License:Open Source License
/** * Creates the titles of the table./*from www . j ava2 s. c o m*/ * * @return The table with the titles. */ private PdfPTable createTableHeader() { try { Font tableTitleFont = new Font( BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED), 9); PdfPTable tableHeader = new PdfPTable(new float[] { 0.3f, 0.15f, 0.15f, 0.15f, 0.15f, 0.10f }); String[] titles = { _("Course"), _("Semester"), _("Lecturer"), _("Booked budgets"), _("Available budgets"), _("Total Budgets") }; /* * Add the title of every column. */ for (int i = 0; i < titles.length; i++) { PdfPCell cell = new PdfPCell(new Phrase(titles[i], tableTitleFont)); tableHeader.addCell(cell); } PdfPTable tableHeaderTable = new PdfPTable(1); PdfPCell cell = new PdfPCell(tableHeader); cell.setPaddingBottom(1); cell.setBorder(0); tableHeaderTable.addCell(cell); return tableHeaderTable; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }