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:gov.medicaid.services.util.PDFHelper.java

License:Apache License

/**
 * Adds a centered cell to the given table.
 *
 * @param table the table to add the cell to
 * @param value the value text//www. ja v  a2 s .co m
 */
public static void addCenterCell(PdfPTable table, String value) {
    PdfPCell val = new PdfPCell(
            new Phrase(Util.defaultString(value), FontFactory.getFont(FontFactory.HELVETICA, 7)));
    val.setBorder(Rectangle.NO_BORDER);
    val.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
    val.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
    table.addCell(val);
}

From source file:it.eng.spagobi.engines.documentcomposition.exporterUtils.PdfCreator.java

License:Mozilla Public License

public FileOutputStream createPdfFile(FileOutputStream fileOutputStream,
        Map<String, DocumentContainer> documentsMap, boolean defaultStyle)
        throws MalformedURLException, IOException, DocumentException {

    logger.debug("IN");

    Document document = new Document(PageSize.A4.rotate());
    Rectangle rect = document.getPageSize();
    docWidth = rect.getWidth();/*from   w  w  w . j a  va 2  s. co  m*/
    docHeight = rect.getHeight();

    logger.debug("document size width: " + docWidth + " height: " + docHeight);

    //PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("C:/comp/SpagoBIProva.pdf"));
    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
    document.open();

    int documentsNumber = documentsMap.keySet().size();
    int columnnsNumber = 2;

    if (defaultStyle == true) {
        logger.debug("use default style");
        int cellsCounter = 0;

        PdfPTable table = new PdfPTable(columnnsNumber);
        table.setWidthPercentage(100);

        for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
            String label = (String) iterator.next();
            DocumentContainer docContainer = documentsMap.get(label);
            byte[] content = docContainer.getContent();
            if (content != null) {
                Image img = null;
                try {
                    img = Image.getInstance(content);
                    table.addCell(img);
                } catch (Exception e) {
                    logger.debug("Trying to evaluate response as a PDF file... ");
                    table.addCell("");
                    //                  try {
                    //                     PdfReader reader = new PdfReader(content);
                    //                     PdfImportedPage page = writer.getImportedPage(reader, 1);
                    //                     writer.addPage(page);
                    //                     table.addCell("");
                    //                  } catch (Exception x) {
                    //                     logger.error("Error in inserting image for document " + label, e);
                    //                     logger.error("Error in inserting pdf file for document " + label, x);
                    //                     table.addCell("");
                    //                  }
                }
            }
            cellsCounter++;
        }

        // if cell counter is not pair make it pair
        if (cellsCounter % 2 != 0) {
            table.addCell("");
        }
        document.add(table);

    } else { // ************* NO DEFAULT STYLE *****************
        logger.debug("No default style");

        // I want to calculate total height of scaled heights!!
        //int totalScaledHeight=calculateTotaleScaledHeights(documentsMap, defaultStyle);

        // run on all documents
        for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
            String label = (String) iterator.next();
            logger.debug("document with label " + label);

            DocumentContainer docContainer = documentsMap.get(label);
            MetadataStyle style = docContainer.getStyle();

            // one table for each image, set at absolute position
            PdfPTable table = new PdfPTable(1);

            // width and height specified for the container by style attribute
            int widthStyle = style.getWidth();
            int heightStyle = style.getHeight();
            logger.debug("style for document width: " + widthStyle + " height: " + heightStyle);

            // width and height for the table scaled to the document size
            int tableWidth = calculatePxSize(docWidth, widthStyle, videoWidth);
            int tableHeight = calculatePxSize(docHeight, heightStyle, videoHeight);

            logger.debug("table for document width: " + tableWidth + " height: " + tableHeight);

            // x and y position as specified for the container by the style attribute
            int yStyle = style.getY();
            int xStyle = style.getX();
            // width and height scaled to the document size
            int xPos = (calculatePxPos(docWidth, xStyle, videoWidth));
            int yPos = (int) docHeight - (calculatePxPos(docHeight, yStyle, videoHeight));
            logger.debug("Table position at x: " + xPos + " y: " + yPos);

            // get the image
            byte[] content = docContainer.getContent();
            if (content != null) {
                Image img = null;
                try {
                    img = Image.getInstance(content);
                } catch (Exception e) {
                    logger.debug("Trying to evaluate response as a PDF file... ");
                    try {
                        PdfReader reader = new PdfReader(content);
                        PdfContentByte cb = writer.getDirectContent();
                        PdfImportedPage page = writer.getImportedPage(reader, 1);
                        float[] tm = getTransformationMatrix(page, xPos, yPos, tableWidth, tableHeight);
                        cb.addTemplate(page, tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
                    } catch (Exception x) {
                        logger.error("Error in inserting image for document " + label, e);
                        logger.error("Error in inserting pdf file for document " + label, x);
                    }
                    continue;
                }

                //if it is a REPORT and has more than one page, too large, you have to resize the image, but how to understand it?
                // if image size is more than double of the container size cut the first part,otherwise scale it
                if (docContainer.getDocumentType().equals("REPORT")) {
                    boolean cutImageWIdth = isToCutWidth(img, tableWidth);
                    boolean cutImageHeight = isToCutHeight(img, tableWidth);

                    if (cutImageWIdth == true || cutImageHeight == true) {
                        logger.debug(
                                "Report will be cut to width " + tableWidth + " and height " + tableHeight);
                        try {
                            img = cutImage(content, cutImageHeight, cutImageWIdth, tableHeight, tableWidth,
                                    (int) img.getWidth(), (int) img.getHeight());
                        } catch (Exception e) {
                            logger.error(
                                    "Error in image cut, cutt will be ignored and image will be drawn anyway ",
                                    e);
                        }
                    }
                }

                // this is percentage to resize
                // The image must be size within the cell               
                int percToResize = percentageToResize((int) img.getWidth(), (int) img.getHeight(), tableWidth,
                        tableHeight);
                logger.debug("image will be scaled of percentage " + percToResize);
                img.scalePercent(percToResize);

                PdfPCell cell = new PdfPCell(img);
                cell.setNoWrap(true);
                cell.setFixedHeight(tableHeight);
                cell.setBorderWidth(0);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                //table.setWidthPercentage(tableWidthPerc);
                table.setTotalWidth(tableWidth);
                table.setLockedWidth(true);
            } else {
                // TODO: setALT!
            }
            logger.debug("Add table");
            table.writeSelectedRows(0, -1, xPos, yPos, writer.getDirectContent());
            logger.debug("Document added");
        }

    }
    document.close();
    logger.debug("OUT");
    return fileOutputStream;
}

