Example usage for com.lowagie.text.pdf BaseFont COURIER

List of usage examples for com.lowagie.text.pdf BaseFont COURIER

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont COURIER.

Prototype

String COURIER

To view the source code for com.lowagie.text.pdf BaseFont COURIER.

Click Source Link

Document

This is a possible value of a base 14 type 1 font

Usage

From source file:org.pz.platypus.plugin.html.HtmlFont.java

License:Open Source License

/**
 * Get the name by which iText refers to this font. This routine is mostly occupied
 * with the special handling of the base14 fonts. For all other fonts, this routine
 * simply returns its existing name.//  w  w w.ja  v  a  2 s  . c  om
 *
 * @param f PdfFont whose iText name we're getting
 * @return a string containing the iText usable name for this font.
 */
String createItextFontName(final HtmlFont f) {
    String iTextFontName;
    String typefaceName = f.typeface;

    // handle the different versions of base14 fonts
    if (typefaceName.equals("COURIER")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.COURIER_BOLDOBLIQUE;
            else
                iTextFontName = BaseFont.COURIER_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.COURIER_OBLIQUE;
        else
            iTextFontName = BaseFont.COURIER;
    } else if (typefaceName.equals("HELVETICA")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.HELVETICA_BOLDOBLIQUE;
            else
                iTextFontName = BaseFont.HELVETICA_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.HELVETICA_OBLIQUE;
        else
            iTextFontName = BaseFont.HELVETICA;
    } else if (typefaceName.equals("TIMES_ROMAN")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.TIMES_BOLDITALIC;
            else
                iTextFontName = BaseFont.TIMES_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.TIMES_ITALIC;
        else
            iTextFontName = BaseFont.TIMES_ROMAN;
    } else if (typefaceName.equals("SYMBOL")) {
        iTextFontName = BaseFont.SYMBOL;
    } else if (typefaceName.equals("DINGBATS")) {
        iTextFontName = BaseFont.ZAPFDINGBATS;
    } else
    // not a base14 font. So make sure we've loaded the font files for Platypus
    // then look up this font among them. If it's still not there, then return
    // a TIMES_ROMAN and note the error.
    {
        //            if( htmlData.getTypefaceMap() == null ) {
        //                TypefaceMap typefaceMap = new TypefaceMap( htmlData.getGdd() );
        //                typefaceMap.loadFamilies();
        //                htmlData.setTypefaceMap( typefaceMap );
        //            }

        // if the font files for this typeface/font family have not been previously registered,
        // then get the filenames from the typefaceMap and register them in iText's FontFactory
        if (!FontFactory.isRegistered(typefaceName)) {
            String[] fontFiles = pdfData.getTypefaceMap().getFamilyFilenames(typefaceName);
            for (String fontFile : fontFiles) {
                FontFactory.register(fontFile);
            }
            gdd.log("Registered fonts for " + typefaceName + " in iText");
        }

        if (FontFactory.isRegistered(typefaceName)) {
            iTextFontName = typefaceName;
        } else {
            // the filename does not exist on the system, so substitute TIMES_ROMAN
            iTextFontName = BaseFont.TIMES_ROMAN;
        }
        //            }
        //            else {
        //                gdd.logInfo(
        //                        gdd.getLit( "FILE#" ) + " " + source.getFileNumber() + " " +
        //                        gdd.getLit( "LINE#" ) + " " + source.getLineNumber() + ": " +
        //                        gdd.getLit( "ERROR.INVALID_FONT_TYPEFACE" ) + " " +
        //                        f.typeface + " " +
        //                        gdd.getLit( "IGNORED" ));
        //                iTextFontName = typeface;
    }
    return (iTextFontName);
}

From source file:org.pz.platypus.plugin.pdf.PdfFontFactory.java

License:Open Source License

/**
 * Gets the iText font name for any of the Base14 fonts.
 *
 * @param font the PdfFont whose name we're looking up.
 * @return the iText name or null if an error has occured
 *//*from   w ww .  j  ava 2  s  .c o m*/
