Example usage for com.lowagie.text.pdf PdfPTable PdfPTable

List of usage examples for com.lowagie.text.pdf PdfPTable PdfPTable

Introduction

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

Prototype

public PdfPTable(PdfPTable table) 

Source Link

Document

Constructs a copy of a PdfPTable.

Usage

From source file:com.huateng.system.util.PdfUtil.java

License:Open Source License

public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map)
        throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////from w  w w . ja  v  a 2  s.  co  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font10 = new Font(bfChinese, 10, Font.NORMAL);
    Font font10Bold = new Font(bfChinese, 10, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.55f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(3);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(3);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            opt += " ";
            opt += o[1].toString();
            opt += "         ";
        }
        c.addElement(new Paragraph(opt.trim(), font10));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");
}

From source file:com.ideaspymes.proyecttemplate.stock.web.ProductoConsultaBean.java

public String createPdf() throws IOException, DocumentException {

    EtiquetaConf conf = etiquetaConfDAO.getEtiquetaConfDefault();

    if (hayParaImprimir() && conf != null) {

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/x-pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"etiquetas.pdf\"");

        float ancho = Utilities.millimetersToPoints(conf.getAnchoHoja().floatValue());
        System.out.println("Ancho: " + ancho);
        float largo = Utilities.millimetersToPoints(conf.getLargoHoja().floatValue());
        System.out.println("Alto: " + largo);

        // step 1
        Document document = new Document(new Rectangle(ancho, largo));
        // step 2

        document.setMargins(0f, 0f, 0f, 0f);

        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
        // step 3
        document.open();//from   ww w.  ja  v  a 2  s  .  c o  m

        // step 4
        PdfContentByte cb = writer.getDirectContent();

        for (Producto p : getLista()) {
            if (p.getCantidadEtiquetas() > 0) {

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(96);

                Font fontbold = FontFactory.getFont("Times-Roman", 8, Font.NORMAL);

                Chunk productTitle = new Chunk("HC", fontbold);

                Paragraph pTitile = new Paragraph(productTitle);
                pTitile.setAlignment(Element.ALIGN_LEFT);
                pTitile.setLeading(6, 0);

                PdfPCell cellTitle = new PdfPCell();
                cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
                cellTitle.setVerticalAlignment(Element.ALIGN_TOP);
                cellTitle.setBorder(Rectangle.NO_BORDER);
                cellTitle.addElement(pTitile);

                Font font2 = FontFactory.getFont("Times-Roman", conf.getTamDescripcion().floatValue(),
                        Font.NORMAL);

                Chunk productName = new Chunk(p.getNombre(), font2);

                Paragraph pName = new Paragraph(productName);
                pName.setAlignment(Element.ALIGN_CENTER);
                //pTitile.setLeading(0, 1);

                cellTitle.addElement(pName);

                table.addCell(cellTitle);

                Barcode39 code39 = new Barcode39();
                code39.setCode(p.getCodigo());
                //code39.setCodeType(Barcode.EAN13);
                code39.setBarHeight(conf.getAltoCodBarra().floatValue());
                //codeEan.setX(0.7f);
                code39.setSize(conf.getTamDescripcion().floatValue());
                //code39.setAltText("HC - " + p.getNombre());

                PdfPCell cellBarcode = new PdfPCell();
                cellBarcode.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellBarcode.setBorder(Rectangle.NO_BORDER);
                cellBarcode.addElement(code39.createImageWithBarcode(cb, null, Color.BLACK));

                table.addCell(cellBarcode);

                for (int i = 0; i < p.getCantidadEtiquetas(); i++) {
                    document.add(table);
                    document.newPage();
                }
            }
        }

        // step 5
        document.close();

        response.getOutputStream().flush();
        response.getOutputStream().close();
        FacesContext.getCurrentInstance().responseComplete();
    } else {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "No hay nada que imprimir", ""));
    }
    return null;
}

From source file:com.itext.test.FirstPdf.java

private static void createTable(Section subCatPart) throws BadElementException {
    //PdfPTable/*ww  w.  j  a  va2 s . c  o m*/
    PdfPTable table = new PdfPTable(3);
    //PdfPTable
    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);
    //PDFPCell
    PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    //PdfPCellHorizontalAlignment?
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    //celltable
    table.addCell(c1);
    //PDFPCell
    c1 = new PdfPCell(new Phrase("Table Header 2"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Table Header 3"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(1);

    table.addCell("1.0");
    table.addCell("1.1");
    table.addCell("1.2");
    table.addCell("2.1");
    table.addCell("2.2");
    table.addCell("2.3");

    subCatPart.add(table);

}

From source file:com.itn.excelDao.PdfView.java

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

    //Users user = (Users) model.get("user");
    List<Users> userlist = (List<Users>) model.get("allUsers");

    PdfPTable table = new PdfPTable(4);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.getDefaultCell().setBackgroundColor(Color.lightGray);

    table.addCell("ID");
    table.addCell("First Name");
    table.addCell("Last Name");
    table.addCell("Email");

    for (Users users : userlist) {
        table.addCell(users.getId().toString());
        table.addCell(users.getFirstName());
        table.addCell(users.getLastName());
        table.addCell(users.getEmail());
    }/*  w  w w. j a  v a2s .co  m*/

    document.add(table);

}

From source file:com.jk.framework.desktop.swing.dao.TableModelPdfBuilder.java

License:Apache License

/**
 * Creates the pdf table./*  w w  w . j  a  v  a 2s  .com*/
 *
 * @return the pdf P table
 * @throws DocumentException
 *             the document exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public PdfPTable createPdfTable() throws DocumentException, IOException {
    final Font font = getFont();
    int columnCount = 0;
    for (int i = 0; i < this.model.getColumnCount(); i++) {
        if (this.model.isVisible(i)) {
            columnCount++;
        }
    }
    final PdfPTable pdfPTable = new PdfPTable(columnCount);
    pdfPTable.setRunDirection(getRunDirection());
    pdfPTable.getDefaultCell().setRunDirection(getRunDirection());
    pdfPTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    pdfPTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    pdfPTable.getDefaultCell().setNoWrap(true);

    createPdfHeaders(pdfPTable, font);
    loadDataTable(pdfPTable, font);
    return pdfPTable;

}

From source file:com.kahlon.guard.controller.DocumentManager.java

/**
 *
 * @param document/*w  w w  .j a v  a 2 s  . c  o  m*/
 */
public void preProcessPDF(Object document) {
    try {

        BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);

        Document pdf = (Document) document;
        pdf.setPageSize(PageSize.A4);
        pdf.setMargins(5f, 5f, 10f, 5f);

        // headers and footers must be added before the document is opened
        HeaderFooter footer = new HeaderFooter(new Phrase("page: ", new Font(bf_courier)), true);
        footer.setBorder(Rectangle.NO_BORDER);
        footer.setAlignment(Element.ALIGN_CENTER);
        pdf.setFooter(footer);

        //            HeaderFooter header = new HeaderFooter(
        //                        new Phrase("This is a header without a page number", new Font(bf_courier)), false);
        //            header.setAlignment(Element.ALIGN_CENTER);
        //            pdf.setHeader(header);

        pdf.open();

        String logoPath = "/resources/image/logo.png";
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String logo = servletContext.getRealPath(logoPath);

        pdf.add(Image.getInstance(logo));

        Person person = context.getSelectedPerson().getDisplayPerson();

        String name = FacesMessageUtil.getMessage("person.name") + " : " + person.getName();
        String age = FacesMessageUtil.getMessage("person.age") + " : " + Integer.toString(person.getAge());
        String gender = FacesMessageUtil.getMessage("person.gender") + " : "
                + person.getGender().getDescription();
        String race = FacesMessageUtil.getMessage("person.ethnicity") + " : "
                + person.getEthnicity().getDescription();

        Person rootPerson = person.getRootPerson();
        PersonImage imgp = imageService.getLastestPersonImage(rootPerson.getId());
        Image imgb = Image.getInstance(imgp.getContent());
        imgb.scaleToFit(100, 120);

        PdfPTable headerTable = new PdfPTable(2);

        PdfPTable personTable = new PdfPTable(1);
        personTable.setWidthPercentage(100);
        PdfPCell cell;

        cell = new PdfPCell(new Phrase(name));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(age));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(gender));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(race));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        BarcodePDF417 pdf417 = new BarcodePDF417();
        pdf417.setText(Integer.toString(person.getId()));
        Image img = pdf417.getImage();
        img.scalePercent(150, 60 * pdf417.getYHeight());
        cell = new PdfPCell(img);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPaddingTop(15);
        personTable.addCell(cell);

        cell.addElement(personTable);
        headerTable.addCell(cell);

        cell = new PdfPCell(imgb);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(Rectangle.NO_BORDER);
        headerTable.addCell(cell);

        headerTable.setSpacingAfter(15);
        pdf.add(headerTable);

        LineSeparator lineSeparator = new LineSeparator();
        lineSeparator.setPercentage(82);
        pdf.add(lineSeparator);
        Paragraph space = new Paragraph();
        space.add("");
        space.setSpacingAfter(15);
        pdf.add(space);

    } catch (IOException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (BadElementException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (DocumentException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (Exception e) {
        logger.log(Level.INFO, e.getMessage());
    }

}

From source file:com.khs.report.writer.ReportPDFWriter.java

License:Apache License

private void createFootings(String footing) throws DocumentException {
    String[] cols = footing.split(ReportProcessor.ALIGN_DELIMITER, 3);
    float[] colSizes = new float[] { 25, 50, 25 };

    footerTable = new PdfPTable(colSizes);
    footerTable.setWidthPercentage(getHeadingWidth());

    int count = 0;
    for (String c : cols) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(c), font));

        c1.setBorder(Rectangle.NO_BORDER);
        int alignment = Element.ALIGN_CENTER;
        if (count == 0) {
            alignment = Element.ALIGN_LEFT;
        } else if (count == 2) {
            alignment = Element.ALIGN_RIGHT;
        }//from w w  w . java  2  s . c o m
        c1.setHorizontalAlignment(alignment);
        setCellPadding(c1);
        footerTable.addCell(c1);
        count++;
    }
}

From source file:com.khs.report.writer.ReportPDFWriter.java

License:Apache License

private void createHeading(String heading) throws DocumentException {

    String[] cols = heading.split(ReportProcessor.ALIGN_DELIMITER, 3);
    PdfPTable table = new PdfPTable(cols.length);
    table.setWidthPercentage(getHeadingWidth());

    int count = 0;
    for (String c : cols) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(c), font));

        c1.setBorder(Rectangle.NO_BORDER);
        int alignment = Element.ALIGN_CENTER;
        if (count == 0) {
            alignment = Element.ALIGN_LEFT;
        } else if (count == 2) {
            alignment = Element.ALIGN_RIGHT;
        }//from w  w w .  jav a 2s  . com
        c1.setHorizontalAlignment(alignment);
        setCellPadding(c1);
        table.addCell(c1);
        count++;
    }
    document.add(table);

}

From source file:com.khs.report.writer.ReportPDFWriter.java

License:Apache License

private void createTable(int cols) throws DocumentException {
    if (colWidths != null) {
        detailTable = createTableWithAbsoluteWidths();
    } else {/*  w  w w .j  a v  a  2s .  co m*/
        detailTable = new PdfPTable(cols);
        detailTable.setWidthPercentage(getWidth());
    }
    detailTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);

}

From source file:com.khs.report.writer.ReportPDFWriter.java

License:Apache License

private PdfPTable createTableWithAbsoluteWidths() throws DocumentException {
    PdfPTable table = new PdfPTable(colWidths.length);
    calculateRelativeSizes(table);//from   ww w.  j a  v  a  2s.c o m
    return table;
}