Example usage for com.itextpdf.text.pdf BaseFont EMBEDDED

List of usage examples for com.itextpdf.text.pdf BaseFont EMBEDDED

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf BaseFont EMBEDDED.

Prototype

boolean EMBEDDED

To view the source code for com.itextpdf.text.pdf BaseFont EMBEDDED.

Click Source Link

Document

if the font has to be embedded

Usage

From source file:com.coderbd.pos.pdf.CodePDF.java

public boolean genCodeVer2Pdf() throws UnsupportedEncodingException {

    if (isFixedRated == true) {
        barcodeQuantity = 12;//  www . j  a v a 2  s .c  o m
    } else {
        barcodeQuantity = 15;
    }

    try {
        String filename = "";

        if (isFixedRated) {
            filename = directory + "\\" + product.getProductBarcode() + "_FIXED_TK" + ".pdf";
        } else {
            filename = directory + "\\" + product.getProductBarcode() + ".pdf";
        }

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Barcode128 code25 = new Barcode128();
        code25.setGenerateChecksum(true);
        code25.setCode(product.getProductBarcode());
        code25.setSize(10f);
        code25.setX(1.50f);

        PdfPTable pdfPTable = new PdfPTable(7);
        pdfPTable.setWidthPercentage(98);
        float[] widths = { 0.22f, 0.04f, 0.22f, 0.04f, 0.22f, 0.04f, 0.22f };
        pdfPTable.setWidths(widths);
        String codeName = getCodeName(product.getShop().getShopName(), product.getShop().getShopId(),
                (int) product.getProductBuyRate());

        System.out.println("BarCodeName:" + codeName);

        Image image = code25.createImageWithBarcode(cb, null, null);

        Font titleArialFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
        Font priceFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);

        Paragraph paragraph = new Paragraph(codeName, titleArialFont);
        paragraph.setSpacingBefore(10.0f);
        PdfPCell title = new PdfPCell(paragraph);
        title.setBorder(Rectangle.NO_BORDER);

        Paragraph fixedTKParagraph = new Paragraph("FIXED TK: " + product.getProductSellRate(), priceFont);
        PdfPCell fixedRateCell = new PdfPCell(fixedTKParagraph);
        fixedRateCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell barcodeCell = new PdfPCell(image, true);
        barcodeCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell blank = new PdfPCell();
        blank.setBorder(Rectangle.NO_BORDER);

        Paragraph blankParagraph = new Paragraph("-                           -");
        PdfPCell pdfPCellBlank = new PdfPCell(blankParagraph);
        pdfPCellBlank.setBorder(Rectangle.NO_BORDER);

        for (int i = 0; i < barcodeQuantity; i++) {
            /**
             * This is for barcode pdf title
             */
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);

            /**
             * This is for barcode image
             */
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            /**
             *
             */
            if (isFixedRated == true) {
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
            }
            /**
             * Blank space after barcode image
             */
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
        }
        document.add(pdfPTable);
        document.add(new Paragraph(
                "Shop Name: " + product.getShop().getShopName() + ", Name:" + product.getProductName()
                        + "\n,Qty: " + product.getProductStock() + ", " + new Date().toString(),
                titleArialFont));
        document.close();
        return true;
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
        return false;
    }
}

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public boolean genPdf() throws UnsupportedEncodingException {
    List<SupplierOrderProductReport> soprs = sor.getSupplierOrderProductReports();
    IDBuilder idBuilder = new IDBuilder();
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);
    try {/*ww  w.j a v a  2s.c  o  m*/
        String timestamp = idBuilder.getUniqueTimeStampID();
        String filename = sor.getSupplier().getSupplierName() + "_O" + sor.getSupplierOrderId() + "_"
                + timestamp + ".pdf";
        String filePath = directory + "\\" + filename;

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
        document.open();

        PdfPTable pdfPTable = new PdfPTable(5);

        pdfPTable.setWidthPercentage(90);
        float[] widths = { 0.40f, 0.13f, 0.15f, 0.15f, 0.17f };
        pdfPTable.addCell(new Paragraph("Product Name", headerFont));
        pdfPTable.addCell(new Paragraph("Buy Rate", headerFont));
        pdfPTable.addCell(new Paragraph("Primary Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Amount", headerFont));

        pdfPTable.setWidths(widths);
        for (SupplierOrderProductReport sopr : soprs) {

            SupplierOrderProduct sop = sopr.getSupplierOrderProduct();
            String name = sop.getSupplierProductName();
            Double rate = sop.getSupplierRate();
            Integer pQty = sop.getSupplierProductQuantity();
            Integer unQty = sopr.getUnSoldProductQuantity();
            Double unAmount = sopr.getUnSoldProductAmount();

            Paragraph nameParag = new Paragraph(name, innerFont);
            Paragraph buyRateParag = new Paragraph(rate.toString(), innerFont);
            Paragraph primaryQuantityParag = new Paragraph(pQty.toString(), innerFont);
            Paragraph unsoldQuantityParag = new Paragraph(unQty.toString(), innerFont);
            Paragraph unsoldAmountParag = new Paragraph(unAmount.toString(), innerFont);

            PdfPCell nameCell = new PdfPCell(nameParag);
            PdfPCell buyRateCell = new PdfPCell(buyRateParag);
            PdfPCell primaryQuantityCell = new PdfPCell(primaryQuantityParag);
            PdfPCell unsoldQuantityCell = new PdfPCell(unsoldQuantityParag);
            PdfPCell unsoldAmountCell = new PdfPCell(unsoldAmountParag);

            pdfPTable.addCell(nameCell);
            pdfPTable.addCell(buyRateCell);
            pdfPTable.addCell(primaryQuantityCell);
            pdfPTable.addCell(unsoldQuantityCell);
            pdfPTable.addCell(unsoldAmountCell);
        }
        document.add(getHeader());

        document.add(pdfPTable);
        document.add(getFooter());
        document.add(new Paragraph("            Report Generated: " + new Date().toString(), innerFont));
        document.close();
        return true;
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
        return false;
    }
}

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public PdfPTable getFooter() throws DocumentException {
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);

    PdfPTable pdfPTableFooter = new PdfPTable(2);
    pdfPTableFooter.setWidthPercentage(90);
    float[] widthsFooter = { 0.80f, 0.20f };
    pdfPTableFooter.setWidths(widthsFooter);

    Paragraph totalSoldPara = new Paragraph("Total Sold: " + sor.getOrderTotalSoldAmount().toString(),
            headerFont);//from w ww . j  a v a2s  .  co  m
    PdfPCell totalSoldCell = new PdfPCell(totalSoldPara);
    totalSoldCell.setBorder(Rectangle.NO_BORDER);

    Paragraph totalUnSoldPara = new Paragraph("Total UnSold: " + sor.getOrderTotalUnsoldAmount().toString(),
            headerFont);
    PdfPCell totalUnSoldCell = new PdfPCell(totalUnSoldPara);
    totalUnSoldCell.setBorder(Rectangle.NO_BORDER);

    Paragraph blankPara = new Paragraph("");
    PdfPCell blankCell = new PdfPCell(blankPara);
    blankCell.setBorder(Rectangle.NO_BORDER);

    Double bill = sor.getTotalBill();
    Double paid = sor.getTotalPaid();
    Double due = bill - paid;

    Paragraph billPara = new Paragraph("BILL: " + bill.toString(), headerFont);
    PdfPCell billCell = new PdfPCell(billPara);
    billCell.setBorder(Rectangle.NO_BORDER);

    Paragraph paidPara = new Paragraph("PAID: " + paid.toString(), headerFont);
    PdfPCell paidCell = new PdfPCell(paidPara);
    paidCell.setBorder(Rectangle.NO_BORDER);

    Paragraph duePara = new Paragraph("DUE: " + due.toString(), headerFont);
    PdfPCell dueCell = new PdfPCell(duePara);
    dueCell.setBorder(Rectangle.NO_BORDER);

    pdfPTableFooter.addCell(totalSoldCell);
    pdfPTableFooter.addCell(billCell);
    pdfPTableFooter.addCell(totalUnSoldCell);
    pdfPTableFooter.addCell(paidCell);
    pdfPTableFooter.addCell(blankCell);
    pdfPTableFooter.addCell(dueCell);

    return pdfPTableFooter;
}

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public PdfPTable getHeader() throws DocumentException {
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.BOLD);

    PdfPTable tableHeader = new PdfPTable(1);
    tableHeader.setWidthPercentage(90);//  w ww  .  ja v  a 2s .c om

    Paragraph supplierNamePara = new Paragraph(sor.getSupplier().getSupplierName(), headerFont);
    Paragraph addressPara = new Paragraph(sor.getSupplier().getSupplierAddress(), innerFont);
    Paragraph mobilePara = new Paragraph("Mobile: " + sor.getSupplier().getSupplierMobile(), innerFont);
    Paragraph orderPara = new Paragraph("Order: " + sor.getSupplierOrderId() + ", Time: " + sor.getOrderTime(),
            innerFont);

    PdfPCell supplierNameCell = new PdfPCell();
    supplierNameCell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
    supplierNameCell.setBorder(Rectangle.NO_BORDER);
    supplierNameCell.addElement(supplierNamePara);

    PdfPCell addressCell = new PdfPCell();
    addressCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    addressCell.setBorder(Rectangle.NO_BORDER);
    addressCell.addElement(addressPara);

    PdfPCell mobileCell = new PdfPCell(mobilePara);
    mobileCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    mobileCell.setBorder(Rectangle.NO_BORDER);
    mobileCell.addElement(mobilePara);

    PdfPCell orderCell = new PdfPCell(orderPara);
    orderCell.setBorder(Rectangle.NO_BORDER);

    PdfPCell bCell = new PdfPCell(new Paragraph(" "));
    bCell.setBorder(Rectangle.NO_BORDER);

    tableHeader.addCell(supplierNameCell);
    tableHeader.addCell(addressCell);
    tableHeader.addCell(mobileCell);
    tableHeader.addCell(orderCell);
    tableHeader.addCell(bCell);

    return tableHeader;
}