String computeBase14ItextFontName(final PdfFont font) {
    final String typefaceName;
    String iTextFontName = null;
    PdfFont f;

    // in the impossible event this gets passed a null, then
    // replace it with the default font. It might be better
    // to just throw an exception. Should revisit this later.
    if (font == null) {
        f = new PdfFont(pdfData);
    } else {
        f = font;
    }

    typefaceName = f.getFace();

    if (typefaceName.equals("COURIER")) {
        if (f.getBold()) {
            if (f.getItalics()) {
                iTextFontName = BaseFont.COURIER_BOLDOBLIQUE;
            } else {
                iTextFontName = BaseFont.COURIER_BOLD;
            }
        } else if (f.getItalics()) {
            iTextFontName = BaseFont.COURIER_OBLIQUE;
        } else
            iTextFontName = BaseFont.COURIER;

        return (iTextFontName);
    }

    if (typefaceName.equals("HELVETICA")) {
        if (f.getBold()) {
            if (f.getItalics()) {
                iTextFontName = BaseFont.HELVETICA_BOLDOBLIQUE;
            } else {
                iTextFontName = BaseFont.HELVETICA_BOLD;
            }
        } else if (f.getItalics()) {
            iTextFontName = BaseFont.HELVETICA_OBLIQUE;
        } else {
            iTextFontName = BaseFont.HELVETICA;
        }

        return (iTextFontName);
    }

    if (typefaceName.equals("TIMES_ROMAN")) {
        if (f.getBold()) {
            if (f.getItalics())
                iTextFontName = BaseFont.TIMES_BOLDITALIC;
            else
                iTextFontName = BaseFont.TIMES_BOLD;
        } else if (f.getItalics())
            iTextFontName = BaseFont.TIMES_ITALIC;
        else
            iTextFontName = BaseFont.TIMES_ROMAN;

        return (iTextFontName);
    }

    if (typefaceName.equals("SYMBOL")) {
        iTextFontName = BaseFont.SYMBOL;
        return (iTextFontName);
    }

    if (typefaceName.equals("DINGBATS")) {
        iTextFontName = BaseFont.ZAPFDINGBATS;
        return (iTextFontName);
    }

    // in theory, impossible, since the font is validated before the function is called.
    return (iTextFontName);
}

From source file:org.pz.platypus.plugin.rtf.RtfFont.java

License:Open Source License

/**
 * Get the name by which iText refers to this font. This routine is mostly occupied
 * with the special handling of the base14 fonts.
 *
 * For all other fonts, this method makes sure the font is registered with iText and
 * returns its name as registered by iText (which is the family name for the font).
 *
 * @param f PdfFont whose iText name we're getting
 * @return a string containing the iText usable name for this font.
 *//*from  www.j a  v a2s . c om*/
String createItextFontName(final RtfFont f) {
    String iTextFontName;
    String typefaceName = f.typeface;

    // handle the different versions of base14 fonts
    if (typefaceName.equals("COURIER")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.COURIER_BOLDOBLIQUE;
            else
                iTextFontName = BaseFont.COURIER_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.COURIER_OBLIQUE;
        else
            iTextFontName = BaseFont.COURIER;
    } else if (typefaceName.equals("HELVETICA")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.HELVETICA_BOLDOBLIQUE;
            else
                iTextFontName = BaseFont.HELVETICA_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.HELVETICA_OBLIQUE;
        else
            iTextFontName = BaseFont.HELVETICA;
    } else if (typefaceName.equals("TIMES_ROMAN")) {
        if (f.bold) {
            if (f.italics)
                iTextFontName = BaseFont.TIMES_BOLDITALIC;
            else
                iTextFontName = BaseFont.TIMES_BOLD;
        } else if (f.italics)
            iTextFontName = BaseFont.TIMES_ITALIC;
        else
            iTextFontName = BaseFont.TIMES_ROMAN;
    } else if (typefaceName.equals("SYMBOL")) {
        iTextFontName = BaseFont.SYMBOL;
    } else if (typefaceName.equals("DINGBATS")) {
        iTextFontName = BaseFont.ZAPFDINGBATS;
    } else
    // It's not a base14 font. So make sure we've loaded the font files for Platypus
    // then look up this font among them. If it's still not there, then return
    // a TIMES_ROMAN and note the error.
    {
        if (!FontFactory.isRegistered(typefaceName)) {
            if (!findAndRegisterFont(typefaceName)) {
                return (null);
            }
        }

        if (FontFactory.isRegistered(typefaceName)) {
            iTextFontName = typefaceName;
        } else {
            // in theory, cannot get here.
            gdd.logWarning(gdd.getLit("COULD_NOT_FIND") + " " + typefaceName + " "
                    + gdd.getLit("IN_FONT_REGISTER") + ". " + gdd.getLit("USING_TIMES_ROMAN") + ".");
            iTextFontName = null;
        }
    }
    return (iTextFontName);
}

