Example usage for com.itextpdf.text Paragraph getLeading

List of usage examples for com.itextpdf.text Paragraph getLeading

Introduction

In this page you can find the example usage for com.itextpdf.text Paragraph getLeading.

Prototype

public float getLeading() 

Source Link

Document

Gets the leading of this phrase.

Usage

From source file:Controller.app.ConsultaController.java

private File createPDF(TblServicioFactura factura) throws IOException {
    String data = factura.getTesPagoResponse();
    JSONObject obj = new JSONObject(data);
    JSONArray content = obj.getJSONArray("lineaFactura");
    String temp = "";
    File _file = null;/*from   w w w .j a v  a 2 s . c  o  m*/
    Document doc = null;
    OutputStream file = null;
    int page = 0;
    try {
        _file = File.createTempFile("temp_file", ".pdf");
        TblServicioServicio servicio = servicios.search(factura.getTesCodigoSintesisBi().toString());
        file = new FileOutputStream(_file);
        doc = new Document(PageSize.LETTER);
        doc.setMargins(servicio.getMarginLeft().floatValue(), servicio.getMarginRight().floatValue(),
                servicio.getMarginTop().floatValue(), servicio.getMarginBottom().floatValue());
        PdfWriter.getInstance(doc, file);
        doc.open();
        //            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        ClassLoader classloader = getClass().getClassLoader();
        URL url = classloader.getResource("cour.ttf");
        BaseFont base = null;
        if (url == null) {
            base = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } else {
            String path = url.getPath();
            if ("/".equals(path.substring(0, 1))) {
                path = path.substring(1);
            }
            Logger.getLogger(ConsultaController.class.getName()).log(Level.INFO, path);
            base = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }
        Font f = new Font(base, servicio.getFontSize(), Font.NORMAL, BaseColor.BLACK);
        String qr = "";
        float line = 0;
        for (Object item : content) {
            Paragraph paragraph = new Paragraph(item.toString(), f);
            if (servicio.getTesDetalleVc().trim().equals("PAGO ENTEL")) {
                if (item.toString().contains("P X#X&$#&K##")) {
                    paragraph = new Paragraph(" ", f);
                    doc.add(paragraph);
                    doc.add(paragraph);
                    paragraph = new Paragraph(item.toString(), f);
                }
            }
            if (item.toString().contains("<b>")) {
                Font bold = new Font(base, 7.0f, Font.BOLD, BaseColor.BLACK);
                paragraph = new Paragraph(item.toString().replace("<b>", ""), bold);
            }
            if (item.toString().contains("<QR>") || item.toString().contains("<QR_ENT_G>")) {
                qr = item.toString().replaceAll("<QR>", "");
                qr = item.toString().replaceAll("<QR_ENT_G>", "");
                Image image = QR.create(qr);
                image.scaleAbsolute(servicio.getQrScale().floatValue(), servicio.getQrScale().floatValue());
                if (servicio.getTesDetalleVc().trim().equals("PAGO ENTEL")
                        || servicio.getTesDetalleVc().trim().equals("PAGO TELECEL")
                        || servicio.getTesDetalleVc().trim().equals("PAGO NUEVATEL")
                        || servicio.getTesDetalleVc().trim().equals("PAGO TIGOSTAR (MULTIVISION)")) {
                    float y = (doc.getPageSize().getHeight() - (paragraph.getLeading() * (line + 1)));
                    float x = 0;
                    if (servicio.getTesDetalleVc().trim().equals("PAGO ENTEL")) {
                        image.setAlignment(Image.ALIGN_RIGHT);
                        x = doc.getPageSize().getWidth() - (110 - servicio.getMarginLeft().floatValue());
                    }
                    if (servicio.getTesDetalleVc().trim().equals("PAGO TELECEL")) {
                        image.setAlignment(Image.ALIGN_LEFT);
                        x = 30;
                        y -= (servicio.getQrScale().floatValue() - (paragraph.getLeading() * 2));
                    }
                    if (servicio.getTesDetalleVc().trim().equals("PAGO NUEVATEL")) {
                        image.setAlignment(Image.ALIGN_LEFT);
                        x = doc.getPageSize().getWidth() - (120 - servicio.getMarginLeft().floatValue());
                        y -= (servicio.getQrScale().floatValue() - (paragraph.getLeading()));
                    }
                    if (servicio.getTesDetalleVc().trim().equals("PAGO TIGOSTAR (MULTIVISION)")) {
                        image.setAlignment(Image.ALIGN_LEFT);
                        x = doc.getPageSize().getWidth() - (120 - servicio.getMarginLeft().floatValue());
                        y -= (servicio.getQrScale().floatValue() - (paragraph.getLeading()));
                    }
                    image.setAbsolutePosition(x, y);
                }
                doc.add(image);
            }
            if (servicio.getDelimitador().equals(item.toString().trim())) {
                line = 0;
                page++;
                doc.newPage();
            } else {
                line += 1f;
                if (!(item.toString().contains("<QR>") || item.toString().contains("<QR_ENT_G>"))) {
                    doc.add(paragraph);
                }
            }
            temp += item.toString();
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ConsultaController.class.getName()).log(Level.INFO, null, ex);
    } catch (DocumentException | IOException ex) {
        Logger.getLogger(ConsultaController.class.getName()).log(Level.INFO, null, ex);
    }
    doc.close();
    file.close();
    return _file;
}