Example usage for com.lowagie.text.pdf PdfPCell setHorizontalAlignment

List of usage examples for com.lowagie.text.pdf PdfPCell setHorizontalAlignment

Introduction

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

Prototype

public void setHorizontalAlignment(int horizontalAlignment) 

Source Link

Document

Sets the horizontal alignment for the cell.

Usage

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

License:Apache License

/**
 * Creates the pdf footer./*from  w  w  w.ja  v a2s.c  o m*/
 *
 * @param pdfPTable
 *            the pdf P table
 * @param font
 *            the font
 * @throws DocumentException
 *             the document exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void createPdfFooter(final PdfPTable pdfPTable, Font font) throws DocumentException, IOException {
    final float width = pdfPTable.getWidthPercentage();
    final int headerWidth = (int) width;
    font = getFont();
    final PdfPCell footerCell = new PdfPCell(new Phrase(getFooter(), font));
    footerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    footerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    footerCell.setColspan(headerWidth);
    footerCell.setBorder(0);
    footerCell.setUseDescender(true);
    pdfPTable.addCell(footerCell);
    pdfPTable.setFooterRows(1);
}

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

License:Apache License

/**
 * Creates the pdf headers.//from  w w  w  .  ja v  a2s  .c  o m
 *
 * @param pdfPTable
 *            the pdf P table
 * @param font
 *            the font
 * @throws DocumentException
 *             the document exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void createPdfHeaders(final PdfPTable pdfPTable, Font font) throws DocumentException, IOException {
    font = getFont();
    final float width = pdfPTable.getWidthPercentage();
    final int headerWidth = (int) width;
    final PdfPCell headersCells = new PdfPCell(new Phrase(getHeader(), font));
    headersCells.setHorizontalAlignment(Element.ALIGN_CENTER);
    headersCells.setColspan(headerWidth);
    headersCells.setUseDescender(true);
    headersCells.setBorder(0);
    pdfPTable.addCell(headersCells);
    for (int i = 0; i < this.model.getColumnCount(); i++) {
        if (this.model.isVisible(i)) {
            final PdfPCell cell = new PdfPCell(new Phrase(Lables.get(this.model.getActualColumnName(i)), font));
            cell.setRotation(getRotationDegree());
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfPTable.addCell(cell);
        }
    }
    // Please explain
    pdfPTable.setHeaderRows(3);
    createPdfFooter(pdfPTable, font);

}

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

License:Apache License

/**
 * Load data table./*from w  ww .j  a  v  a 2s. c  o m*/
 *
 * @param pdfPTable
 *            the pdf P table
 * @param font
 *            the font
 * @throws DocumentException
 *             the document exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void loadDataTable(final PdfPTable pdfPTable, Font font) throws DocumentException, IOException {
    font = getFont();
    for (int row = 0; row < this.model.getRowCount(); row++) {
        for (int column = 0; column < this.model.getColumnCount(); column++) {
            if (this.model.isVisible(column)) {
                final PdfPCell cell = new PdfPCell(
                        new Phrase((String) this.model.getValueAt(row, column), font));
                cell.setNoWrap(true);
                cell.setRunDirection(getRunDirection());
                // TODO : make the alignment according to the colunm
                // datatype
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdfPTable.addCell(cell);
            }
        }
    }
}

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

/**
 *
 * @param document//from   w w w.  ja 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 addDetail(String[] data) throws DocumentException {

    for (String value : data) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(value), font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);/*from w  w  w .j a v a  2s.c  om*/
        detailTable.addCell(c1);
    }

}

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

License:Apache License

private void addSubtotal(String[] data) throws DocumentException {

    // add dashed line...
    for (String value : data) {

        String v = null;//from  ww  w .  j  av  a 2 s.  c o  m
        String rawValue = value(value);
        if (StringUtils.isNotEmpty(rawValue)) {
            v = line(rawValue, SINGLE_LINE);
        }

        PdfPCell c1 = new PdfPCell(new Phrase(v, font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

    // add totals....
    for (String value : data) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(value), font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

    // add blank line....
    for (String value : data) {
        PdfPCell c1 = new PdfPCell(new Phrase(" ", font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

}

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

License:Apache License

private void addTotal(String[] data) throws DocumentException {

    // add dashed line...
    for (String value : data) {

        String v = null;/*w  w  w  . ja v a  2s  .  c  om*/
        String rawValue = value(value);
        if (StringUtils.isNotEmpty(rawValue)) {
            v = line(rawValue, SINGLE_LINE);
        }

        PdfPCell c1 = new PdfPCell(new Phrase(v, font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

    // add totals....
    for (String value : data) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(value), font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

    // add blank line....

    for (String value : data) {

        String v = null;
        String rawValue = value(value);
        if (StringUtils.isNotEmpty(rawValue)) {
            v = line(rawValue, DOUBLE_LINE);
        }

        PdfPCell c1 = new PdfPCell(new Phrase(v, font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(value));
        setCellPadding(c1);
        detailTable.addCell(c1);
    }

}

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

License:Apache License

private void createColHeadings(String[] cols) throws DocumentException {

    createTable(cols.length);/* www.  java  2 s  . c  om*/
    for (String col : cols) {
        PdfPCell c1 = new PdfPCell(new Phrase(value(col), font));
        c1.setBorder(Rectangle.NO_BORDER);
        c1.setHorizontalAlignment(alignment(col));
        setCellPadding(c1);
        c1.setPaddingBottom(5);
        detailTable.addCell(c1);
    }

}

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.  j  a v a2s  .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  ww  w  .  j ava  2s.  c o  m*/
        c1.setHorizontalAlignment(alignment);
        setCellPadding(c1);
        table.addCell(c1);
        count++;
    }
    document.add(table);

}