From source file:org.xhtmlrenderer.pdf.ITextFontResolver.java

License:Open Source License

private static void addCourier(HashMap result) throws DocumentException, IOException {
    FontFamily courier = new FontFamily();
    courier.setName("Courier");

    courier.addFontDescription(// ww w  . ja v  a2 s .co m
            new FontDescription(createFont(BaseFont.COURIER_BOLDOBLIQUE), IdentValue.OBLIQUE, 700));
    courier.addFontDescription(
            new FontDescription(createFont(BaseFont.COURIER_OBLIQUE), IdentValue.OBLIQUE, 400));
    courier.addFontDescription(new FontDescription(createFont(BaseFont.COURIER_BOLD), IdentValue.NORMAL, 700));
    courier.addFontDescription(new FontDescription(createFont(BaseFont.COURIER), IdentValue.NORMAL, 400));

    result.put("DialogInput", courier);
    result.put("Monospaced", courier);
    result.put("Courier", courier);
}

From source file:oscar.eform.util.EFormPDFServlet.java

License:Open Source License

private void writeContent(Properties printCfg, Properties props, Properties measurements, float height,
        PdfContentByte cb) throws Exception {
    for (Enumeration e = printCfg.propertyNames(); e.hasMoreElements();) {
        StringBuilder temp = new StringBuilder(e.nextElement().toString());
        String[] cfgVal = printCfg.getProperty(temp.toString()).split(" *, *");

        String[] fontType = null;
        int fontFlags = 0;
        if (cfgVal[4].indexOf(";") > -1) {
            fontType = cfgVal[4].split(";");
            if (fontType[1].trim().equals("italic"))
                fontFlags = Font.ITALIC;
            else if (fontType[1].trim().equals("bold"))
                fontFlags = Font.BOLD;
            else if (fontType[1].trim().equals("bolditalic"))
                fontFlags = Font.BOLDITALIC;
            else/*from   w  w  w .  j  a v  a2  s. co  m*/
                fontFlags = Font.NORMAL;
        } else {
            fontFlags = Font.NORMAL;
            fontType = new String[] { cfgVal[4].trim() };
        }

        String encoding = null;
        if (fontType[0].trim().equals("BaseFont.HELVETICA")) {
            fontType[0] = BaseFont.HELVETICA;
            encoding = BaseFont.CP1252; //latin1 encoding
        } else if (fontType[0].trim().equals("BaseFont.HELVETICA_OBLIQUE")) {
            fontType[0] = BaseFont.HELVETICA_OBLIQUE;
            encoding = BaseFont.CP1252;
        } else if (fontType[0].trim().equals("BaseFont.ZAPFDINGBATS")) {
            fontType[0] = BaseFont.ZAPFDINGBATS;
            encoding = BaseFont.ZAPFDINGBATS;
        } else {
            fontType[0] = BaseFont.COURIER;
            encoding = BaseFont.CP1252;
        }

        BaseFont bf = BaseFont.createFont(fontType[0], encoding, BaseFont.NOT_EMBEDDED);
        String propValue = props.getProperty(temp.toString());
        //if not in regular config then check measurements
        if (propValue == null) {
            propValue = measurements.getProperty(temp.toString(), "");
        }

        ColumnText ct = new ColumnText(cb);
        // write in a rectangle area
        if (cfgVal.length >= 9) {
            Font font = new Font(bf, Integer.parseInt(cfgVal[5].trim()), fontFlags);
            ct.setSimpleColumn(Integer.parseInt(cfgVal[1].trim()),
                    (height - Integer.parseInt(cfgVal[2].trim())), Integer.parseInt(cfgVal[7].trim()),
                    (height - Integer.parseInt(cfgVal[8].trim())), Integer.parseInt(cfgVal[9].trim()),
                    (cfgVal[0].trim().equals("left") ? Element.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? Element.ALIGN_RIGHT : Element.ALIGN_CENTER)));

            ct.setText(new Phrase(12, propValue, font));
            ct.go();
            continue;
        }

        // draw line directly
        if (temp.toString().startsWith("__$line")) {
            cb.setRGBColorStrokeF(0f, 0f, 0f);
            cb.setLineWidth(Float.parseFloat(cfgVal[4].trim()));
            cb.moveTo(Float.parseFloat(cfgVal[0].trim()), Float.parseFloat(cfgVal[1].trim()));
            cb.lineTo(Float.parseFloat(cfgVal[2].trim()), Float.parseFloat(cfgVal[3].trim()));
            cb.stroke();

        } else if (temp.toString().startsWith("__")) {
            cb.beginText();
            cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim()));
            cb.showTextAligned(
                    (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT
                                    : PdfContentByte.ALIGN_CENTER)),
                    (cfgVal.length >= 7 ? (cfgVal[6].trim()) : propValue), Integer.parseInt(cfgVal[1].trim()),
                    (height - Integer.parseInt(cfgVal[2].trim())), 0);
            cb.endText();
        } else { // write prop text
            cb.beginText();
            cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim()));
            cb.showTextAligned(
                    (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT
                                    : PdfContentByte.ALIGN_CENTER)),
                    (cfgVal.length >= 7 ? ((propValue.equals("") ? "" : cfgVal[6].trim())) : propValue),
                    Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0);

            cb.endText();
        }
    }
}

