Biblioteca Java - Rev 9

Subversion Repositories:
Rev:
package com.linkscreens.inventory.pdfgenerator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.linkscreens.inventory.entity.InventoryItem;

public class PDFGenerator {

    private static Font TIME_ROMAN =
            new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
    private static Font TIME_ROMAN_SMALL =
            new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
    private static Font TIMES_ROMAN_SMALLEST =
            new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.NORMAL);
    private static Font TIMES_ROMAN_SMALLEST_BOLD =
            new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.BOLD);


    public static Document createPDF(String file, List<InventoryItem> items, boolean isFull) {

        Document document = null;

        try {
            document = new Document(PageSize.LETTER.rotate());
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
            addMetaData(document);

            addTitlePage(document, items.get(0).getAnLunaAdaugare());

            createTable(document, items, isFull);

            addRowAfter(document);

            document.close();

        } catch (FileNotFoundException  e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;

    }

    private static void addMetaData(Document document) {
        document.addTitle("Generare raport PDF");
        document.addSubject("Generare raport PDF");
        document.addAuthor("LinkScreens");
        document.addCreator("LinkScreens");
    }

    private static void addRowAfter(Document document) throws DocumentException {
        Paragraph rowAfter = new Paragraph();
        creteEmptyLine(rowAfter, 1);
        rowAfter.add(new Paragraph("Director General", TIME_ROMAN));
        document.add(rowAfter);
    }

    private static void addTitlePage(Document document, String yearMonthAdded)
            throws DocumentException {

        Paragraph preface = new Paragraph();
        creteEmptyLine(preface, 1);
        preface.add(new Paragraph("LINKSCREENS SRL", TIME_ROMAN));

        creteEmptyLine(preface, 1);

        preface.add(new Paragraph("Adresa: str.VIDRARU 9-11, Ap.104 Localitatea CLUJ NAPOCA Judetul Cluj", TIME_ROMAN_SMALL));
        preface.add(new Paragraph("Cod Fiscal: RO31223800", TIME_ROMAN_SMALL));
        preface.add(new Paragraph("Reg. comertului: J12/455/13.02.2013", TIME_ROMAN_SMALL));
        creteEmptyLine(preface, 1);

        String month = getMonth(yearMonthAdded);
        Paragraph title = new Paragraph("Registru inventar " + month + " " + yearMonthAdded.split("/")[0], TIME_ROMAN);
        title.setAlignment(Element.ALIGN_CENTER);
        preface.add(title);
        document.add(preface);

        document.addHeader("Registru inventar " + month + " " + yearMonthAdded.split("/")[0] + "(continuare)",
            "Registru inventar " + month + " " + yearMonthAdded.split("/")[0] + "(continuare)");
    }

    private static void creteEmptyLine(Paragraph paragraph,
                                       int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private static String getMonth(String yearMonthAdded) {
        String month = "";
        if (yearMonthAdded.split("/")[1].equalsIgnoreCase("01")) {
            month = "Ianuarie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("02")) {
            month = "Februarie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("03")) {
            month = "Martie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("04")) {
            month = "Aprilie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("05")) {
            month = "Mai";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("06")) {
            month = "Iunie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("07")) {
            month = "Iulie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("08")) {
            month = "August";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("09")) {
            month = "Septembrie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("10")) {
            month = "Octombrie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("11")) {
            month = "Noiembrie";
        } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("12")) {
            month = "Decembrie";
        }
        return month;
    }

    private static void createTable(Document document, List<InventoryItem> items, boolean isFull)
            throws DocumentException {
        Paragraph paragraph = new Paragraph();
        creteEmptyLine(paragraph, 2);
        document.add(paragraph);
        PdfPTable table = null;
        if (isFull) {
            table = new PdfPTable(17);
        } else {
            table = new PdfPTable(10);
        }

        PdfPCell c1 = new PdfPCell(new Phrase("Nr. Crt.", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Simbol clasa mijloc fix", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Mijloc fix", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Nr. Inv.", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Cont", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Cant.", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("UM", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Pret", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Valoare", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        if (isFull) {
            c1 = new PdfPCell(new Phrase("Amortizare precedenta", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Amortizare curenta", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Amortizare totala", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Valoare ramasa", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Durata norm. (ani)", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Durata ramasa (luni)", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("An luna prima rata", TIME_ROMAN_SMALL));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
        }

        c1 = new PdfPCell(new Phrase("An luna adaugare", TIME_ROMAN_SMALL));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        table.setHeaderRows(1);

        table.setWidthPercentage(100);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

        for (InventoryItem item : items) {
            if (item.getId() == null || item.getId() == 0L) {
                PdfPCell totalName = new PdfPCell(new Phrase(item.getSimbolClasaMijlocFix(), TIMES_ROMAN_SMALLEST_BOLD));
                totalName.setColspan(8);
                totalName.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(totalName);

                PdfPCell totalValue = new PdfPCell(new Phrase(item.getValoare().toString(), TIMES_ROMAN_SMALLEST_BOLD));
                totalValue.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(totalValue);

                if (isFull) {
                    PdfPCell totalPast = new PdfPCell(new Phrase(item.getAmortizarePrecedenta().toString(), TIMES_ROMAN_SMALLEST_BOLD));
                    totalPast.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(totalPast);

                    PdfPCell totalCurrent = new PdfPCell(new Phrase(item.getAmortizareCurenta().toString(), TIMES_ROMAN_SMALLEST_BOLD));
                    totalCurrent.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(totalCurrent);

                    PdfPCell totalAmortization = new PdfPCell(new Phrase(item.getAmortizareTotala().toString(), TIMES_ROMAN_SMALLEST_BOLD));
                    totalAmortization.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(totalAmortization);

                    PdfPCell remainingValue = new PdfPCell(new Phrase(item.getValoareRamasa().toString(), TIMES_ROMAN_SMALLEST_BOLD));
                    remainingValue.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(remainingValue);

                    PdfPCell remainingCells = new PdfPCell(new Phrase(""));
                    remainingCells.setColspan(3);
                    table.addCell(remainingCells);
                }

                PdfPCell remainingCell = new PdfPCell(new Phrase(""));
                remainingCell.setColspan(1);
                table.addCell(remainingCell);

            } else {

                PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(item.getNrCrt()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(String.valueOf(item.getSimbolClasaMijlocFix()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(String.valueOf(item.getMijlocFix()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(String.valueOf(item.getNrInventar()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(item.getCont(), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(String.valueOf(item.getCantitate()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(item.getUM(), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(item.getPret().toString(), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(item.getValoare().toString(), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);

                if (isFull) {
                    cell = new PdfPCell(new Phrase(item.getAmortizarePrecedenta().toString(), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(item.getAmortizareCurenta().toString(), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(item.getAmortizareTotala().toString(), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(item.getValoareRamasa().toString(), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(String.valueOf(item.getDurataNormala()), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(String.valueOf(item.getDurataRamasa()), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);

                    cell = new PdfPCell(new Phrase(String.valueOf(item.getAnLunaPrimaRata()), TIMES_ROMAN_SMALLEST));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cell);
                }

                cell = new PdfPCell(new Phrase(String.valueOf(item.getAnLunaAdaugare()), TIMES_ROMAN_SMALLEST));
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cell);
            }
        }
        document.add(table);
    }

}