From source file:com.cyberninjas.invoice.pdf.PdfInvoiceEditor.java

License:Open Source License

/**
 * Set the default font and size./*from  w  ww  .  j  av  a  2  s .c  om*/
 *
 * @param font path to the .ttf of the default font.
 * @param size default font size
 * @throws IOException on I/O error reading font file.
 */
public void setFontAndSize(final String font, final int size) throws IOException {
    try {
        defaultFont = BaseFont.createFont(font, BaseFont.WINANSI, BaseFont.EMBEDDED);
    } catch (DocumentException ex) {
        throw new PdfException("Failed to configure font", ex);
    }

    defaultFont.setSubset(true);

    defaultSize = size;
}

From source file:com.dandymadeproductions.ajqvue.structures.DataExportProperties.java

License:Open Source License

private void loadPDFFonts() {
    // Method Instances.
    String fileSeparator;/*from   w ww .  j  ava2  s .c  o  m*/
    String fontPath, fontName;
    String fontsDirectoryName;
    Iterator<String> fontNameIterator;
    File fontsDirectory;
    File[] fontsSubDirectories;
    String[] fontNames;
    TreeSet<String> fonts;

    // Setting up.
    fileSeparator = Utils.getFileSeparator();
    fontsDirectoryName = "fonts" + fileSeparator;
    fonts = new TreeSet<String>();

    // Insure have one default font.

    fontTreeMap.put(PDFExportPreferencesPanel.DEFAULT_FONT, new Font(Font.FontFamily.UNDEFINED));

    // Create the default registered fonts.

    fontNameIterator = FontFactory.getRegisteredFonts().iterator();

    while (fontNameIterator.hasNext()) {
        fontName = fontNameIterator.next();
        fontTreeMap.put(fontName, FontFactory.getFont(fontName));
    }

    // Create embedded fonts from fonts directory.

    fontsDirectory = new File(fontsDirectoryName);

    if (fontsDirectory.exists()) {
        // Handle one level of sub-directories.
        fontsSubDirectories = fontsDirectory.listFiles(new DirectoriesFilter());

        if (fontsSubDirectories != null) {
            for (int i = 0; i < fontsSubDirectories.length; i++) {
                fontNames = fontsSubDirectories[i].list(new FontNameFilter());

                if (fontNames != null)
                    for (int j = 0; j < fontNames.length; j++)
                        fonts.add(fontsSubDirectories[i] + fileSeparator + fontNames[j]);
            }
        }

        // Handle all direct font names in the directory.
        fontNames = fontsDirectory.list(new FontNameFilter());

        if (fontNames != null) {
            for (int i = 0; i < fontNames.length; i++)
                fonts.add(fontsDirectoryName + fontNames[i]);
        }

        // Load the found fonts.
        fontNameIterator = fonts.iterator();

        while (fontNameIterator.hasNext()) {
            fontPath = fontNameIterator.next();
            if (fontPath.indexOf(fileSeparator) != -1)
                fontName = fontPath.substring((fontPath.lastIndexOf(fileSeparator) + 1), fontPath.indexOf("."));
            else
                fontName = fontPath.substring(0, fontPath.indexOf("."));

            // System.out.println(fontName + " " + fontPath);
            try {
                BaseFont currentBaseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                        BaseFont.EMBEDDED);
                fontTreeMap.put(fontName, new Font(currentBaseFont, 12));
            } catch (DocumentException de) {
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.dandymadeproductions.myjsqlview.structures.DataExportProperties.java

License:Open Source License

private void loadPDFFonts() {
    // Method Instances.
    String fileSeparator;/*from   w  ww . ja va 2  s.c o m*/
    String fontPath, fontName;
    String fontsDirectoryName;
    Iterator<String> fontNameIterator;
    File fontsDirectory;
    File[] fontsSubDirectories;
    String[] fontNames;
    TreeSet<String> fonts;

    // Setting up.
    fileSeparator = MyJSQLView_Utils.getFileSeparator();
    fontsDirectoryName = "fonts" + fileSeparator;
    fonts = new TreeSet<String>();

    // Insure have one default font.

    fontTreeMap.put(PDFExportPreferencesPanel.DEFAULT_FONT, new Font(Font.FontFamily.UNDEFINED));

    // Create the default registered fonts.

    fontNameIterator = FontFactory.getRegisteredFonts().iterator();

    while (fontNameIterator.hasNext()) {
        fontName = fontNameIterator.next();
        fontTreeMap.put(fontName, FontFactory.getFont(fontName));
    }

    // Create embedded fonts from fonts directory.

    fontsDirectory = new File(fontsDirectoryName);

    if (fontsDirectory.exists()) {
        // Handle one level of sub-directories.
        fontsSubDirectories = fontsDirectory.listFiles(directoriesFilter);

        for (int i = 0; i < fontsSubDirectories.length; i++) {
            fontNames = fontsSubDirectories[i].list(fontNameFilter);

            for (int j = 0; j < fontNames.length; j++)
                fonts.add(fontsSubDirectories[i] + fileSeparator + fontNames[j]);
        }

        // Handle all direct font names in the directory.
        fontNames = fontsDirectory.list(fontNameFilter);

        for (int i = 0; i < fontNames.length; i++)
            fonts.add(fontsDirectoryName + fontNames[i]);

        // Load the found fonts.
        fontNameIterator = fonts.iterator();

        while (fontNameIterator.hasNext()) {
            fontPath = fontNameIterator.next();
            if (fontPath.indexOf(fileSeparator) != -1)
                fontName = fontPath.substring((fontPath.lastIndexOf(fileSeparator) + 1), fontPath.indexOf("."));
            else
                fontName = fontPath.substring(0, fontPath.indexOf("."));

            // System.out.println(fontName + " " + fontPath);
            try {
                BaseFont currentBaseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                        BaseFont.EMBEDDED);
                fontTreeMap.put(fontName, new Font(currentBaseFont, 12));
            } catch (DocumentException de) {
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.dexter.fms.mbean.ReportsMBean.java

@SuppressWarnings("unchecked")
public void createPDF(int type, String filename, int pageType) {
    try {//from   w ww.ja va  2  s.c o m
        FacesContext context = FacesContext.getCurrentInstance();
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new HeaderFooter());
        writer.setBoxSize("footer", new Rectangle(36, 54, 559, 788));
        if (!document.isOpen()) {
            document.open();
        }

        switch (pageType) {
        case 1:
            document.setPageSize(PageSize.A4);
            break;
        case 2:
            document.setPageSize(PageSize.A4.rotate());
            break;
        }
        document.addAuthor("FMS");
        document.addCreationDate();
        document.addCreator("FMS");
        document.addSubject("Report");
        document.addTitle(getReport_title());

        PdfPTable headerTable = new PdfPTable(1);

        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
                .getContext();
        String logo = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "images"
                + File.separator + "satraklogo.jpg";

        Hashtable<String, Object> params = new Hashtable<String, Object>();
        params.put("partner", dashBean.getUser().getPartner());
        GeneralDAO gDAO = new GeneralDAO();
        Object pSettingsObj = gDAO.search("PartnerSetting", params);
        PartnerSetting setting = null;
        if (pSettingsObj != null) {
            Vector<PartnerSetting> pSettingsList = (Vector<PartnerSetting>) pSettingsObj;
            for (PartnerSetting e : pSettingsList) {
                setting = e;
            }
        }
        gDAO.destroy();

        PdfPCell c = null;
        if (setting != null && setting.getLogo() != null) {
            Image logoImg = Image.getInstance(setting.getLogo());
            logoImg.scaleToFit(212, 51);
            c = new PdfPCell(logoImg);
        } else
            c = new PdfPCell(Image.getInstance(logo));
        c.setBorder(0);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        headerTable.addCell(c);

        Paragraph stars = new Paragraph(20);
        stars.add(Chunk.NEWLINE);
        stars.setSpacingAfter(20);

        c = new PdfPCell(stars);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
            //font exception
        }
        Font font = new Font(helvetica, 16, Font.NORMAL | Font.BOLD);

        c = new PdfPCell(new Paragraph(getReport_title(), font));
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);

        if (getReport_start_dt() != null && getReport_end_dt() != null) {
            stars = new Paragraph(20);
            stars.add(Chunk.NEWLINE);
            stars.setSpacingAfter(10);

            c = new PdfPCell(stars);
            c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            c.setBorder(0);
            headerTable.addCell(c);

            new Font(helvetica, 12, Font.NORMAL);
            Paragraph ch = new Paragraph("From " + getReport_start_dt() + " to " + getReport_end_dt(), font);
            c = new PdfPCell(ch);
            c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            headerTable.addCell(c);
        }
        stars = new Paragraph(20);
        stars.add(Chunk.NEWLINE);
        stars.setSpacingAfter(20);

        c = new PdfPCell(stars);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);
        document.add(headerTable);

        PdfPTable pdfTable = exportPDFTable(type);
        if (pdfTable != null)
            document.add(pdfTable);

        //Keep modifying your pdf file (add pages and more)

        document.close();
        String fileName = filename + ".pdf";

        writeFileToResponse(context.getExternalContext(), baos, fileName, "application/pdf");

        context.responseComplete();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dexter.fms.mbean.ReportsMBean.java

@SuppressWarnings("deprecation")
private PdfPTable exportPDFTable(int type) {
    int numberOfColumns = 0;
    PdfPTable pdfTable = null;//w  ww  .j a  va2s . c  o m
    switch (type) {
    case 1: // all users report
    {
        numberOfColumns = 7;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("User name", font));
        pdfTable.addCell(new Paragraph("Full name", font));
        pdfTable.addCell(new Paragraph("Email", font));
        pdfTable.addCell(new Paragraph("Department", font));
        pdfTable.addCell(new Paragraph("Region", font));
        pdfTable.addCell(new Paragraph("Phone", font));
        pdfTable.addCell(new Paragraph("Position", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getAllUsers() != null) {
            for (PartnerUser e : getAllUsers()) {
                pdfTable.addCell(new Paragraph(e.getUsername(), font));
                pdfTable.addCell(new Paragraph(
                        e.getPersonel().getFirstname() + " " + e.getPersonel().getLastname(), font));
                pdfTable.addCell(new Paragraph(e.getPersonel().getEmail(), font));
                pdfTable.addCell(new Paragraph(
                        (e.getPersonel().getDepartment() != null) ? e.getPersonel().getDepartment().getName()
                                : "N/A",
                        font));
                pdfTable.addCell(new Paragraph(
                        (e.getPersonel().getRegion() != null) ? e.getPersonel().getRegion().getName() : "N/A",
                        font));
                pdfTable.addCell(new Paragraph(e.getPersonel().getPhone(), font));
                pdfTable.addCell(new Paragraph(e.getPersonel().getPosition(), font));
            }
        }
        break;
    }
    case 2: // due driver's license
    {
        numberOfColumns = 6;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Full Name", font));
        pdfTable.addCell(new Paragraph("Dept", font));
        pdfTable.addCell(new Paragraph("Region", font));
        pdfTable.addCell(new Paragraph("Phone", font));
        pdfTable.addCell(new Paragraph("Position", font));
        pdfTable.addCell(new Paragraph("License Due Date", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getDueDriversLic() != null) {
            for (DriverLicense e : getDueDriversLic()) {
                pdfTable.addCell(new Paragraph(e.getDriver().getPersonel().getFirstname() + " "
                        + e.getDriver().getPersonel().getLastname(), font));
                pdfTable.addCell(new Paragraph(e.getDriver().getPersonel().getDepartment() != null
                        ? e.getDriver().getPersonel().getDepartment().getName()
                        : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getDriver().getPersonel().getRegion() != null
                        ? e.getDriver().getPersonel().getRegion().getName()
                        : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getDriver().getPersonel().getPhone(), font));
                pdfTable.addCell(new Paragraph(e.getDriver().getPersonel().getPosition(), font));
                pdfTable.addCell(new Paragraph(e.getLic_end_dt().toLocaleString(), font));
            }
        }
        break;
    }
    case 3: // accidents
    {
        numberOfColumns = 8;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Accident Date", font));
        pdfTable.addCell(new Paragraph("Description", font));
        pdfTable.addCell(new Paragraph("Driver", font));
        pdfTable.addCell(new Paragraph("Driver Comment", font));
        pdfTable.addCell(new Paragraph("Police Officer", font));
        pdfTable.addCell(new Paragraph("Police Station", font));
        pdfTable.addCell(new Paragraph("Police Comment", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getVehicleAccidents() != null) {
            for (VehicleAccident e : getVehicleAccidents()) {
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(e.getAccident_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getAccidentDescription(), font));
                pdfTable.addCell(new Paragraph(e.getDriver_name(), font));
                pdfTable.addCell(new Paragraph(e.getDriverComment(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceOfficer(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceStation(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceComment(), font));
            }
        }
        break;
    }
    case 4: // vehicles by brand
    {
        numberOfColumns = 4;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Make", font));
        pdfTable.addCell(new Paragraph("Model", font));
        pdfTable.addCell(new Paragraph("Year", font));
        pdfTable.addCell(new Paragraph("Reg No.", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getVehiclesByBrand() != null) {
            for (Vehicle e : getVehiclesByBrand()) {
                pdfTable.addCell(new Paragraph(e.getModel().getMaker().getName(), font));
                pdfTable.addCell(new Paragraph(e.getModel().getName(), font));
                pdfTable.addCell(new Paragraph(e.getModel().getYear(), font));
                pdfTable.addCell(new Paragraph(e.getRegistrationNo(), font));
            }
        }
        break;
    }
    case 5: // routine maintenance
    {
        numberOfColumns = 8;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Requester", font));
        pdfTable.addCell(new Paragraph("Description", font));
        pdfTable.addCell(new Paragraph("Start Date", font));
        pdfTable.addCell(new Paragraph("Initial Cost", font));
        pdfTable.addCell(new Paragraph("Status", font));
        pdfTable.addCell(new Paragraph("Close Date", font));
        pdfTable.addCell(new Paragraph("Final Cost", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getRmaints() != null) {
            for (VehicleRoutineMaintenance e : getRmaints()) {
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(e.getCreatedBy().getPersonel().getFirstname() + " "
                        + e.getCreatedBy().getPersonel().getLastname(), font));
                pdfTable.addCell(new Paragraph(e.getDescription(), font));
                pdfTable.addCell(new Paragraph(e.getStart_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getInitial_amount().toPlainString(), font));
                pdfTable.addCell(new Paragraph(e.getStatus(), font));
                pdfTable.addCell(new Paragraph(e.getClose_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getClosed_amount().toPlainString(), font));
            }
        }
        break;
    }
    case 6: // ad hoc maintenance
    {
        numberOfColumns = 8;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Requester", font));
        pdfTable.addCell(new Paragraph("Description", font));
        pdfTable.addCell(new Paragraph("Start Date", font));
        pdfTable.addCell(new Paragraph("Initial Cost", font));
        pdfTable.addCell(new Paragraph("Status", font));
        pdfTable.addCell(new Paragraph("Close Date", font));
        pdfTable.addCell(new Paragraph("Final Cost", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getAdhocmaints() != null) {
            for (VehicleAdHocMaintenance e : getAdhocmaints()) {
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(e.getCreatedBy().getPersonel().getFirstname() + " "
                        + e.getCreatedBy().getPersonel().getLastname(), font));
                pdfTable.addCell(new Paragraph(e.getDescription(), font));
                pdfTable.addCell(new Paragraph(e.getStart_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getInitial_cost().toPlainString(), font));
                pdfTable.addCell(new Paragraph(e.getStatus(), font));
                pdfTable.addCell(new Paragraph(e.getClose_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getClosed_cost().toPlainString(), font));
            }
        }
        break;
    }
    case 7: // active accidents
    {
        numberOfColumns = 8;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Accident Date", font));
        pdfTable.addCell(new Paragraph("Description", font));
        pdfTable.addCell(new Paragraph("Driver Name", font));
        pdfTable.addCell(new Paragraph("Driver Comment", font));
        pdfTable.addCell(new Paragraph("Police Name", font));
        pdfTable.addCell(new Paragraph("Station Name", font));
        pdfTable.addCell(new Paragraph("Police Comment", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getActiveAccidents() != null) {
            for (VehicleAccident e : getActiveAccidents()) {
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(e.getAccident_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getAccidentDescription(), font));
                pdfTable.addCell(new Paragraph(e.getDriver_name(), font));
                pdfTable.addCell(new Paragraph(e.getDriverComment(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceOfficer(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceStation(), font));
                pdfTable.addCell(new Paragraph(e.getPoliceComment(), font));
            }
        }
        break;
    }
    case 8: // coporate trips
    {
        numberOfColumns = 9;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 6, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Requester", font));
        pdfTable.addCell(new Paragraph("Department", font));
        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Departure Location", font));
        pdfTable.addCell(new Paragraph("Arrival Location", font));
        pdfTable.addCell(new Paragraph("Departure Date and Time", font));
        pdfTable.addCell(new Paragraph("Est. Arrival Date and Time", font));
        pdfTable.addCell(new Paragraph("Act. Arrival Date and Time", font));
        pdfTable.addCell(new Paragraph("Status", font));

        font = new Font(helvetica, 6, Font.NORMAL);

        if (getCorTrips() != null) {
            for (CorporateTrip e : getCorTrips()) {
                pdfTable.addCell(
                        new Paragraph(e.getStaff().getFirstname() + " " + e.getStaff().getLastname(), font));
                pdfTable.addCell(new Paragraph(
                        (e.getStaff().getDepartment() != null) ? e.getStaff().getDepartment().getName() : "N/A",
                        font));
                pdfTable.addCell(new Paragraph(
                        e.getVehicle() != null ? e.getVehicle().getRegistrationNo() : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getDepartureLocation(), font));
                pdfTable.addCell(new Paragraph(e.getArrivalLocation(), font));
                pdfTable.addCell(new Paragraph(e.getDepartureDateTime().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getEstimatedArrivalDateTime().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(
                        e.getCompleteRequestDateTime() != null ? e.getCompleteRequestDateTime().toLocaleString()
                                : "N/A",
                        font));
                pdfTable.addCell(new Paragraph(e.getTripStatus(), font));
            }
        }
        break;
    }
    case 9: // expense report
    {
        numberOfColumns = 4;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Type", font));
        pdfTable.addCell(new Paragraph("Date", font));
        pdfTable.addCell(new Paragraph("Amount", font));
        pdfTable.addCell(new Paragraph("Detail", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getExpenses() != null) {
            for (Expense e : getExpenses()) {
                pdfTable.addCell(new Paragraph(e.getType().getName(), font));
                pdfTable.addCell(new Paragraph(e.getExpense_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph("" + e.getAmount(), font));
                pdfTable.addCell(new Paragraph(e.getRemarks(), font));
            }
        }
        break;
    }
    case 10: // vehicle ages
    {
        numberOfColumns = 6;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Make", font));
        pdfTable.addCell(new Paragraph("Model", font));
        pdfTable.addCell(new Paragraph("Year", font));
        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Date of Purchase", font));
        pdfTable.addCell(new Paragraph("Age (in years)", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getVehiclesAges() != null) {
            for (Vehicle e : getVehiclesAges()) {
                pdfTable.addCell(new Paragraph(
                        e.getModel().getMaker() != null ? e.getModel().getMaker().getName() : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getModel() != null ? e.getModel().getName() : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getModel() != null ? e.getModel().getYear() : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(
                        e.getPurchaseDate() != null ? e.getPurchaseDate().toLocaleString() : "N/A", font));
                pdfTable.addCell(new Paragraph("" + e.getAge(), font));
            }
        }
        break;
    }
    case 11: // vehicle fuelings
    {
        numberOfColumns = 5;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Date", font));
        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Litres", font));
        pdfTable.addCell(new Paragraph("Amount", font));
        pdfTable.addCell(new Paragraph("Source", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getFuelings() != null) {
            for (VehicleFueling e : getFuelings()) {
                pdfTable.addCell(new Paragraph(e.getCaptured_dt().toLocaleString(), font));
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph("" + e.getLitres(), font));
                pdfTable.addCell(new Paragraph("" + e.getAmt(), font));
                pdfTable.addCell(new Paragraph(e.getSource(), font));
            }
        }
        break;
    }
    case 12: // due vehicle licenses
    {
        numberOfColumns = 5;
        pdfTable = new PdfPTable(numberOfColumns);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        Font font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Reg No.", font));
        pdfTable.addCell(new Paragraph("Fleet", font));
        pdfTable.addCell(new Paragraph("Make", font));
        pdfTable.addCell(new Paragraph("Model", font));
        pdfTable.addCell(new Paragraph("License Due Date", font));

        font = new Font(helvetica, 8, Font.NORMAL);

        if (getDueVehicleLicenses() != null) {
            for (VehicleLicense e : getDueVehicleLicenses()) {
                pdfTable.addCell(new Paragraph(e.getVehicle().getRegistrationNo(), font));
                pdfTable.addCell(new Paragraph(e.getVehicle().getFleet().getName(), font));
                pdfTable.addCell(new Paragraph(
                        e.getVehicle().getModel() != null ? e.getVehicle().getModel().getMaker().getName()
                                : "N/A",
                        font));
                pdfTable.addCell(new Paragraph(
                        e.getVehicle().getModel() != null ? e.getVehicle().getModel().getName() : "N/A", font));
                pdfTable.addCell(new Paragraph(e.getLic_end_dt().toLocaleString(), font));
            }
        }
        break;
    }
    }

    pdfTable.setWidthPercentage(100);

    return pdfTable;
}

From source file:com.dexter.fuelcard.mbean.UtilMBean.java

public byte[] createInvoicePDF(int pageType, String title, String dateperiod, String amount, String chargeType,
        String chargeAmount, String total) {
    try {// w w  w  .  jav  a  2  s.co  m
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new HeaderFooter());
        writer.setBoxSize("footer", new Rectangle(36, 54, 559, 788));
        if (!document.isOpen()) {
            document.open();
        }

        switch (pageType) {
        case 1:
            document.setPageSize(PageSize.A4);
            break;
        case 2:
            document.setPageSize(PageSize.A4.rotate());
            break;
        }
        document.addAuthor("FUELCARD");
        document.addCreationDate();
        document.addCreator("FUELCARD");
        document.addSubject("Invoice");
        document.addTitle(title);

        PdfPTable headerTable = new PdfPTable(1);

        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
                .getContext();
        String logo = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "images"
                + File.separator + "satraklogo.jpg";

        PdfPCell c = new PdfPCell(Image.getInstance(logo));
        c.setBorder(0);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        headerTable.addCell(c);

        Paragraph stars = new Paragraph(20);
        stars.add(Chunk.NEWLINE);
        stars.setSpacingAfter(20);

        c = new PdfPCell(stars);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);

        BaseFont helvetica = null;
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
            //font exception
        }
        Font font = new Font(helvetica, 16, Font.NORMAL | Font.BOLD);

        c = new PdfPCell(new Paragraph(title, font));
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);

        if (dateperiod != null) {
            stars = new Paragraph(20);
            stars.add(Chunk.NEWLINE);
            stars.setSpacingAfter(10);

            c = new PdfPCell(stars);
            c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            c.setBorder(0);
            headerTable.addCell(c);

            new Font(helvetica, 12, Font.NORMAL);
            Paragraph ch = new Paragraph("For " + dateperiod, font);
            c = new PdfPCell(ch);
            c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            headerTable.addCell(c);
        }
        stars = new Paragraph(20);
        stars.add(Chunk.NEWLINE);
        stars.setSpacingAfter(20);

        c = new PdfPCell(stars);
        c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c.setBorder(0);
        headerTable.addCell(c);
        document.add(headerTable);

        PdfPTable pdfTable = new PdfPTable(4);
        try {
            helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (Exception e) {
        }
        font = new Font(helvetica, 8, Font.BOLDITALIC);

        pdfTable.addCell(new Paragraph("Charge Type", font)); // % per transaction or flat per license
        pdfTable.addCell(new Paragraph("Charge Amount", font)); // the amount of the charge setting

        if (chargeType.equalsIgnoreCase("Percent-Per-Tran")) {
            pdfTable.addCell(new Paragraph("Total Amount", font)); // the amount of the charge setting
        } else {
            pdfTable.addCell(new Paragraph("Total License", font)); // the amount of the charge setting
        }
        //pdfTable.addCell(new Paragraph("Description", font)); // the 
        pdfTable.addCell(new Paragraph("Amount Due", font));

        font = new Font(helvetica, 8, Font.NORMAL);
        pdfTable.addCell(new Paragraph(chargeType, font));
        pdfTable.addCell(new Paragraph(chargeAmount, font));
        pdfTable.addCell(new Paragraph(total, font));
        pdfTable.addCell(new Paragraph(amount, font));

        pdfTable.setWidthPercentage(100);
        if (pdfTable != null)
            document.add(pdfTable);

        //Keep modifying your pdf file (add pages and more)

        document.close();

        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}