br.edu.unipampa.recipemanager.pdf.CreatePDF.java Source code

Java tutorial

Introduction

Here is the source code for br.edu.unipampa.recipemanager.pdf.CreatePDF.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 br.edu.unipampa.recipemanager.pdf;

import br.edu.unipampa.recipemanager.recipe.MenuRecipe;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 *
 * @author yuryalencar
 */
public class CreatePDF {

    public boolean newPdf(List<MenuRecipe> menuRecipe, String month, String responsibleName) {
        double valueMonth = 0;
        try {
            Document doc = new Document(PageSize.A4, 72, 72, 72, 72);
            OutputStream os = new FileOutputStream(namePdf(menuRecipe));
            PdfWriter.getInstance(doc, os);

            doc.open();
            Paragraph p;
            Font f = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);

            p = new Paragraph("Ms: " + month, f);
            doc.add(p);

            f = new Font(Font.FontFamily.COURIER, 14, Font.ITALIC);

            for (MenuRecipe menu : menuRecipe) {
                valueMonth += menu.priceMenu();
                p.setSpacingBefore(5);
                p.setSpacingAfter(5);
                p = new Paragraph(menu.toString(), f);
                doc.add(p);
            }

            p = new Paragraph("Preo total de todos os cardpios: " + valueMonth, f);
            doc.add(p);

            f = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            p = new Paragraph("_____________________________________\n" + responsibleName, f);
            p.setSpacingAfter(15);
            p.setAlignment(Element.ALIGN_RIGHT);
            doc.add(p);

            doc.close();
            os.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Mtodo para verificar se o nome do pdf j existe, e fazer a melhor
     * escolha
     *
     * @param recipes Tipo:List contendo a lista das receitas que foram
     * includas para a gerao do pdf.
     * @return Tipo:File retornando o arquivo pronto j para a gravao do
     * documento.
     */
    private File namePdf(List<MenuRecipe> recipes) {
        File file;
        int cont = 1;

        if (recipes.size() == 1) {

            file = new File(System.getProperty("user.home") + System.getProperty("file.separator")
                    + recipes.get(0).getNameMenu() + ".pdf");

            while (file.exists()) {
                file = new File(System.getProperty("user.home") + System.getProperty("file.separator")
                        + recipes.get(0).getNameMenu() + "(" + cont + ").pdf");
                cont++;
            }

        } else {

            file = new File(
                    System.getProperty("user.home") + System.getProperty("file.separator") + "Menu set.pdf");

            while (file.exists()) {
                file = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "Menu set("
                        + cont + ").pdf");
                cont++;
            }

        }

        return file;
    }
}