com.kahlon.guard.controller.DocumentManager.java Source code

Java tutorial

Introduction

Here is the source code for com.kahlon.guard.controller.DocumentManager.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.kahlon.guard.controller;

import com.kahlon.guard.entity.Person;
import com.kahlon.guard.entity.PersonImage;
import com.kahlon.guard.service.ImageService;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BarcodePDF417;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.draw.LineSeparator;
import com.prime.app.util.FacesMessageUtil;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

/**
 *
 * @author prime
 */
@Named("document")
public class DocumentManager implements Serializable {

    private static final long serialVersionUID = 9196263323752494128L;

    @Inject
    GuardSessionContext context;

    @Inject
    FacesContext facesContext;

    @Inject
    ImageService imageService;

    @Inject
    Logger logger;

    /**
     *
     * @param document
     */
    public void preProcessPDF(Object document) {
        try {

            BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);

            Document pdf = (Document) document;
            pdf.setPageSize(PageSize.A4);
            pdf.setMargins(5f, 5f, 10f, 5f);

            // headers and footers must be added before the document is opened
            HeaderFooter footer = new HeaderFooter(new Phrase("page: ", new Font(bf_courier)), true);
            footer.setBorder(Rectangle.NO_BORDER);
            footer.setAlignment(Element.ALIGN_CENTER);
            pdf.setFooter(footer);

            //            HeaderFooter header = new HeaderFooter(
            //                        new Phrase("This is a header without a page number", new Font(bf_courier)), false);
            //            header.setAlignment(Element.ALIGN_CENTER);
            //            pdf.setHeader(header);

            pdf.open();

            String logoPath = "/resources/image/logo.png";
            ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
            String logo = servletContext.getRealPath(logoPath);

            pdf.add(Image.getInstance(logo));

            Person person = context.getSelectedPerson().getDisplayPerson();

            String name = FacesMessageUtil.getMessage("person.name") + " : " + person.getName();
            String age = FacesMessageUtil.getMessage("person.age") + " : " + Integer.toString(person.getAge());
            String gender = FacesMessageUtil.getMessage("person.gender") + " : "
                    + person.getGender().getDescription();
            String race = FacesMessageUtil.getMessage("person.ethnicity") + " : "
                    + person.getEthnicity().getDescription();

            Person rootPerson = person.getRootPerson();
            PersonImage imgp = imageService.getLastestPersonImage(rootPerson.getId());
            Image imgb = Image.getInstance(imgp.getContent());
            imgb.scaleToFit(100, 120);

            PdfPTable headerTable = new PdfPTable(2);

            PdfPTable personTable = new PdfPTable(1);
            personTable.setWidthPercentage(100);
            PdfPCell cell;

            cell = new PdfPCell(new Phrase(name));
            cell.setBorder(Rectangle.NO_BORDER);
            personTable.addCell(cell);

            cell = new PdfPCell(new Phrase(age));
            cell.setBorder(Rectangle.NO_BORDER);
            personTable.addCell(cell);

            cell = new PdfPCell(new Phrase(gender));
            cell.setBorder(Rectangle.NO_BORDER);
            personTable.addCell(cell);

            cell = new PdfPCell(new Phrase(race));
            cell.setBorder(Rectangle.NO_BORDER);
            personTable.addCell(cell);

            BarcodePDF417 pdf417 = new BarcodePDF417();
            pdf417.setText(Integer.toString(person.getId()));
            Image img = pdf417.getImage();
            img.scalePercent(150, 60 * pdf417.getYHeight());
            cell = new PdfPCell(img);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingTop(15);
            personTable.addCell(cell);

            cell.addElement(personTable);
            headerTable.addCell(cell);

            cell = new PdfPCell(imgb);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setBorder(Rectangle.NO_BORDER);
            headerTable.addCell(cell);

            headerTable.setSpacingAfter(15);
            pdf.add(headerTable);

            LineSeparator lineSeparator = new LineSeparator();
            lineSeparator.setPercentage(82);
            pdf.add(lineSeparator);
            Paragraph space = new Paragraph();
            space.add("");
            space.setSpacingAfter(15);
            pdf.add(space);

        } catch (IOException e) {
            logger.log(Level.INFO, e.getMessage());
        } catch (BadElementException e) {
            logger.log(Level.INFO, e.getMessage());
        } catch (DocumentException e) {
            logger.log(Level.INFO, e.getMessage());
        } catch (Exception e) {
            logger.log(Level.INFO, e.getMessage());
        }

    }

    public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.BLUE.index);

        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setColor(HSSFColor.WHITE.index);
        cellStyle.setFont(font);
        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);
            cell.setCellStyle(cellStyle);
        }
    }

}