Java tutorial
/** * Copyright 2014 - Vincent Zurczak * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.vzurczak.timesheetgenerator; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; /** * @author Vincent Zurczak */ public class PdfGenerator { final SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMMM yyyy"); /** * Creates a PDF document. * @param bean a generation bean (not null) * @throws DocumentException * @throws FileNotFoundException */ public void createDocument(GenerationDataBean bean) throws FileNotFoundException, DocumentException { // Create the document File outputFile = new File("./Feuille-De-Temps.pdf"); final Document doc = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(doc, new FileOutputStream(outputFile)); doc.open(); doc.addAuthor(bean.getName()); doc.addCreator(bean.getName()); String s; if (bean.getEndWeek() - bean.getStartWeek() > 1) s = "Feuilles de Temps - Semaines " + bean.getStartWeek() + " " + bean.getEndWeek(); else s = "Feuille de Temps - Semaine " + bean.getStartWeek(); doc.addTitle(s); doc.addSubject(s); // Add pages for (int i = bean.getStartWeek(); i <= bean.getEndWeek(); i++) addPageForWeek(i, doc, bean); // That's it! doc.close(); } /** * Adds a page for a given week. * @param i the week number * @param doc the document to update * @param bean a generation bean (not null) * @throws DocumentException */ private void addPageForWeek(int weekNumber, Document doc, GenerationDataBean bean) throws DocumentException { doc.newPage(); final Font boldFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD); final Font normalFont = FontFactory.getFont(FontFactory.HELVETICA); Calendar calendar = Utils.findCalendar(weekNumber); // Title Paragraph paragraph = new Paragraph("Bordereau de Dclaration des Temps", boldFont); paragraph.setAlignment(Element.ALIGN_CENTER); doc.add(paragraph); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); // Meta: week final PdfPTable metaTable = new PdfPTable(1); paragraph = new Paragraph(); paragraph.add(new Chunk("Semaine : ", boldFont)); paragraph.add(new Chunk(String.valueOf(weekNumber), normalFont)); PdfPCell c = new PdfPCell(paragraph); c.setBorder(Rectangle.NO_BORDER); metaTable.addCell(c); // Meta: date Calendar endOfWeekCalendar = ((Calendar) calendar.clone()); endOfWeekCalendar.add(Calendar.DATE, 4); String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(endOfWeekCalendar.getTime()); paragraph = new Paragraph(); paragraph.add(new Chunk("Date : ", boldFont)); paragraph.add(new Chunk(formattedDate, normalFont)); c = new PdfPCell(paragraph); c.setBorder(Rectangle.NO_BORDER); metaTable.addCell(c); doc.add(metaTable); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); // Signatures final PdfPTable signaturesTable = new PdfPTable(2); paragraph = new Paragraph(); paragraph.add(new Chunk("Nom : ", boldFont)); paragraph.add(new Chunk(bean.getName(), normalFont)); c = new PdfPCell(paragraph); c.setBorder(Rectangle.NO_BORDER); signaturesTable.addCell(c); paragraph = new Paragraph(); paragraph.add(new Chunk("Responsable : ", boldFont)); paragraph.add(new Chunk(bean.getManagerName(), normalFont)); c = new PdfPCell(paragraph); c.setBorder(Rectangle.NO_BORDER); signaturesTable.addCell(c); c = new PdfPCell(new Paragraph("Signature : ", boldFont)); c.setBorder(Rectangle.NO_BORDER); signaturesTable.addCell(c); c = new PdfPCell(new Paragraph("Signature : ", boldFont)); c.setBorder(Rectangle.NO_BORDER); signaturesTable.addCell(c); doc.add(signaturesTable); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); // Calendar final PdfPTable timeTable = new PdfPTable(7); timeTable.addCell(new PdfPCell()); for (int i = 0; i < 5; i++) { final String date = this.sdf.format(calendar.getTime()); timeTable.addCell(newCell(date, 10)); calendar.add(Calendar.DATE, 1); } timeTable.addCell(newCell("Total", 10)); timeTable.addCell(newCell("Heures Effectues", 20)); for (int i = 0; i < 5; i++) timeTable.addCell(newCell("", 20)); if (bean.getTotalHours() > 0) timeTable.addCell(newCell(bean.getTotalHours() + " h", 20)); timeTable.completeRow(); doc.add(timeTable); } private PdfPCell newCell(String content, int padding) { PdfPCell c = new PdfPCell(new Phrase(content)); c.setHorizontalAlignment(Element.ALIGN_CENTER); c.setPaddingBottom(padding); c.setPaddingTop(padding); return c; } }