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

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

Introduction

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

Prototype

public static BaseFont createFont(String name, String encoding, boolean embedded)
        throws DocumentException, IOException 

Source Link

Document

Creates a new font.

Usage

From source file:com.carfinance.module.common.controller.DocumentDownloadController.java

/**
 * ??//from w  ww . j a  v  a  2  s  .  c  o m
 * @param model
 * @param request
 * @param response
 */
@RequestMapping(value = "/pdfhunchecontrace", method = RequestMethod.GET)
public void pdfHuncheContrace(Model model, HttpServletRequest request, HttpServletResponse response) {
    String contrace_id_str = request.getParameter("contrace_id");

    String org_id = "";
    String contrace_no = "";
    String customer_name = "";
    String vehicle_id = "";
    String daily_available_km = "";

    VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService
            .getVehicleContraceInfoById(Long.valueOf(contrace_id_str));
    if (vehicleContraceInfo != null) {
        org_id = String.valueOf(vehicleContraceInfo.getOrg_id());
        contrace_no = vehicleContraceInfo.getContrace_no();
        customer_name = vehicleContraceInfo.getCustomer_name();
        daily_available_km = vehicleContraceInfo.getDaily_available_km() + "";
    } else {
        PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService
                .getPropertyContraceInfoById(Long.valueOf(contrace_id_str));
        if (propertyContraceInfo != null) {
            org_id = String.valueOf(propertyContraceInfo.getOrg_id());
            contrace_no = propertyContraceInfo.getContrace_no();
            customer_name = propertyContraceInfo.getCustomer_name();
        }
    }

    List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService
            .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str));

    //1.ContentType
    response.setContentType("multipart/form-data");
    //2.????(??a.pdf)
    response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".pdf");

    Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 50);
    // ?? pdf ?
    try {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);

        Font bold_fontChinese = new Font(bfChinese, 18, Font.BOLD, BaseColor.BLACK);
        Font normal_fontChinese = new Font(bfChinese, 12, Font.NORMAL, BaseColor.BLACK);
        Font normal_desc_fontChinese = new Font(bfChinese, 6, Font.NORMAL, BaseColor.BLACK);

        FileOutputStream pdfFile = new FileOutputStream(
                new File(appProps.get("hunche.contrace.download.path") + contrace_no + ".pdf"));
        // pdf ?
        Paragraph paragraph1 = new Paragraph("???", bold_fontChinese);
        paragraph1.setAlignment(Element.ALIGN_CENTER);

        Paragraph paragraph2 = new Paragraph(
                "???", normal_fontChinese);
        Paragraph paragraph3 = new Paragraph("" + customer_name + " (",
                normal_fontChinese);
        Paragraph paragraph4 = new Paragraph(
                "?????________________???",
                normal_fontChinese);
        Paragraph paragraph5 = new Paragraph("??????",
                normal_fontChinese);
        // table
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100);
        table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
        PdfPCell cell = new PdfPCell();
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        // 
        cell.setPhrase(new Paragraph("?", normal_fontChinese));
        table.addCell(cell);
        cell.setPhrase(new Paragraph("", normal_fontChinese));
        table.addCell(cell);
        cell.setPhrase(new Paragraph("?/", normal_fontChinese));
        table.addCell(cell);
        cell.setPhrase(new Paragraph("?", normal_fontChinese));
        table.addCell(cell);
        // ?
        for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) {

            PdfPCell newcell = new PdfPCell();
            newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            newcell.setPhrase(new Paragraph("1", normal_fontChinese));
            table.addCell(newcell);
            newcell.setPhrase(new Paragraph(v.getModel(), normal_fontChinese));
            table.addCell(newcell);
            newcell.setPhrase(new Paragraph(v.getDaily_price() + "", normal_fontChinese));
            table.addCell(newcell);
            newcell.setPhrase(new Paragraph(daily_available_km, normal_fontChinese));
            table.addCell(newcell);
        }

        Paragraph paragraph6 = new Paragraph(
                "??_________________________________________________________________",
                normal_fontChinese);
        Paragraph paragraph7 = new Paragraph(
                "____________________________?______",
                normal_fontChinese);
        Paragraph paragraph8 = new Paragraph("????", normal_fontChinese);
        Paragraph paragraph9 = new Paragraph(
                "???________________________???________________________",
                normal_fontChinese);
        Paragraph paragraph10 = new Paragraph("?", normal_fontChinese);
        Paragraph paragraph11 = new Paragraph(
                "???????????????",
                normal_fontChinese);
        Paragraph paragraph12 = new Paragraph("?", normal_fontChinese);
        Paragraph paragraph13 = new Paragraph(
                "??????????????",
                normal_fontChinese);
        Paragraph paragraph14 = new Paragraph(
                "?????????",
                normal_fontChinese);
        Paragraph paragraph15 = new Paragraph("?????",
                normal_fontChinese);
        Paragraph paragraph16 = new Paragraph(
                "(/)                                       (/)",
                normal_fontChinese);
        Paragraph paragraph17 = new Paragraph(
                "                                                      ??", normal_fontChinese);
        Paragraph paragraph18 = new Paragraph(
                "??                                               ??  ",
                normal_fontChinese);
        Paragraph paragraph19 = new Paragraph(
                "                                                      201          ",
                normal_fontChinese);

        //  Document ?File  PdfWriter ?
        PdfWriter.getInstance(pdfDoc, pdfFile);
        pdfDoc.open(); //  Document 

        // ??
        pdfDoc.add(paragraph1);
        pdfDoc.add(new Chunk("\n\n"));
        pdfDoc.add(paragraph2);
        pdfDoc.add(paragraph3);
        pdfDoc.add(paragraph4);
        pdfDoc.add(paragraph5);
        pdfDoc.add(table);
        pdfDoc.add(paragraph6);
        pdfDoc.add(paragraph7);
        pdfDoc.add(paragraph8);
        pdfDoc.add(paragraph9);
        pdfDoc.add(paragraph10);
        pdfDoc.add(paragraph11);
        pdfDoc.add(paragraph12);
        pdfDoc.add(paragraph13);
        pdfDoc.add(paragraph14);
        pdfDoc.add(paragraph15);
        pdfDoc.add(paragraph16);
        pdfDoc.add(paragraph17);
        pdfDoc.add(paragraph18);
        pdfDoc.add(paragraph19);

        pdfDoc.close();

        ServletOutputStream out;
        //File(?download.pdf)
        File file = new File(appProps.get("hunche.contrace.download.path") + contrace_no + ".pdf");

        try {
            FileInputStream inputStream = new FileInputStream(file);
            //3.response?ServletOutputStream(out)
            out = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[512];
            while (b != -1) {
                b = inputStream.read(buffer);
                //4.?(out)
                out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cib.statementstamper.windows.StatementStamperMainWindow.java

License:Open Source License

@Override
public void attach() {
    super.attach();
    try {//from   w ww  .  j  a  v  a  2 s .co  m
        logo = Image
                .getInstance(StatementStamperMainWindow.class.getResource("/resources/images/cib_logo.jpg"));
        logo.setAlignment(Image.LEFT);
        logo.scalePercent(30);
        isplogo = Image
                .getInstance(StatementStamperMainWindow.class.getResource("/resources/images/isplogo.jpg"));
        isplogo.setAlignment(Image.LEFT);
        isplogo.scalePercent(50);
        baseFontArial = BaseFont.createFont("/resources/fonts/ARIAL.TTF", BaseFont.CP1250, BaseFont.EMBEDDED);
        myFontBase = BaseFont.createFont("/resources/fonts/courbd.ttf", BaseFont.CP1250, BaseFont.EMBEDDED);
        bottomFont = new Font(baseFontArial, 7, Font.NORMAL, BaseColor.BLACK);
        bottomFontBold = new Font(baseFontArial, 7, Font.BOLD, BaseColor.BLACK);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    upload.addListener((Upload.SucceededListener) this);
    upload.addListener((Upload.StartedListener) this);
    upload.addListener((Upload.FinishedListener) this);
    upload.addListener((Upload.FailedListener) this);
    upload.setImmediate(true);
    addComponent(upload);
}

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

License:Open Source License

/**
 * Set the default font and size./*from ww w  .ja v a 2  s . co m*/
 *
 * @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 w w.j  a  v  a2 s .  c  om
    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 w w . ja  v a2  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  w w .j  av  a 2  s.co 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;//from   w ww .  ja va 2s. 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 {/*from   www  . j a v a 2 s .  c  om*/
        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;
    }
}

From source file:com.example.admin.avoidq.billGenerated.java

private void initializeFonts() {

    try {//  w w w  .  jav  a2  s. co m
        bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.albfernandez.joinpdf.JoinPdf.java

License:Open Source License

private void writePageNumber(final PdfContentByte cb) throws DocumentException, IOException {
    if (isPrintPageNumbers()) {
        Rectangle size = cb.getPdfDocument().getPageSize();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        this.actualPage++;
        cb.beginText();// w  w w.  ja  va  2s.co  m
        cb.setFontAndSize(bf, 9);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, this.actualPage + " of " + this.totalPages,
                size.getWidth() / 2, 10, 0);
        cb.endText();
    }
}