Example usage for com.itextpdf.text Phrase getInstance

List of usage examples for com.itextpdf.text Phrase getInstance

Introduction

In this page you can find the example usage for com.itextpdf.text Phrase getInstance.

Prototype

public static final Phrase getInstance(final String string) 

Source Link

Document

Gets a special kind of Phrase that changes some characters into corresponding symbols.

Usage

From source file:com.automaster.autoview.server.servlet.PdfServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*from w w w.ja va  2 s .c om*/
        this.dataAtual = new Date(System.currentTimeMillis());
        this.cnpjUn = null;

        String tempoDecorrido = " 0";
        int codVeiculo = Integer.parseInt(request.getParameter("cod"));
        String placa = request.getParameter("placa");
        Timestamp dataInicio = new Timestamp(Long.parseLong(request.getParameter("dataInicio")));
        Timestamp dataFim = new Timestamp(Long.parseLong(request.getParameter("dataFim")));

        TimeZone timeZonePadrao = TimeZone.getTimeZone(ZoneId.of("-3"));
        //System.out.println("Time zone : " + timeZonePadrao);

        ZzzPosPlacaVeiculoDAO zzzPosPlacaVeiculoDAO = new ZzzPosPlacaVeiculoDAO();
        VeiculoDAO veiculoDAO = new VeiculoDAO();
        TreeMap<String, String> veiculo = veiculoDAO.buscarVeiculoPorCodigo(codVeiculo);
        placa = veiculo.get("placa");
        //System.out.println("PLACA: "+veiculo.get("placa"));
        ArrayList<TreeMap<String, String>> posicoes = zzzPosPlacaVeiculoDAO
                .buscarPosicoesPorIntervaloData(placa, dataInicio, dataFim);
        int codCliente = Integer.parseInt(veiculo.get("clienteCodCliente"));
        //System.out.println("COD CLIENTE: "+codCliente);
        ClienteDAO clienteDAO = null;
        clienteDAO = new ClienteDAO();
        TreeMap<String, String> infoClienteUnidade = clienteDAO.buscarPorCodClienteSimplificado(codCliente);
        // Get the text that will be added to the PDF
        // step 1
        Document document = new Document();
        document.addHeader("Adriano", "AutoMaster");
        document.addCreator("Adriano Vale");
        document.addAuthor("Adriano Vale");
        document.addCreationDate();
        document.setPageSize(PageSize.A4.rotate());
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        TableHeader event = new TableHeader();
        writer.setPageEvent(event);
        // step 3
        document.open();
        // step 4
        //getServletContext().getRealPath("/")
        String url = getServletContext().getRealPath("/");
        //"D:\\Users\\Adriano\\Documents\\NetBeansProjects\\JRGWT\\web\\imagens\\logo.jpg"
        Image logo = Image.getInstance(url + "/imagens/logo.jpg");
        logo.setAlignment(Element.ALIGN_CENTER);

        Paragraph titulo = new Paragraph("Relatrio de Posies",
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 20, Font.BOLD));
        titulo.setAlignment(Element.ALIGN_CENTER);

        Paragraph tituloPlaca = new Paragraph("Veculo: " + placa,
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 16, Font.BOLD));
        tituloPlaca.setAlignment(Element.ALIGN_CENTER);

        SimpleDateFormat dataFormatadaCabecalho = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        dataFormatadaCabecalho.setTimeZone(timeZonePadrao);

        Date dataHoraInicio0 = new Date(Long.parseLong(request.getParameter("dataInicio")));
        Date dataHoraFim0 = new Date(Long.parseLong(request.getParameter("dataFim")));
        Paragraph periodo = new Paragraph(
                "Perodo: De: " + dataFormatadaCabecalho.format(dataHoraInicio0) + " at: "
                        + dataFormatadaCabecalho.format(dataHoraFim0),
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 16, Font.BOLD));
        periodo.setSpacingAfter(10.0f);
        periodo.setAlignment(Element.ALIGN_CENTER);
        //PdfPTable tabela = new PdfPTable(new float[]{0.11f, 0.095f, 0.06f, 0.065f, 0.085f, 0.06f, 0.04f, 0.065f, 0.055f, 0.06f, 0.24f, 0.065f});
        PdfPTable tabela = new PdfPTable(new float[] { 0.07f, 0.045f, 0.045f, 0.27f });
        tabela.setWidthPercentage(98.0f);
        tabela.setHorizontalAlignment(Element.ALIGN_CENTER);
        PdfPCell tituloData = new PdfPCell(Phrase.getInstance("Data e Hora"));
        tituloData.setHorizontalAlignment(Element.ALIGN_CENTER);
        tabela.addCell(tituloData);
        //tabela.addCell("Data e hora");
        PdfPCell tituloVel = new PdfPCell(Phrase.getInstance("Velocidade"));
        tituloVel.setHorizontalAlignment(Element.ALIGN_CENTER);
        tabela.addCell(tituloVel);
        PdfPCell tituloIgn = new PdfPCell(Phrase.getInstance("Ignio"));
        tituloIgn.setHorizontalAlignment(Element.ALIGN_CENTER);
        tabela.addCell(tituloIgn);
        //tabela.addCell("Latitude");
        //tabela.addCell("Longitude");
        //tabela.addCell("Satlite");
        //tabela.addCell("GPS");
        //tabela.addCell("Entrada");
        //tabela.addCell("Sada");
        //tabela.addCell("Evento");
        PdfPCell tituloEnd = new PdfPCell(Phrase.getInstance("Endereo"));
        tituloEnd.setHorizontalAlignment(Element.ALIGN_CENTER);
        tabela.addCell(tituloEnd);
        //tabela.addCell("Direo");
        double latAnt = 0;
        double lonAnt = 0;
        double latAtual = 0;
        double lonAtual = 0;
        double distanciaTotal = 0;
        double distancia = 0;
        event.setHeader("AutoMaster");

        for (int i = 0; i < posicoes.size(); i++) {
            //for (int col = 0; col < posicoes.get(i).size(); col++) {
            if (i == 0) {
                distancia = 0;
                //System.out.println("linha 00 - PDF");
            } else {
                //System.out.println("linha 01 - PDF");
                latAnt = Double.parseDouble(posicoes.get(i - 1).get("lat"));
                lonAnt = Double.parseDouble(posicoes.get(i - 1).get("lon"));
                latAtual = Double.parseDouble(posicoes.get(i).get("lat"));
                lonAtual = Double.parseDouble(posicoes.get(i).get("lon"));
                //System.out.println("linha 02 - PDF");
                if (latAnt == latAtual && lonAnt == lonAtual) {
                    distancia = 0;
                } else {
                    distancia = caculaDistanciaEntreDoisPontos(latAnt, lonAnt, latAtual, lonAtual);
                    //System.out.println("linha 03 - PDF");
                }

            }
            distanciaTotal = distanciaTotal + distancia;
            //System.out.println("linha 04 - PDF");
            //TimeZone.setDefault(timeZoneMundial);
            Date dataHora0 = new Date(Long.parseLong(posicoes.get(i).get("dataHora")));
            //System.out.println("dataHora0 : "+dataHora0.toString());
            //TimeZone.setDefault(timeZoneCliente);
            //Date dataHora = new Date(dataHora0.getTime());
            SimpleDateFormat dataFormatada = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            dataFormatada.setTimeZone(timeZonePadrao);
            //System.out.println("dataFormatada : "+dataFormatada);
            PdfPCell celData = new PdfPCell(Phrase.getInstance(dataFormatada.format(dataHora0)));
            celData.setHorizontalAlignment(Element.ALIGN_CENTER);
            tabela.addCell(celData);
            //tabela.addCell(dataFormatada);
            PdfPCell celVel = new PdfPCell(Phrase.getInstance(posicoes.get(i).get("vel")));
            celVel.setHorizontalAlignment(Element.ALIGN_CENTER);
            tabela.addCell(celVel);
            PdfPCell celIgn = new PdfPCell(Phrase
                    .getInstance(posicoes.get(i).get("ign").equalsIgnoreCase("True") ? "Ligada" : "Desligada"));
            celIgn.setHorizontalAlignment(Element.ALIGN_CENTER);
            tabela.addCell(celIgn);
            //tabela.addCell(posicoes.get(i).get("lat"));
            //tabela.addCell(posicoes.get(i).get("lon"));
            //tabela.addCell(posicoes.get(i).get("sat"));
            //tabela.addCell(posicoes.get(i).get("gps"));
            //tabela.addCell(posicoes.get(i).get("entrada"));
            //tabela.addCell(posicoes.get(i).get("saida"));
            //tabela.addCell(posicoes.get(i).get("evento"));
            PdfPCell celEnd = new PdfPCell(
                    Phrase.getInstance(posicoes.get(i).get("endereco") == null ? "Sem endereo"
                            : posicoes.get(i).get("endereco")));
            celEnd.setHorizontalAlignment(Element.ALIGN_CENTER);
            tabela.addCell(celEnd);
            //tabela.addCell(posicoes.get(i).get("direcao"));
            //}
        }
        //System.out.println("linha 05 - PDF");
        tempoDecorrido = calculaDatas(Long.parseLong(posicoes.get(0).get("dataHora")),
                Long.parseLong(posicoes.get(posicoes.size() - 1).get("dataHora")));
        //System.out.println("linha 06 - PDF");
        int index = 0;
        String kms = "0";
        String m = "";
        double metros = 0;
        //System.out.println("linha 07 - PDF");
        if (distanciaTotal > 0) {
            //System.out.println("linha 08 - PDF");
            BigDecimal decimalFormatado = new BigDecimal(distanciaTotal).setScale(2, RoundingMode.HALF_EVEN);
            //System.out.println("linha 09 - PDF");
            index = String.valueOf(decimalFormatado).indexOf(".");
            kms = String.valueOf(decimalFormatado).substring(0, index);
            m = "0" + (String.valueOf(decimalFormatado).substring(index));
            metros = Double.parseDouble(m) * 1000;
            //System.out.println("linha 10 - PDF");
        }
        /*document.add(new Paragraph(String.format(
         "You have submitted the following text using the %s method:",
         request.getMethod())));
         document.add(new Paragraph(text));*/
        //System.out.println("linha 11 - PDF");
        Paragraph kilometragem = new Paragraph(
                "Percorridos: " + kms + " KM e " + String.valueOf(metros) + " metros. Tempo: " + tempoDecorrido,
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 16, Font.BOLD));
        //System.out.println("linha 12 - PDF");
        periodo.setSpacingAfter(10.0f);
        periodo.setAlignment(Element.ALIGN_CENTER);
        // step 5
        document.add(logo);
        document.add(titulo);
        document.add(tituloPlaca);
        document.add(periodo);
        document.add(tabela);
        document.add(kilometragem);
        String dia = new SimpleDateFormat("dd").format(dataAtual);
        String ano = new SimpleDateFormat("yyyy").format(dataAtual);
        int mes = dataAtual.getMonth();
        String mesEscrito = null;
        switch (mes) {
        case 0:
            mesEscrito = "janeiro";
            break;
        case 1:
            mesEscrito = "fevereiro";
            break;
        case 2:
            mesEscrito = "maro";
            break;
        case 3:
            mesEscrito = "abril";
            break;
        case 4:
            mesEscrito = "maio";
            break;
        case 5:
            mesEscrito = "junho";
            break;
        case 6:
            mesEscrito = "julho";
            break;
        case 7:
            mesEscrito = "agosto";
            break;
        case 8:
            mesEscrito = "setembro";
            break;
        case 9:
            mesEscrito = "outubro";
            break;
        case 10:
            mesEscrito = "novembro";
            break;
        case 11:
            mesEscrito = "dezembro";
            break;
        }
        String textRodape = infoClienteUnidade.get("cidadeUnidade") + " , "
                + infoClienteUnidade.get("estadoUnidade") + "    " + dia + "  de  " + mesEscrito + "  de  "
                + ano + ".";
        Paragraph localData = new Paragraph(textRodape,
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 12, Font.PLAIN));
        localData.setAlignment(Element.ALIGN_RIGHT);
        localData.setSpacingBefore(30.0f);
        localData.setSpacingAfter(10.0f);
        document.add(localData);
        Image assinatura = Image.getInstance(url + "/imagens/assinatura.png");
        assinatura.setAlignment(Element.ALIGN_CENTER);
        assinatura.scaleAbsolute(185, 91);
        document.add(assinatura);
        Paragraph infoEmpresa1 = new Paragraph("AUTO MASTER LTDA",
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 12, Font.BOLD));
        infoEmpresa1.setAlignment(Element.ALIGN_CENTER);
        infoEmpresa1.setSpacingAfter(1f);
        document.add(infoEmpresa1);
        Paragraph infoEmpresa2 = new Paragraph("___________________________",
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 12, Font.BOLD));
        infoEmpresa2.setAlignment(Element.ALIGN_CENTER);
        document.add(infoEmpresa2);
        String cnpjEmpresa = formataCNPJ(infoClienteUnidade.get("cnpjUnidade"));
        Paragraph infoEmpresa3 = new Paragraph(cnpjEmpresa,
                new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA, 12, Font.BOLD));
        infoEmpresa3.setAlignment(Element.ALIGN_CENTER);
        document.add(infoEmpresa3);
        document.close();
        // setting some response headers
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        // setting the content type
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=Historico-" + placa + ".pdf");
        // the contentlength
        response.setContentLength(baos.size());
        // write ByteArrayOutputStream to the ServletOutputStream
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close();
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
    //        response.setContentType("text/html;charset=UTF-8");
    //        try (PrintWriter out = response.getWriter()) {
    //            /* TODO output your page here. You may use following sample code. */
    //            out.println("<!DOCTYPE html>");
    //            out.println("<html>");
    //            out.println("<head>");
    //            out.println("<title>Servlet PdfServlet</title>");            
    //            out.println("</head>");
    //            out.println("<body>");
    //            out.println("<h1>Servlet PdfServlet at " + request.getContextPath() + "</h1>");
    //            out.println("</body>");
    //            out.println("</html>");
    //        }
}

From source file:com.chaschev.itext.AbstractParagraphBuilder.java

License:Apache License

public static Phrase getInstance(String string) {
    return Phrase.getInstance(string);
}