Java tutorial
package edu.bedelias.utils; import java.io.FileOutputStream; import java.util.Date; 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.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import edu.bedelias.entities.Curso; import edu.bedelias.entities.Evaluacion; import edu.bedelias.entities.Examen; import edu.bedelias.entities.Student; /** * Stolen from http://www.vogella.com/articles/JavaPDF/article.html * * @author Lars Vogel * * --Gas * */ public class ReportsService { private static final String PDF_TITLE = "Reporte PDF autogenerado"; private static final String PDF_SUBJECT = "Creado utilizando iText"; private static final String PDF_KEYWORDS = "Bedelias, Activiti, UdelaR, Java, PDF, iText"; private static final String PDF_AUTHOR = "SGB TecnoInf - Activiti"; private static String FILE_PATH = System.getProperty("user.home"); private static String FILE_SEPARATOR = System.getProperty("file.separator"); private static String FILE_FOLDER = FILE_PATH + FILE_SEPARATOR + "reportes" + FILE_SEPARATOR; 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 subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); public static void print(String title, String author, String subject, String keywords, String creator, String filename) { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE_FOLDER + filename)); document.open(); addMetaData(document); addTitlePage(title, author, document); // addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Adds metadata info to the generated file. This info can be viewed in under File -> Properties * * @param title * file title * @param author * name of the author * @param subject * { metadata } * @param keywords * { metadata, comma-separated values } * @param creator * { metadata, file creator } * @param document * file to be modified */ private static void addMetaData(String title, String author, String subject, String keywords, String creator, Document document) { if (title == null || title.isEmpty()) { document.addTitle(PDF_TITLE); } if (subject == null || subject.isEmpty()) { document.addSubject(PDF_SUBJECT); } if (keywords == null || keywords.isEmpty()) { document.addKeywords(PDF_KEYWORDS); } if (author == null || author.isEmpty()) { document.addAuthor(PDF_AUTHOR); } if (creator == null || creator.isEmpty()) document.addCreator(PDF_AUTHOR); } private static void addMetaData(Document document) { addMetaData(null, null, null, null, null, document); } private static void addTitlePage(String title, String user, Document document) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header if (title == null) { title = PDF_TITLE; } preface.add(new Paragraph(title, catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date // System.getProperty("user.name") preface.add(new Paragraph("Reporte generado por: " + user + ", " + new Date(), smallBold)); addEmptyLine(preface, 3); // preface.add(new Paragraph("Nuevo Sistema de Gestion de Bedelias", // 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(); } public static void imprimirActaCurso(java.util.List<Student> estudiantes, Curso curso) { try { Document document = new Document(); String a = FILE_FOLDER + "acta_" + curso.getName() + ".pdf"; PdfWriter.getInstance(document, new FileOutputStream(a)); document.open(); StringBuilder title = new StringBuilder("Resultados "); title.append(curso.getName()); addTitlePage(title.toString(), "Bedelias Explorer", document); addMetaData(document); addStudentGrid(estudiantes, document); document.close(); } catch (Exception e) { e.printStackTrace(); } } public static void imprimirEscolaridad(Student student, java.util.List<Evaluacion> evaluaciones) { // FIXME implementar // TODO completar metodos // aca la joda es el estudiante con sus datos + imprimir uno a uno los // cursos de cada evaluacion // y su resultado correspondiente, falta ver de agregar los examenes // tambien return; } public static void imprimirActaExamen(java.util.List<Student> estudiantes, Examen examen) { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE_FOLDER + "acta_" + examen.getAsignatura().getName() + ".pdf")); document.open(); StringBuilder title = new StringBuilder("Resultados "); title.append(examen.getAsignatura().getName()); addTitlePage(title.toString(), "Bedelias Explorer", document); addMetaData(document); addStudentGrid(estudiantes, document); document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addHeaderData(Document document, Curso curso) { // Next section Paragraph content = new Paragraph(curso.getName(), catFont); } private static void addStudentGrid(java.util.List<Student> students, Document document) throws DocumentException { Paragraph results = new Paragraph(); // Add a table PdfPTable table = new PdfPTable(3); PdfPCell c1 = new PdfPCell(new Phrase("CI Estudiante")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Nombre Completo")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Resultado")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); for (Student s : students) { table.addCell(s.getCedula()); table.addCell(s.getName()); table.addCell(""); } results.add(table); // Now add all this to the document document.add(results); } private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } }