Example usage for com.itextpdf.text.pdf PdfPCell PdfPCell

List of usage examples for com.itextpdf.text.pdf PdfPCell PdfPCell

Introduction

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

Prototype

public PdfPCell() 

Source Link

Document

Constructs an empty PdfPCell.

Usage

From source file:com.timesheet.export.PdfGenerator.java

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
        HttpServletRequest request, HttpServletResponse response) throws Exception {

    /* Add header*/
    Font fontHeader = FontFactory.getFont(FontFactory.TIMES_ROMAN, 22, Font.BOLD);
    Paragraph headerPara = new Paragraph("Time sheet report", fontHeader);
    headerPara.setSpacingAfter(20f);/*w  w  w.j a va2 s.co m*/
    document.add(headerPara);

    /*Add user info*/
    User userProfile = (User) model.get("userprofile");
    Font fontUserInfo = FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD);
    document.add(new Paragraph("Name : " + userProfile.getName(), fontUserInfo));
    document.add(new Paragraph("User no. : " + userProfile.getUserIdentifier(), fontUserInfo));
    document.add(new Paragraph("Department : " + userProfile.getDepartment(), fontUserInfo));
    document.add(new Paragraph("Email : " + userProfile.getEmail(), fontUserInfo));
    Paragraph spacing = new Paragraph("");
    spacing.setSpacingAfter(20f);
    document.add(spacing);

    PdfPTable table = new PdfPTable(5);
    table.setWidthPercentage(100.0f);
    table.setWidths(new float[] { 5.0f, 3.0f, 3.0f, 3.0f, 10.0f });
    table.setSpacingBefore(10);

    // define font for table header row
    Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);
    //font.setColor(BaseColor.WHITE);

    // define table header cell
    PdfPCell cell = new PdfPCell();
    //cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    cell.setPadding(5);
    cell.setBorderWidth(2.0f);
    cell.setBorder(Rectangle.BOTTOM);
    // get data model which is passed by the Spring container
    List<Booking> bookings = (List<Booking>) model.get("bookings");

    // write table header
    cell.setPhrase(new Phrase("Project", font));
    table.addCell(cell);
    // write table header
    cell.setPhrase(new Phrase("Option", font));
    table.addCell(cell);
    // write table header
    cell.setPhrase(new Phrase("Date", font));
    table.addCell(cell);
    // write table header
    cell.setPhrase(new Phrase("Duration", font));
    table.addCell(cell);
    // write table header
    cell.setPhrase(new Phrase("Description", font));
    table.addCell(cell);

    int sum = 0;

    Font fontData = FontFactory.getFont(FontFactory.TIMES_ROMAN, 11);

    for (Booking booking : bookings) {
        PdfPCell cell1 = new PdfPCell(new Phrase(
                booking.getProject().getProjectId() + "-" + booking.getProject().getName(), fontData));
        cell1.setBorder(Rectangle.BOTTOM);
        cell1.setPadding(5);
        table.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(new Phrase(booking.getBookingOption().getOptionLabel(), fontData));
        cell2.setBorder(Rectangle.BOTTOM);
        cell2.setPadding(5);
        table.addCell(cell2);
        PdfPCell cell3 = new PdfPCell(new Phrase(formatDate(booking.getBookingDate()), fontData));
        cell3.setBorder(Rectangle.BOTTOM);
        cell3.setPadding(5);
        table.addCell(cell3);
        int hh = booking.getDuration() / 60;
        int mm = booking.getDuration() % 60;
        PdfPCell cell4 = new PdfPCell(new Phrase(hh + ":" + mm, fontData));
        cell4.setBorder(Rectangle.BOTTOM);
        cell4.setPadding(5);
        table.addCell(cell4);
        PdfPCell cell5 = new PdfPCell(
                new Phrase(StringEscapeUtils.escapeHtml(booking.getDescription()), fontData));
        cell5.setBorder(Rectangle.BOTTOM);
        cell5.setPadding(5);
        table.addCell(cell5);

        sum += booking.getDuration();
    }

    document.add(table);

    int sumHH = sum / 60;
    int sumMM = sum % 60;

    document.add(new Paragraph("Sum : " + sumHH + ":" + sumMM, fontUserInfo));
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

From source file:comisionesafis.informes.FacturasComisionesAgentes.java

public boolean generar() {

    // Abrimos el fichero de comisiones
    Periodos periodos = new Periodos(pb.getFicheroComisiones());
    String sSQL = "";
    Statement stmt;//from ww w . j a v  a2  s  .  c om
    ResultSet rsComisiones;
    String sFactura1;
    String sFactura2;

    // Generamos la sentencia de Seleccin de Datos
    try {

        // Generamos el PDF
        Document documento = new Document(PageSize.A4, 80, 80, 50, 50);
        FileOutputStream salida = new FileOutputStream("FacturasComisionesAgentes.pdf");
        PdfWriter writer = PdfWriter.getInstance(documento, salida);
        writer.setInitialLeading(0);

        //  Obtenemos una instancia de nuestro manejador de eventos
        FacturasComisionesAgentesPie pie = new FacturasComisionesAgentesPie();
        //Asignamos el manejador de eventos al escritor.
        writer.setPageEvent(pie);

        // Abrimos el Documento
        documento.open();

        sSQL = "SELECT * ";
        sSQL += "  FROM ResumenComisiones";
        sSQL += " ORDER BY CodAgente";
        stmt = conexion.createStatement();
        rsComisiones = stmt.executeQuery(sSQL);

        while (rsComisiones.next()) {

            // Generamos los prrafos
            // Datos del agente
            Paragraph pAgente[] = new Paragraph[5];
            pAgente[0] = new Paragraph();
            pAgente[0].add(rsComisiones.getString("Nombre"));
            pAgente[0].setAlignment(Paragraph.ALIGN_LEFT);
            pAgente[1] = new Paragraph();
            pAgente[1].add(rsComisiones.getString("Direccion"));
            pAgente[1].setAlignment(Paragraph.ALIGN_LEFT);
            pAgente[2] = new Paragraph();
            pAgente[2].add(rsComisiones.getString("CodPostal") + "  " + rsComisiones.getString("Poblacion"));
            pAgente[2].setAlignment(Paragraph.ALIGN_LEFT);
            pAgente[3] = new Paragraph();
            pAgente[3].add(rsComisiones.getString("Provincia"));
            pAgente[3].setAlignment(Paragraph.ALIGN_LEFT);
            pAgente[4] = new Paragraph();
            pAgente[4].add(rsComisiones.getString("NIF"));
            pAgente[4].setAlignment(Paragraph.ALIGN_LEFT);

            // creating separators
            LineSeparator separador = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);

            // Datos fijos
            Paragraph pPelayo[] = new Paragraph[5];
            pPelayo[0] = new Paragraph();
            pPelayo[0].add("PELAYO VIDA");
            pPelayo[0].setAlignment(Paragraph.ALIGN_RIGHT);
            pPelayo[1] = new Paragraph();
            pPelayo[1].add("CL SANTA ENGRACIA 69");
            pPelayo[1].setAlignment(Paragraph.ALIGN_RIGHT);
            pPelayo[2] = new Paragraph();
            pPelayo[2].add("28010  MADRID");
            pPelayo[2].setAlignment(Paragraph.ALIGN_RIGHT);
            pPelayo[3] = new Paragraph();
            pPelayo[3].add("MADRID");
            pPelayo[3].setAlignment(Paragraph.ALIGN_RIGHT);
            pPelayo[4] = new Paragraph();
            pPelayo[4].add("06422");
            pPelayo[4].setAlignment(Paragraph.ALIGN_RIGHT);

            // Fecha
            Paragraph pFecha = new Paragraph();
            pFecha = new Paragraph("Madrid, a " + periodos.extraeFechaLarga());
            pFecha.setAlignment(Paragraph.ALIGN_RIGHT);

            // Lnea 1 de FACTURA
            Paragraph pFactura1 = new Paragraph();
            sFactura1 = "FACTURA: n factura ";
            sFactura1 += rsComisiones.getString("CodAgente") + " - ";
            sFactura1 += periodos.extraePeriodoMY("MM-YYYY");
            pFactura1 = new Paragraph(sFactura1);

            // Lnea 2 de FACTURA
            Paragraph pFactura2 = new Paragraph();
            sFactura2 = "Factura por la prestacin de servicios relativos a las operaciones ";
            sFactura2 += "de intermediacin de seguros realizadas para su entidad del mes de ";
            sFactura2 += periodos.extraePeriodoMY("MM YYYY");
            ;
            pFactura2 = new Paragraph(sFactura2);

            // Datos Econmicos
            float[] anchuras = { 5f, 2f };
            PdfPTable table = new PdfPTable(anchuras);
            table.getDefaultCell().setBorder(0);

            // Tipo de letra para la tabla
            Font font = new Font(Font.FontFamily.COURIER, 11, Font.NORMAL);

            PdfPCell celda = new PdfPCell();

            celda = new PdfPCell(new Phrase("Comisiones pagadas", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(celda);

            celda = new PdfPCell(
                    new Phrase(Double.toString(rsComisiones.getDouble("TotalComisiones")) + " ", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase("Otros conceptos", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase(" ", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase(
                    "Retencin (" + Double.toString(rsComisiones.getDouble("RetencionPorcentaje")) + "%)",
                    font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(celda);

            Double dblRetencion = rsComisiones.getDouble("TotalComisiones")
                    * (rsComisiones.getDouble("RetencionPorcentaje") / 100);
            celda = new PdfPCell(new Phrase(Numeros.formateaDosDecimales(dblRetencion) + " ", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase("Conceptos no sujetos", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase(" ", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celda);

            celda = new PdfPCell(new Phrase("Total a pagar", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(celda);

            Double dblTotalPagar = rsComisiones.getDouble("TotalComisiones") - dblRetencion;
            celda = new PdfPCell(new Phrase(Numeros.formateaDosDecimales(dblTotalPagar) + " ", font));
            celda.setBorder(Rectangle.NO_BORDER);
            celda.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celda);

            // Literal: Operacin exenta de IVA
            Paragraph pColetilla = new Paragraph();
            pColetilla.add("Operacin exenta de IVA");

            // Aadimos los prrafos al Documento
            for (int i = 0; i < 5; i++)
                documento.add(pAgente[i]);

            documento.add(new Paragraph(" "));
            documento.add(separador);

            for (int i = 0; i < 5; i++)
                documento.add(pPelayo[i]);

            documento.add(new Paragraph(" "));

            documento.add(pFecha);
            documento.add(new Paragraph(" "));

            documento.add(pFactura1);
            documento.add(separador);
            documento.add(pFactura2);

            // Agregamos la tabla al documento    
            documento.add(new Paragraph(" "));
            documento.add(table);

            documento.add(new Paragraph(" "));
            documento.add(new Paragraph(" "));
            documento.add(pColetilla);
            documento.newPage();
        }
        documento.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:comisionesafis.informes.LiquidacionComisiones.java

private void printCabecera2(Document documento, ResultSet rsAgentes) {

    String sSQL = "";
    Statement stmt;/*from   w ww .  java 2s.  c om*/
    ResultSet rsDatosAgente;

    try {

        // SELECT para extraer todos los cdigos de los agentes con Recibos
        sSQL = "SELECT * ";
        sSQL += "  FROM Agentes";
        sSQL += " WHERE CodAgente='" + rsAgentes.getString("Agente") + "'";
        stmt = conexion.createStatement();
        rsDatosAgente = stmt.executeQuery(sSQL);

        if (!rsDatosAgente.next()) {
            // Este error no debera darse
            System.out.println("Error: No hay datos para el agente " + rsAgentes.getString("Agente"));
            return;
        }

        // Generamos la cabecera
        //Font fuenteCabecera = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
        float[] anchuras = { 3f, 1f, 5f };
        PdfPTable table1 = new PdfPTable(anchuras);

        // Tipo de letra para la tabla
        Font font = new Font(Font.FontFamily.COURIER, 9, Font.NORMAL);

        PdfPCell celda = new PdfPCell();

        // Sucursal/Delegacin
        celda = new PdfPCell(new Phrase("SUCURSAL/DELEGACIN", font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        celda = new PdfPCell(new Phrase("02", font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        celda = new PdfPCell(new Phrase("PELAYO MONDIALE VIDA", font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        // No tenemos estos datos. Me pide Fernando que los quite
        //            // Inspeccin
        //            celda = new PdfPCell(new Phrase("INSPECCIN",font));
        //            celda.setBorder(Rectangle.NO_BORDER);
        //            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        //            table1.addCell(celda);
        //
        //            celda = new PdfPCell(new Phrase("000001",font));
        //            celda.setBorder(Rectangle.NO_BORDER);
        //            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        //            table1.addCell(celda);
        //
        //            celda = new PdfPCell(new Phrase("ACUERDO DE DISTRIBUCIN",font));
        //            celda.setBorder(Rectangle.NO_BORDER);
        //            celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        //            table1.addCell(celda);

        // Agente
        celda = new PdfPCell(new Phrase("AGENTE", font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        celda = new PdfPCell(new Phrase(rsDatosAgente.getString("CodAgente"), font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        celda = new PdfPCell(new Phrase(rsDatosAgente.getString("Nombre"), font));
        celda.setBorder(Rectangle.NO_BORDER);
        celda.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(celda);

        documento.add(table1);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:coreservlets.reportPDF.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w  w w.  j a  v a2 s  .  c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        response.setContentType("application/pdf");
        /** create Object Of document */
        Document document = new Document(PageSize.FLSE);
        /** set margin of page */
        document.setMargins(1.25F, 0.75F, 5F, 0.75F);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);
        document.open();
        /** Table For header address */
        PdfPTable headerTable = new PdfPTable(3);
        headerTable.setWidthPercentage(91);
        /** Add company Address*/
        /** First Cell  For address*/
        PdfPCell headerCell = new PdfPCell();
        headerCell.addElement(new Paragraph("My comapany"));
        /** add cell to headerTable*/
        headerTable.addCell(headerCell);
        /** Second cell For CompanyLogo */
        headerCell = new PdfPCell();
        /** get context object */
        ServletContext context = request.getSession().getServletContext();
        /** get Real  Path of image */
        String path = context.getRealPath(login.getPath());
        /** set image to cell*/
        Image img = Image.getInstance(path);
        headerCell.setImage(img);
        /** add cell to headerTable*/
        headerTable.addCell(headerCell);
        /** Third cell for lable of document **/
        headerCell = new PdfPCell();
        String allLedger = "ALL LEDGER";
        headerCell.addElement(new Paragraph(
                allLedger + "\n" + "From Date:" + session.getAttribute("firstDate") + "\nTo Date:"
                        + session.getAttribute("lastDate"),
                new Font(Font.TIMES_ROMAN, 10f, Font.BOLD, Color.black)));

        /** add cell to headerTable*/

        headerTable.addCell(headerCell);
        /** add table to document */

        document.add(headerTable);
        document.close();
        /** Don't know why??*/
        response.setContentLength(baos.size());
        /** comment it if u don't want to download pdf*/
        //response.addHeader("Content-Disposition", "attachment; filename=\"ledgers.pdf\"");

        /** get out put Stream*/
        out = response.getOutputStream();
        /** write to--- Don't know*/
        baos.writeTo(out);
        /** Clear OutputStream*/
        out.flush();
        out.close();

    }
}