com.tomasz.drag.triballocommanderro.controller.MakerPDF.java Source code

Java tutorial

Introduction

Here is the source code for com.tomasz.drag.triballocommanderro.controller.MakerPDF.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 com.tomasz.drag.triballocommanderro.controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
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.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.tomasz.drag.triballocommanderro.data.EventGig;
import com.tomasz.drag.triballocommanderro.data.Person;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Tomasz Drg
 */
public class MakerPDF {

    public static void printToPDFBoss(ArrayList<Person> workers, EventGig event)
            throws IOException, DocumentException {
        String filename = event.getName() + " Boss" + ".pdf";
        String path = event.getData() + " " + event.getName() + "//";
        //Rectangle rect = new Rectangle(0, 595, 8, 0);
        Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
        //Document document = new Document(rect);
        //document.setMargins(0, 0, 0, 0);
        BaseFont bf;
        //BaseFont bf2;
        // bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        bf = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
        //bf2 = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        com.itextpdf.text.Font ft = new com.itextpdf.text.Font(bf, 9);
        //com.itextpdf.text.Font ft2 = new com.itextpdf.text.Font(bf2,9);

        PdfWriter.getInstance(document, new FileOutputStream(path + filename));

        document.open();
        Paragraph p = new Paragraph(event.getName() + " " + event.getData(), ft);
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);
        document.add(new Paragraph(" "));
        PdfPTable table = new PdfPTable(workers.size() + 1);
        table.setWidthPercentage(98);
        PdfPCell cell = new PdfPCell(new Phrase("    "));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            cell = new PdfPCell(new Paragraph(tempPerson.getName(), ft));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

        }
        if (event.isZaladunekScenaDach()) {
            cell = new PdfPCell(new Paragraph("Za. Sc./Dach", ft));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getZaladunekScenaDachWyplata() > 0) {
                    cell = new PdfPCell(
                            new Paragraph(String.format("%.2f", tempPerson.getZaladunekScenaDachWyplata()), ft));
                    cell.setVerticalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }
        if (event.isZaladunekTechniki()) {
            cell = new PdfPCell(new Phrase("Za. Tech.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getZaladunekTechnikiWyplata() > 0) {
                    cell = new PdfPCell(
                            new Paragraph(String.format("%.2f", tempPerson.getZaladunekTechnikiWyplata()), ft));
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isRozladunekScenaDach()) {
            cell = new PdfPCell(new Phrase("Roz Sc/Dach", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getRozladunekScenaDachWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getRozladunekScenaDachWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }
        if (event.isRozladunekTechniki()) {
            cell = new PdfPCell(new Phrase("Roz. Tech.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getRozladunekTechnikiWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getRozladunekTechnikiWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isMontazDachu()) {
            cell = new PdfPCell(new Phrase("Mon. Dach", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getMontazDachuWyplata() > 0) {
                    cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazDachuWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isMontazDzwieku()) {
            cell = new PdfPCell(new Phrase("Mon. Dw.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getMontazDzwiekuWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getMontazDzwiekuWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isMontazSceny()) {
            cell = new PdfPCell(new Phrase("Mon. Sc.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getMontazScenyWyplata() > 0) {
                    cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazScenyWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isMontazSwiatla()) {
            cell = new PdfPCell(new Phrase("Mon. w.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getMontazSwiatlaWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getMontazSwiatlaWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isMontazTechniki()) {
            cell = new PdfPCell(new Phrase("Mon. Tech.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getMontazTechnikiWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getMontazTechnikiWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDemontazDachu()) {
            cell = new PdfPCell(new Phrase("Dem. Dach", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDemontazDachuWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getDemontazDachuWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDemontazDzwieku()) {
            cell = new PdfPCell(new Phrase("Dem. Dw.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDemontazDzwiekuWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getDemontazDzwiekuWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDemontazSceny()) {
            cell = new PdfPCell(new Phrase("Dem. Sc.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDemontazScenyWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getDemontazScenyWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDemontazSwiatla()) {
            cell = new PdfPCell(new Phrase("Dem. w.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDemontazSwiatlaWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getDemontazSwiatlaWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase("   "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDemontazTechniki()) {
            cell = new PdfPCell(new Phrase("Dem. Tech.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDemontazTechnikiWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getDemontazTechnikiWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isDyzur()) {
            cell = new PdfPCell(new Phrase("Dyur", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getDyzurWyplata() > 0) {
                    cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getDyzurWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(""));
                    table.addCell(cell);
                }
            }
        }

        if (event.isTechnikSceny()) {
            cell = new PdfPCell(new Phrase("Technik Sc.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getTechnikScenyWyplata() > 0) {
                    cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getTechnikScenyWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isRealizacjaFOH()) {
            cell = new PdfPCell(new Phrase("Real. FOH", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getRealizacjaFOHWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getRealizacjaFOHWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isRealizacjaMON()) {
            cell = new PdfPCell(new Phrase("Real. MON", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getRealizacjaMONWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getRealizacjaMONWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (event.isRealizacjaSwiatla()) {
            cell = new PdfPCell(new Phrase("Real. w.", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getRealizacjaSwiatlaWyplata() > 0) {
                    cell = new PdfPCell(
                            new Phrase(String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        if (true) {
            cell = new PdfPCell(new Phrase("Premia", ft));
            table.addCell(cell);
            for (Person tempPerson : workers) {
                if (tempPerson.getPremia() > 0) {
                    cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getPremia()), ft));
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                    table.addCell(cell);
                }
            }
        }

        Paragraph ph = new Paragraph("Razem", ft);

        //cell = new PdfPCell(new Phrase(ph));
        cell.addElement(ph);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getSumaWyplata() >= 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getSumaWyplata()) + " z", ft));
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
        //        for (Object obj : table.getChunks()) {
        //            if (obj instanceof PdfPCell) {
        //                System.out.println("jest PdfCell");
        //                PdfPCell tempCell = (PdfPCell) obj;
        //                tempCell.setVerticalAlignment(Element.ALIGN_CENTER);
        //                //tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //            }
        //            
        //        }
        table.setHorizontalAlignment(Element.ALIGN_CENTER);
        document.add(table);
        document.close();

    }

    public static void printToPDF(ArrayList<Person> workers, EventGig event) throws IOException, DocumentException {
        String filename = event.getName() + ".pdf";
        String path = event.getData() + " " + event.getName() + "//";

        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path + filename));
        // step 3
        document.open();
        // step 4
        ColumnText column = new ColumnText(writer.getDirectContent());

        float[][] x = { { document.left(), document.left() + 230 }, { document.right() - 230, document.right() } };
        for (Person person : workers) {
            column.addElement(MakerPDF.createTable(person, event));
        }
        int count = 0;
        float height = 0;
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            column.setSimpleColumn(x[count][0], document.bottom(), x[count][1], document.top() - height - 10);
            // render as much content as possible
            status = column.go();
            // go to a new page if you've reached the last column
            if (++count > 1) {
                count = 0;
                document.newPage();
            }
        }
        document.newPage();
        document.close();
    }

    public static PdfPTable createTable(Person person, EventGig event) throws DocumentException, IOException {
        int a = 0;
        if (person.getZaladunekScenaDachWyplata() > 0) {
            a++;
        }
        if (person.getZaladunekTechnikiWyplata() > 0) {
            a++;
        }

        if (person.getMontazDachuWyplata() > 0) {
            a++;
        }
        if (person.getMontazDzwiekuWyplata() > 0) {
            a++;
        }

        if (person.getMontazScenyWyplata() > 0) {
            a++;
        }
        if (person.getMontazSwiatlaWyplata() > 0) {
            a++;
        }

        if (person.getMontazTechnikiWyplata() > 0) {
            a++;
        }

        if (person.getDemontazDachuWyplata() > 0) {
            a++;
        }
        if (person.getDemontazDzwiekuWyplata() > 0) {
            a++;
        }

        if (person.getDemontazScenyWyplata() > 0) {
            a++;
        }
        if (person.getDemontazSwiatlaWyplata() > 0) {
            a++;
        }

        if (person.getDemontazTechnikiWyplata() > 0) {
            a++;
        }

        if (person.getRozladunekScenaDachWyplata() > 0) {
            a++;
        }
        if (person.getRozladunekTechnikiWyplata() > 0) {
            a++;
        }
        if (person.getDyzurWyplata() > 0) {
            a++;
        }
        if (person.getTechnikScenyWyplata() > 0) {
            a++;
        }
        if (person.getRealizacjaFOHWyplata() > 0) {
            a++;
        }
        if (person.getRealizacjaMONWyplata() > 0) {
            a++;
        }
        if (person.getRealizacjaSwiatlaWyplata() > 0) {
            a++;
        }

        if (person.getPremia() > 0) {
            a++;
        }

        System.out.println(a);
        BaseFont bf;
        bf = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
        // bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        com.itextpdf.text.Font ft = new com.itextpdf.text.Font(bf);
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(new float[] { 90, 190 });
        table.setLockedWidth(true);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(person.getName() + " " + person.getSurName(), ft));
        cell.setColspan(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(event.getName() + " " + event.getData(), ft));
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(""));

        cell.setRowspan(a);

        table.addCell(cell);

        if (person.getZaladunekScenaDachWyplata() > 0) {
            table.addCell(new Phrase("Zaadunek Scena/Dach", ft));

        }
        if (person.getZaladunekTechnikiWyplata() > 0) {
            table.addCell(new Phrase("Zaadunek Techniki", ft));
        }

        if (person.getMontazDachuWyplata() > 0) {
            table.addCell(new Phrase("Monta Dachu", ft));

        }
        if (person.getMontazDzwiekuWyplata() > 0) {
            table.addCell(new Phrase("Monta Dwieku", ft));
        }

        if (person.getMontazScenyWyplata() > 0) {
            table.addCell(new Phrase("Monta Sceny", ft));

        }
        if (person.getMontazSwiatlaWyplata() > 0) {
            table.addCell(new Phrase("Monta wiata", ft));
        }

        if (person.getMontazTechnikiWyplata() > 0) {
            table.addCell(new Phrase("Monta Techniki", ft));
        }

        if (person.getDemontazDachuWyplata() > 0) {
            table.addCell(new Phrase("Demonta Dachu", ft));

        }
        if (person.getDemontazDzwiekuWyplata() > 0) {
            table.addCell(new Phrase("Demonta Dwieku", ft));
        }

        if (person.getDemontazScenyWyplata() > 0) {
            table.addCell(new Phrase("Demonta Sceny", ft));

        }
        if (person.getDemontazSwiatlaWyplata() > 0) {
            table.addCell(new Phrase("Demonta wiata", ft));
        }

        if (person.getDemontazTechnikiWyplata() > 0) {
            table.addCell(new Phrase("Demonta Techniki", ft));
        }

        if (person.getRozladunekScenaDachWyplata() > 0) {
            table.addCell(new Phrase("Rozadunek Scena/Dach", ft));

        }
        if (person.getRozladunekTechnikiWyplata() > 0) {
            table.addCell(new Phrase("Rozadunek Techniki", ft));
        }

        if (person.getDyzurWyplata() > 0) {
            table.addCell(new Phrase("Dyur", ft));
        }

        if (person.getTechnikScenyWyplata() > 0) {
            table.addCell(new Phrase("Technik Sceny", ft));
        }

        if (person.getRealizacjaFOHWyplata() > 0) {
            table.addCell(new Phrase("Realizacja FOH", ft));
        }

        if (person.getRealizacjaMONWyplata() > 0) {
            table.addCell(new Phrase("Realizacja MON", ft));
        }

        if (person.getRealizacjaSwiatlaWyplata() > 0) {
            table.addCell(new Phrase("Realizacja wiata", ft));
        }

        if (person.getPremia() > 0) {
            table.addCell(new Phrase("Premia", ft));
        }

        cell = new PdfPCell(new Phrase("Suma: "));
        cell.setColspan(1);
        table.addCell(cell);
        //cell = new PdfPCell(new Phrase(Double.toString(person.getSumaWyplata())));//String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata())
        cell = new PdfPCell(new Phrase(String.format("%.2f", person.getSumaWyplata()) + " z", ft));//String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata())
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(" "));
        cell.setColspan(1);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //cell = new PdfPCell(new Phrase(Double.toString(person.getSumaWyplata())));//String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata())
        cell = new PdfPCell(new Phrase("  "));//String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata())
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        table.setBreakPoints(2 + a);

        return table;

    }

    public static void dirMaker(EventGig event) {
        File dir = new File(event.getData() + " " + event.getName());
        dir.mkdir();
        //"src\\
    }
}