From source file:oscar.form.pdfservlet.FrmCustomedPDFServlet.java

License:Open Source License

protected ByteArrayOutputStream generatePDFDocumentBytes(final HttpServletRequest req, final ServletContext ctx)
        throws DocumentException {
    logger.debug("***in generatePDFDocumentBytes2 FrmCustomedPDFServlet.java***");
    // added by vic, hsfo
    Enumeration<String> em = req.getParameterNames();
    while (em.hasMoreElements()) {
        logger.debug("para=" + em.nextElement());
    }//from w ww  .j ava2s .c  o  m
    em = req.getAttributeNames();
    while (em.hasMoreElements())
        logger.debug("attr: " + em.nextElement());

    if (HSFO_RX_DATA_KEY.equals(req.getParameter("__title"))) {
        return generateHsfoRxPDF(req);
    }
    String newline = System.getProperty("line.separator");

    ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
    PdfWriter writer = null;
    String method = req.getParameter("__method");
    String origPrintDate = null;
    String numPrint = null;
    if (method != null && method.equalsIgnoreCase("rePrint")) {
        origPrintDate = req.getParameter("origPrintDate");
        numPrint = req.getParameter("numPrints");
    }

    logger.debug("method in generatePDFDocumentBytes " + method);
    String clinicName;
    String clinicTel;
    String clinicFax;
    // check if satellite clinic is used
    String useSatelliteClinic = req.getParameter("useSC");
    logger.debug(useSatelliteClinic);
    if (useSatelliteClinic != null && useSatelliteClinic.equalsIgnoreCase("true")) {
        String scAddress = req.getParameter("scAddress");
        logger.debug("clinic detail" + "=" + scAddress);
        HashMap<String, String> hm = parseSCAddress(scAddress);
        clinicName = hm.get("clinicName");
        clinicTel = hm.get("clinicTel");
        clinicFax = hm.get("clinicFax");
    } else {
        // parameters need to be passed to header and footer
        clinicName = req.getParameter("clinicName");
        logger.debug("clinicName" + "=" + clinicName);
        clinicTel = req.getParameter("clinicPhone");
        clinicFax = req.getParameter("clinicFax");
    }
    String patientPhone = req.getParameter("patientPhone");
    String patientCityPostal = req.getParameter("patientCityPostal");
    String patientAddress = req.getParameter("patientAddress");
    String patientName = req.getParameter("patientName");
    String sigDoctorName = req.getParameter("sigDoctorName");
    String rxDate = req.getParameter("rxDate");
    String rx = req.getParameter("rx");
    String patientDOB = req.getParameter("patientDOB");
    String showPatientDOB = req.getParameter("showPatientDOB");
    String imgFile = req.getParameter("imgFile");
    String patientHIN = req.getParameter("patientHIN");
    String patientChartNo = req.getParameter("patientChartNo");
    String pracNo = req.getParameter("pracNo");
    Locale locale = req.getLocale();

    boolean isShowDemoDOB = false;
    if (showPatientDOB != null && showPatientDOB.equalsIgnoreCase("true")) {
        isShowDemoDOB = true;
    }
    if (!isShowDemoDOB)
        patientDOB = "";
    if (rx == null) {
        rx = "";
    }

    String additNotes = req.getParameter("additNotes");
    String[] rxA = rx.split(newline);
    List<String> listRx = new ArrayList<String>();
    String listElem = "";
    // parse rx and put into a list of rx;
    for (String s : rxA) {

        if (s.equals("") || s.equals(newline) || s.length() == 1) {
            listRx.add(listElem);
            listElem = "";
        } else {
            listElem = listElem + s;
            listElem += newline;
        }

    }

    // get the print prop values
    Properties props = new Properties();
    StringBuilder temp = new StringBuilder();
    for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();) {
        temp = new StringBuilder(e.nextElement().toString());
        props.setProperty(temp.toString(), req.getParameter(temp.toString()));
    }

    for (Enumeration<String> e = req.getAttributeNames(); e.hasMoreElements();) {
        temp = new StringBuilder(e.nextElement().toString());
        props.setProperty(temp.toString(), req.getAttribute(temp.toString()).toString());
    }
    Document document = new Document();

    try {
        String title = req.getParameter("__title") != null ? req.getParameter("__title") : "Unknown";

        // specify the page of the picture using __graphicPage, it may be used multiple times to specify multiple pages
        // however the same graphic will be applied to all pages
        // ie. __graphicPage=2&__graphicPage=3
        String[] cfgGraphicFile = req.getParameterValues("__cfgGraphicFile");
        int cfgGraphicFileNo = cfgGraphicFile == null ? 0 : cfgGraphicFile.length;
        if (cfgGraphicFile != null) {
            // for (String s : cfgGraphicFile) {
            // p("cfgGraphicFile", s);
            // }
        }

        String[] graphicPage = req.getParameterValues("__graphicPage");
        ArrayList<String> graphicPageArray = new ArrayList<String>();
        if (graphicPage != null) {
            // for (String s : graphicPage) {
            // p("graphicPage", s);
            // }
            graphicPageArray = new ArrayList<String>(Arrays.asList(graphicPage));
        }

        // A0-A10, LEGAL, LETTER, HALFLETTER, _11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA
        // and FLSE
        // the following shows a temp way to get a print page size
        Rectangle pageSize = PageSize.LETTER;
        String pageSizeParameter = req.getParameter("rxPageSize");
        if (pageSizeParameter != null) {
            if ("PageSize.HALFLETTER".equals(pageSizeParameter)) {
                pageSize = PageSize.HALFLETTER;
            } else if ("PageSize.A6".equals(pageSizeParameter)) {
                pageSize = PageSize.A6;
            } else if ("PageSize.A4".equals(pageSizeParameter)) {
                pageSize = PageSize.A4;
            }
        }
        /*
         * if ("PageSize.HALFLETTER".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.HALFLETTER; } else if ("PageSize.A6".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.A6; } else if
         * ("PageSize.A4".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.A4; }
         */
        // p("size of page ", props.getProperty(PAGESIZE));

        document.setPageSize(pageSize);
        // 285=left margin+width of box, 5f is space for looking nice
        document.setMargins(15, pageSize.getWidth() - 285f + 5f, 170, 60);// left, right, top , bottom

        writer = PdfWriter.getInstance(document, baosPDF);
        writer.setPageEvent(new EndPage(clinicName, clinicTel, clinicFax, patientPhone, patientCityPostal,
                patientAddress, patientName, patientDOB, sigDoctorName, rxDate, origPrintDate, numPrint,
                imgFile, patientHIN, patientChartNo, pracNo, locale));
        document.addTitle(title);
        document.addSubject("");
        document.addKeywords("pdf, itext");
        document.addCreator("OSCAR");
        document.addAuthor("");
        document.addHeader("Expires", "0");

        document.open();
        document.newPage();

        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf; // = normFont;

        cb.setRGBColorStroke(0, 0, 255);
        // render prescriptions
        for (String rxStr : listRx) {
            bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Paragraph p = new Paragraph(new Phrase(rxStr, new Font(bf, 10)));
            p.setKeepTogether(true);
            p.setSpacingBefore(5f);
            document.add(p);
        }
        // render additional notes
        if (additNotes != null && !additNotes.equals("")) {
            bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Paragraph p = new Paragraph(new Phrase(additNotes, new Font(bf, 10)));
            p.setKeepTogether(true);
            p.setSpacingBefore(10f);
            document.add(p);
        }
        // render optometristEyeParam
        if (req.getAttribute("optometristEyeParam") != null) {
            bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Paragraph p = new Paragraph(
                    new Phrase("Eye " + (String) req.getAttribute("optometristEyeParam"), new Font(bf, 10)));
            p.setKeepTogether(true);
            p.setSpacingBefore(15f);
            document.add(p);
        }

        // render QrCode
        if (PrescriptionQrCodeUIBean.isPrescriptionQrCodeEnabledForCurrentProvider()) {
            Integer scriptId = Integer.parseInt(req.getParameter("scriptId"));
            byte[] qrCodeImage = PrescriptionQrCodeUIBean.getPrescriptionHl7QrCodeImage(scriptId);
            Image qrCode = Image.getInstance(qrCodeImage);
            document.add(qrCode);
        }

    } catch (DocumentException dex) {
        baosPDF.reset();
        throw dex;
    } catch (Exception e) {
        logger.error("Error", e);
    } finally {
        if (document != null) {
            document.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    logger.debug("***END in generatePDFDocumentBytes2 FrmCustomedPDFServlet.java***");
    return baosPDF;
}

From source file:recite18th.controller.Controller.java

License:Open Source License

public void print(String action) {
    /** thanks to http://www.java2s.com/Code/Java/PDF-RTF/DemonstratesthecreatingPDFinportraitlandscape.htm
     * QUICK FIX : do landscape//from  w  w w.j  ava2 s . c  o  m
     */
    response.setContentType("application/pdf"); // Code 1
    if (action.equals("download")) {
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Content-Disposition",
                "attachment; filename=\"" + "Report " + controllerName + ".pdf\"");
    }
    Document document = new Document(PageSize.A1.rotate());
    try {
        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); // Code 2
        document.open();

        // various fonts
        BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
        BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
        BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
        BaseFont bf_symbol = BaseFont.createFont(BaseFont.SYMBOL, "Cp1252", false);

        String headerImage = Config.base_path + "images/report-logo.gif";

        Image imghead = Image.getInstance(headerImage);
        imghead.setAbsolutePosition(0, 0);
        PdfContentByte cbhead = writer.getDirectContent();
        PdfTemplate tpLogo = cbhead.createTemplate(600, 300);
        tpLogo.addImage(imghead);

        PdfTemplate tpTitle = cbhead.createTemplate(1100, 300);
        String txtHeader = "BADAN KEPEGAWAIAN DAERAH PEMERINTAH DAERAH";//Config.application_title;
        tpTitle.beginText();
        tpTitle.setFontAndSize(bf_times, 36);
        tpTitle.showText(txtHeader);
        tpTitle.endText();

        PdfTemplate tpTitle2 = cbhead.createTemplate(900, 300);
        String txtHeader2 = "         KABUPATEN BANTUL YOGYAKARTA";
        tpTitle2.beginText();
        tpTitle2.setFontAndSize(bf_times, 36);
        tpTitle2.showText(txtHeader2);
        tpTitle2.endText();

        PdfTemplate tpAlamat = cbhead.createTemplate(1000, 400);
        tpAlamat.beginText();
        tpAlamat.setFontAndSize(bf_times, 24);
        tpAlamat.showText(
                "Alamat : Jln. R. W. Monginsidi No. 01 Kompleks Parasamya Bantul, Telp. (0274) 367509");
        tpAlamat.endText();

        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        java.util.Date dt = new java.util.Date();
        PdfTemplate tp3 = cbhead.createTemplate(600, 300);
        tp3.beginText();
        tp3.setFontAndSize(bf_times, 16);

        tp3.showText("Tanggal : " + df.format(dt));
        tp3.endText();

        cbhead.addTemplate(tpLogo, 800, 1500);//logo
        cbhead.addTemplate(tpTitle, 1000, 1580);
        cbhead.addTemplate(tpTitle2, 1000, 1540);
        cbhead.addTemplate(tpAlamat, 1000, 1500);//alamat
        cbhead.addTemplate(tp3, 270, 1500);//tanggal

        HeaderFooter header = new HeaderFooter(new Phrase(cbhead + "", new Font(bf_helv)), false);
        header.setAlignment(Element.ALIGN_CENTER);

        document.setHeader(header);

        //PdfContentByte cb = writer.getDirectContent();
        Paragraph par = new Paragraph(
                "\n\n\n\n\n\n\nLAPORAN DATA SELURUH " + controllerName.toUpperCase() + "\n");
        par.getFont().setStyle(Font.BOLD);
        par.getFont().setSize(18);
        par.setAlignment("center");
        document.add(par);
        document.add(new Paragraph("\n\n"));

        // get data
        initSqlViewDataPerPage();
        if (sqlViewDataPerPageForReport == null) {
            sqlViewDataPerPageForReport = sqlViewDataPerPage;
        }
        PreparedStatement pstmt = Db.getCon().prepareStatement(sqlViewDataPerPageForReport,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = pstmt.executeQuery();
        ResultSetMetaData metaColumn = resultSet.getMetaData();
        int nColoumn = metaColumn.getColumnCount();
        // thanks to set cell width http://www.jexp.ru/index.php/Java/PDF_RTF/Table_Cell_Size#Setting_Cell_Widths
        if (nColoumn > 0) {
            Model model = initModel();
            String tableName = model.getTableName();
            // create table header
            //     float[] widths = {1, 4};
            PdfPTable table;// = new PdfPTable(nColoumn);
            PdfPCell cell = new PdfPCell(new Paragraph("Daftar " + controllerName));

            Hashtable hashModel = TableCustomization.getTable(model.getTableName());

            int ncolumnHeader = nColoumn + 1; // +1 because of row. number
            if (hashModel != null) {
                ncolumnHeader = Integer.parseInt("" + hashModel.get("columnCount")) + 1;
            }
            table = new PdfPTable(ncolumnHeader);
            cell.setColspan(ncolumnHeader);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);

            Paragraph p2 = new Paragraph("No.");
            p2.getFont().setSize(20);
            PdfPCell cellColNo = new PdfPCell(p2);
            cellColNo.setNoWrap(true);
            cellColNo.setMinimumHeight(50);
            cellColNo.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cellColNo);

            if (hashModel != null) {
                Enumeration k = hashModel.keys();
                while (k.hasMoreElements()) {
                    String key = (String) k.nextElement();
                    if (key.equals("columnCount")) {
                        continue;
                    }
                    PdfPCell cellCol = new PdfPCell(new Paragraph(hashModel.get(key) + ""));
                    cellCol.setNoWrap(true);
                    cellCol.setMinimumHeight(50);
                    cellCol.setHorizontalAlignment(Element.ALIGN_CENTER);

                    table.addCell(cellCol);
                }
            } else {
                for (int i = 1; i < ncolumnHeader; i++) {
                    System.out.println("DATA = " + metaColumn.getColumnName(i));
                    Paragraph p1 = new Paragraph(metaColumn.getColumnName(i) + "");
                    p1.getFont().setSize(20);
                    PdfPCell cellCol = new PdfPCell(p1);
                    cellCol.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cellCol);
                }
            }

            //iterate all columns : table data
            resultSet.beforeFirst();
            int row = 1;
            while (resultSet.next()) {
                System.out.println(row);
                Paragraph p3 = new Paragraph(row + "");
                p3.getFont().setSize(20);
                cell = new PdfPCell(p3);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);
                if (hashModel != null) {//skip dulu u/ kasus ga pny class kustomasi table
                    Enumeration k = hashModel.keys();
                    while (k.hasMoreElements()) {
                        String key = (String) k.nextElement();
                        if (key.equals("columnCount")) {
                            continue;
                        }
                        table.addCell(resultSet.getObject(key) + "");
                    }
                } else {
                    for (int i = 1; i < ncolumnHeader; i++) {
                        System.out.println("DATA = " + metaColumn.getColumnName(i));
                        Paragraph p1 = new Paragraph(resultSet.getObject(metaColumn.getColumnName(i)) + "");
                        p1.getFont().setSize(18);
                        PdfPCell cellCol = new PdfPCell(p1);
                        cellCol.setHorizontalAlignment(Element.ALIGN_CENTER);
                        table.addCell(cellCol);
                    }
                }

                row++;
            }

            document.add(table);
            document.add(new Paragraph("\n\n"));
            par = new Paragraph("Mengetahui");
            par.setAlignment("center");
            document.add(par);
            par = new Paragraph("Kepada Badan Kepegawaian");
            par.setAlignment("center");
            document.add(par);
            par = new Paragraph("\n\n\n");

            document.add(par);
            par = new Paragraph("Drs. Maman Permana");
            par.setAlignment("center");
            document.add(par);
            par = new Paragraph("Nip: 197802042006041013");
            par.setAlignment("center");

            document.add(par);

        }
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}