From source file:it.govpay.web.console.pagamenti.gde.exporter.PdfExporter.java

License:Open Source License

private static void createInfospcoopTable(Section subCatPart, Infospcoop infospcoop)
        throws BadElementException {

    PdfPTable table = new PdfPTable(2);

    PdfPCell c1 = new PdfPCell(new Phrase("Infospcoop"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setColspan(2);//w ww. ja  va2s . c  o  m
    table.addCell(c1);
    table.setHeaderRows(1);

    //riga 1
    table.addCell(new Phrase("IdEgov"));
    table.addCell(new Phrase(infospcoop.getIdEgov()));
    // riga 2
    table.addCell(new Phrase("Soggetto Erogatore"));
    table.addCell(new Phrase(infospcoop.getTipoSoggettoErogatore() + "/" + infospcoop.getSoggettoErogatore()));
    // riga 3
    table.addCell(new Phrase("Soggetto Fruitore"));
    table.addCell(new Phrase(infospcoop.getTipoSoggettoFruitore() + "/" + infospcoop.getSoggettoFruitore()));
    // riga 4
    table.addCell(new Phrase("Servizio"));
    table.addCell(new Phrase(infospcoop.getTipoServizio() + "/" + infospcoop.getServizio()));
    // riga 5
    table.addCell(new Phrase("Azione"));
    table.addCell(new Phrase(infospcoop.getAzione()));

    subCatPart.add(table);

}

From source file:it.govpay.web.console.pagamenti.gde.exporter.PdfExporter.java

License:Open Source License

private static void createEventoTable(Section subCatPart, Evento evento) throws BadElementException {

    PdfPTable table = new PdfPTable(2);

    PdfPCell c1 = new PdfPCell(new Phrase("Evento Id[" + evento.getId() + "]"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setColspan(2);//from  w ww .j  a  v  a  2s. c om
    table.addCell(c1);
    table.setHeaderRows(1);

    //riga 1
    if (evento.getData() != null) {
        table.addCell(new Phrase("Data registrazione"));
        table.addCell(new Phrase("" + evento.getData()));
    }
    //riga 1
    if (evento.getDominio() != null) {
        table.addCell(new Phrase("Id Dominio"));
        table.addCell(new Phrase(evento.getDominio()));
    }
    //riga 1
    if (evento.getIuv() != null) {
        table.addCell(new Phrase("IUV"));
        table.addCell(new Phrase(evento.getIuv()));
    }
    //riga 1
    if (evento.getCcp() != null) {
        table.addCell(new Phrase("CCP"));
        table.addCell(new Phrase(evento.getCcp()));
    }
    //riga 1
    if (evento.getPsp() != null) {
        table.addCell(new Phrase("Id PSP"));
        table.addCell(new Phrase(evento.getPsp()));
    }
    //riga 1
    if (evento.getTipoVersamento() != null) {
        table.addCell(new Phrase("Tipo Versamento"));
        table.addCell(new Phrase(evento.getTipoVersamento()));
    }
    //riga 1
    if (evento.getComponente() != null) {
        table.addCell(new Phrase("Componente"));
        table.addCell(new Phrase(evento.getComponente().toString()));
    }
    //riga 1
    if (evento.getCategoria() != null) {
        table.addCell(new Phrase("Categoria Evento"));
        table.addCell(new Phrase(evento.getCategoria().toString()));
    }
    //riga 1
    if (evento.getTipo() != null) {
        table.addCell(new Phrase("Tipo Evento"));
        table.addCell(new Phrase(evento.getTipo()));
    }
    //riga 1
    if (evento.getSottoTipo() != null) {
        table.addCell(new Phrase("Sottotipo Evento"));
        table.addCell(new Phrase(evento.getSottoTipo().toString()));
    }
    //riga 1
    if (evento.getFruitore() != null) {
        table.addCell(new Phrase("Id Fruitore"));
        table.addCell(new Phrase(evento.getFruitore()));
    }
    //riga 1
    if (evento.getErogatore() != null) {
        table.addCell(new Phrase("Id Erogatore"));
        table.addCell(new Phrase(evento.getErogatore()));
    }
    //riga 1
    if (evento.getStazioneIntermediarioPA() != null) {
        table.addCell(new Phrase("Id Stazione Intermediario PA"));
        table.addCell(new Phrase(evento.getStazioneIntermediarioPA()));
    }
    //riga 1
    if (evento.getCanalePagamento() != null) {
        table.addCell(new Phrase("Canale Pagamento"));
        table.addCell(new Phrase(evento.getCanalePagamento()));
    }
    //riga 1
    if (evento.getParametri() != null) {
        table.addCell(new Phrase("Parametri Specifici Interfaccia"));
        table.addCell(new Phrase(evento.getParametri()));
    }

    if (evento.getEsito() != null) {
        table.addCell(new Phrase("Esito"));
        table.addCell(new Phrase(evento.getEsito()));
    }

    subCatPart.add(table);

}

From source file:jdbreport.model.io.pdf.itext2.PdfWriter.java

License:Apache License

private PdfPCell writeCell(ReportModel model, jdbreport.model.Cell srcCell, int row, int col)
        throws BadElementException, IOException, SaveReportException {

    CellStyle style = model.getStyles(srcCell.getStyleId());

    java.awt.Rectangle rect = model.getCellRect(row, col, true, true);

    float h = Math.round((float) Units.PT.setYPixels((int) rect.getHeight()));
    float w = Math.round((float) Units.PT.setXPixels((int) rect.getWidth()));

    PdfPCell pdfCell = null;

    if (srcCell.getPicture() != null) {
        java.awt.Image awtImage = srcCell.getPicture().getImage();
        com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h);
        pdfCell = new PdfPCell(image);
    } else {/* w  w w . j  av a2s.  co  m*/

        String text = null;

        if (srcCell.getValue() instanceof CellValue<?>) {

            StringWriter strWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(strWriter);

            if (!((CellValue<?>) srcCell.getValue()).write(printWriter, model, row, col, this,
                    ReportBook.PDF)) {
                java.awt.Image awtImage = ((CellValue<?>) srcCell.getValue()).getAsImage(model, row, col);
                if (awtImage != null) {
                    com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h);
                    pdfCell = new PdfPCell(image);
                }
            } else {
                text = strWriter.getBuffer().toString();
            }

        } else {
            if (jdbreport.model.Cell.TEXT_HTML.equals(srcCell.getContentType())) {
                pdfCell = new PdfPCell();
                writeHTMLText(model.getStyles(srcCell.getStyleId()), srcCell, pdfCell);
            } else {
                text = model.getCellText(srcCell);
            }
        }

        if (pdfCell == null) {
            pdfCell = new PdfPCell();
        }

        if (text != null && text.length() > 0) {
            com.lowagie.text.Font font;
            if (fonts.containsKey(style.getId())) {
                font = fonts.get(style.getId());
            } else {
                font = getFontMapper().styleToPdf(style);
                fonts.put(style.getId(), font);
            }
            Paragraph p;
            if (font != null) {
                p = new Paragraph(text, font);
            } else {
                p = new Paragraph(text);
            }
            pdfCell.setPhrase(p);
            pdfCell.setPadding(1);
            pdfCell.setLeading(0f, 1.1f);
        } else {
            pdfCell.setPadding(0);
        }
    }

    pdfCell.setFixedHeight(h);
    pdfCell.setBackgroundColor(style.getBackground());
    pdfCell.setHorizontalAlignment(toPdfHAlignment(style.getHorizontalAlignment()));
    pdfCell.setVerticalAlignment(toPdfVAlignment(style.getVerticalAlignment()));

    if (style.getAngle() != 0) {
        pdfCell.setRotation(roundAngle(style.getAngle()));
    }

    assignBorders(style, pdfCell);
    pdfCell.setNoWrap(!style.isWrapLine());
    if (srcCell.getColSpan() > 0) {
        pdfCell.setColspan(srcCell.getColSpan() + 1);
    }
    if (srcCell.getRowSpan() > 0) {
        pdfCell.setRowspan(srcCell.getRowSpan() + 1);
    }

    return pdfCell;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDF(String texto, int tipo, int tamanio, int estilo, int alineacion, int borde) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.LIGHT_GRAY);
    celda.setBorderWidth(borde);//from  ww  w.j a va  2s .  c o m
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDF(String texto, int tipo, int tamanio, int estilo, int alineacion, int borde,
        int padding) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.LIGHT_GRAY);
    celda.setBorderWidth(borde);/* w ww.j a  v  a 2 s. c  o  m*/
    celda.setPadding(padding);
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDFBottom(String texto, int tipo, int tamanio, int estilo, int alineacion,
        int borde, int padding, int colspan) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo, Color.WHITE)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.WHITE);
    celda.setColspan(colspan);//from   w  w  w . j  a v a 2  s .  com
    celda.setBorder(borde);
    celda.setPadding(padding);
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDFCarnetOpciones(String texto, int tipo, int tamanio, int estilo,
        int alineacion, int borde, int padding, int colspan, Color color) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo, Color.WHITE)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.black);
    celda.setBackgroundColor(color);/*  w ww .j  av  a 2  s  . c  o m*/
    celda.setColspan(colspan);
    celda.setBorderWidth(borde);
    celda.setPadding(0);
    celda.setPaddingBottom(padding);
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDFCarnet(String texto, int tipo, int tamanio, int estilo, int alineacion,
        int borde, int padding, int colspan) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo, Color.WHITE)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.WHITE);
    celda.setColspan(colspan);//w  ww .ja va 2 s .c  o m
    celda.setBorderWidth(0);
    celda.setBorderWidthBottom(borde);
    celda.setPadding(0);
    celda.setPaddingBottom(padding);
    return celda;
}