model.PrintPDF.java Source code

Java tutorial

Introduction

Here is the source code for model.PrintPDF.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 model;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class PrintPDF {

    SimpleDateFormat dateFormat;

    public PrintPDF() {
        dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    }

    public void inThongKeCanBo(ArrayList<CanBo> listCanBo, String pathName, String nam, int gt, String tongNhanVien,
            String soNVVH) {
        PdfWriter pdfWriter = null;
        String gioiTinh = "";
        if (gt == 1) {
            gioiTinh = "n";
        } else {
            gioiTinh = "nam";
        }

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k cn b " + gioiTinh + " nm " + nam + "\n\n").setFont(hfont)
                    .setBold().setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 2, 4, 3, 4 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ngy sinh").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("S in thoi").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));

            //  ni dung ca bng thng k
            for (int i = 0; i < listCanBo.size(); i++) {
                table.addCell(new Paragraph((i + 1) + "").setFont(hfont).setTextAlignment(TextAlignment.CENTER));
                table.addCell(new Paragraph(listCanBo.get(i).getMaCB()).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER));
                table.addCell(new Paragraph(listCanBo.get(i).getHoTen()).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER));
                table.addCell(new Paragraph(dateFormat.format(listCanBo.get(i).getNgaySinh())).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER));
                table.addCell(new Paragraph(listCanBo.get(i).getSDT()).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER));
            }
            document.add(table);
            document.add(new Paragraph("\n\n Tng s nhn vin            :  " + tongNhanVien).setFont(hfont)
                    .setTextAlignment(TextAlignment.LEFT));
            document.add(new Paragraph(" Tng s nhn vin sp v? hu :  " + tongNhanVien).setFont(hfont)
                    .setTextAlignment(TextAlignment.LEFT));

            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void thongKeDoanPhi(ArrayList<String> listDataTable, String pathName, String nam, String tongDoanVien,
            String tienDoanPhi) {
        PdfWriter pdfWriter = null;
        String gioiTinh = "";

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k on vin  " + gioiTinh + " nm " + nam + "\n\n")
                    .setFont(hfont).setBold().setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 2, 4, 3, 4 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ngy sinh").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("S ti khon ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));

            //  ni dung ca bng thng k
            for (int i = 0; i < listDataTable.size(); i++) {
                table.addCell(new Paragraph(listDataTable.get(i)).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            }

            document.add(table);
            document.add(new Paragraph("\n\n Tng s on vin           :  " + tongDoanVien).setFont(hfont)
                    .setTextAlignment(TextAlignment.LEFT));
            document.add(new Paragraph(" ?on ph (VN? / ?on Vin) :  " + tienDoanPhi + "VN?")
                    .setFont(hfont).setTextAlignment(TextAlignment.LEFT));
            double tongTienDoanPhi = Integer.parseInt(tongDoanVien) * Integer.parseInt(tienDoanPhi);
            document.add(new Paragraph(" Tng s ti?n on ph            :  " + tongTienDoanPhi + " VND")
                    .setFont(hfont).setTextAlignment(TextAlignment.LEFT));
            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void thongKeCongDoanPhi(ArrayList<String> listDataTable, String pathName, String nam,
            String tongCongDoanVien, String tienCongDoanPhi) {
        PdfWriter pdfWriter = null;

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k cng on vin  nm " + nam + "\n\n").setFont(hfont)
                    .setBold().setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 2, 4, 3, 3, 3 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ngy sinh").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Chc v ").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Cng on ph ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            //  ni dung ca bng thng k
            for (int i = 0; i < listDataTable.size(); i++) {
                table.addCell(new Paragraph(listDataTable.get(i)).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            }

            document.add(table);
            document.add(new Paragraph("\n\n Tng s nhn vin           :  " + tongCongDoanVien)
                    .setFont(hfont).setTextAlignment(TextAlignment.LEFT));
            document.add(new Paragraph(" Tng cng on ph  :  " + tienCongDoanPhi).setFont(hfont)
                    .setTextAlignment(TextAlignment.LEFT));

            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void thongKeDangPhi(ArrayList<String> listDataTable, String pathName, String nam, String tongDangVien) {
        PdfWriter pdfWriter = null;

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k ng vin  " + " nm " + nam + "\n\n").setFont(hfont)
                    .setBold().setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 2, 4, 3, 4, 4 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ngy sinh").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Chc v ").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("?ng ph ").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));

            //  ni dung ca bng thng k
            for (int i = 0; i < listDataTable.size(); i++) {
                table.addCell(new Paragraph(listDataTable.get(i)).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            }

            document.add(table);
            document.add(new Paragraph("\n\n Tng s ng vin           :  " + tongDangVien).setFont(hfont)
                    .setTextAlignment(TextAlignment.LEFT));
            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void thongKeTienThi(ArrayList<String> listDataTable, String pathName) {
        PdfWriter pdfWriter = null;

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k ti?n thi ging vin  " + "\n\n").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 2, 4, 3, 4, 4 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Mn h?c").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("S SV ").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ph thi").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));

            //  ni dung ca bng thng k
            for (int i = 0; i < listDataTable.size(); i++) {
                table.addCell(new Paragraph(listDataTable.get(i)).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            }

            document.add(table);
            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void thongKeThieuNhi(ArrayList<String> listDataTable, String pathName) {
        PdfWriter pdfWriter = null;

        try {
            pdfWriter = new PdfWriter(pathName);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument, PageSize.A4.rotate());
            PdfFont hfont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\tahoma.ttf", PdfEncodings.IDENTITY_H,
                    true);
            document.setMargins(50, 50, 50, 50);
            document.add(new Paragraph("TRNG ?I HC B?CH KHOA H NI ").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(16));
            document.add(new Paragraph("Thng k cc chu thiu nhi   " + "\n\n").setFont(hfont).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            float[] columnWidths = { 1, 3, 3, 3, 4 };
            Table table = new Table(columnWidths);
            table.setWidthPercent(100);
            table.addHeaderCell(new Paragraph("STT").setFont(hfont).setTextAlignment(TextAlignment.CENTER).setBold()
                    .setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("Ngy sinh ").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("M cn b").setFont(hfont).setTextAlignment(TextAlignment.CENTER)
                    .setBold().setFontSize(14));
            table.addHeaderCell(new Paragraph("H? v tn cn b ").setFont(hfont)
                    .setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(14));

            //  ni dung ca bng thng k
            for (int i = 0; i < listDataTable.size(); i++) {
                table.addCell(new Paragraph(listDataTable.get(i)).setFont(hfont)
                        .setTextAlignment(TextAlignment.CENTER).setFontSize(14));
            }

            document.add(table);
            document.add(new Paragraph(
                    "\n\n\n H Ni, ngy    thng        nm     \n  Ng?i lp phiu \n  ( Ghi r h? tn) ")
                            .setFont(hfont).setTextAlignment(TextAlignment.RIGHT).setItalic().setMarginRight(40));
            document.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pdfWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(PrintPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

}