be.thomasmore.service.CreatePDFServiceImp.java Source code

Java tutorial

Introduction

Here is the source code for be.thomasmore.service.CreatePDFServiceImp.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package be.thomasmore.service;

import be.thomasmore.model.Score;
import com.itextpdf.text.Document;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author Vetsoo
 */
@Stateless
public class CreatePDFServiceImp implements CreatePDFService {

    @Override
    public void createPDF(List<Score> scores) {
        try {
            OutputStream file = new FileOutputStream(new File("D:\\Desktop\\Scores.pdf"));

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();
            document.add(new Paragraph("Scores"));
            document.add(new Paragraph(new Date().toString()));

            document.addAuthor("Projectgroep 4");
            document.addCreator("Projectgroep 4");
            document.addTitle("ScoreTracker");

            //Create Paragraph
            Paragraph paragraph = new Paragraph("", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));

            //New line
            paragraph.add(new Paragraph(" "));
            paragraph.add("Scores");
            paragraph.add(new Paragraph(" "));
            document.add(paragraph);

            //Create a table in PDF
            PdfPTable pdftabel = new PdfPTable(4);
            PdfPCell cell1 = new PdfPCell(new Phrase("Student"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdftabel.addCell(cell1);

            cell1 = new PdfPCell(new Phrase("Vak"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdftabel.addCell(cell1);

            cell1 = new PdfPCell(new Phrase("Test"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdftabel.addCell(cell1);

            cell1 = new PdfPCell(new Phrase("Score"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdftabel.addCell(cell1);

            pdftabel.setHeaderRows(1);

            for (Score score : scores) {
                pdftabel.addCell(score.getStudent().getNaam());
                pdftabel.addCell(score.getTest().getVak().getNaam());
                pdftabel.addCell(score.getTest().getNaam());
                int resultaat = score.getScore();
                pdftabel.addCell(resultaat + " / " + score.getTest().getTotaal());
            }

            document.add(pdftabel);
            document.addCreationDate();
            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Override
    public void createPDFVoorStudent(ArrayList<ArrayList<Score>> puntenlijst, List<Double> gemiddeldelijst,
            Double totaalGemiddelde) {
        try {
            OutputStream file = new FileOutputStream(new File("D:\\Desktop\\Scores.pdf"));

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();
            document.add(new Paragraph("Scores"));
            document.add(new Paragraph(puntenlijst.get(0).get(0).getStudent().getNaam()));
            document.add(new Paragraph(new Date().toString()));

            document.addAuthor("Projectgroep 4");
            document.addCreator("Projectgroep 4");
            document.addTitle("ScoreTracker");

            //Create Paragraph
            Paragraph paragraph = new Paragraph("", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));

            //New line
            paragraph.add(new Paragraph(" "));
            paragraph.add("Scores");
            paragraph.add(new Paragraph(" "));
            document.add(paragraph);

            for (int i = 0; i < puntenlijst.size(); i++) {
                //Create a table in PDF
                PdfPTable pdftabel = new PdfPTable(2);
                //vak invullen
                paragraph = new Paragraph("", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));
                paragraph.add(new Paragraph(puntenlijst.get(i).get(0).getTest().getVak().getNaam()));
                document.add(paragraph);

                PdfPCell cell1 = new PdfPCell(new Phrase("Test"));
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdftabel.addCell(cell1);

                cell1 = new PdfPCell(new Phrase("Score"));
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdftabel.addCell(cell1);

                pdftabel.setHeaderRows(1);

                for (Score score : puntenlijst.get(i)) {
                    pdftabel.addCell(score.getTest().getNaam());
                    int resultaat = score.getScore();
                    pdftabel.addCell(resultaat + " / " + score.getTest().getTotaal());
                }

                document.add(pdftabel);

                //gemmidelde per vak invoeren
                paragraph = new Paragraph("", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));
                paragraph.add(new Paragraph("Gemiddelde: " + gemiddeldelijst.get(i).toString()));
                document.add(paragraph);
            }
            paragraph = new Paragraph("", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));
            paragraph.add(new Paragraph("Algemeen gemiddelde: " + totaalGemiddelde));
            document.add(paragraph);
            document.addCreationDate();
            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}