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.prime.location.billing.InvoicedBillingManager.java

/**
 * Print invoice//from   www  .  j  a v a  2  s.com
 */
public void printInvoice() {
    try { //catch better your exceptions, this is just an example
        FacesContext context = FacesContext.getCurrentInstance();

        Document pdf = new Document(PageSize.A4, 5f, 5f, 75f, 45f);

        String fileName = "PDFFile";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(pdf, baos);
        TableHeader event = new TableHeader();
        event.setHeader("Header Here");
        writer.setPageEvent(event);

        if (!pdf.isOpen()) {
            pdf.open();
        }

        PdfPCell cell;

        PdfPTable header = new PdfPTable(new float[] { 1, 2, 1 });
        cell = new PdfPCell(new Paragraph("INVOICE",
                FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC, Color.BLACK)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setBorder(Rectangle.NO_BORDER);
        header.addCell(cell);
        header.setTotalWidth(527);
        header.setLockedWidth(true);

        cell = new PdfPCell(new Phrase("Agency: " + selectedAgency.getDescription()));
        cell.setColspan(2);
        cell.setBorder(Rectangle.NO_BORDER);
        header.addCell(cell);

        cell = new PdfPCell(new Phrase("Invoice#: " + selectedInvoice.getId()));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        header.addCell(cell);

        cell = new PdfPCell(
                new Phrase("Date: " + DateUtility.dateTimeFormat(selectedInvoice.getInvoiceDate())));
        cell.setColspan(3);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        header.addCell(cell);

        pdf.add(header);

        PdfPTable table = new PdfPTable(new float[] { 1, 3, 2, 1 });
        table.setSpacingBefore(15f);

        table.setTotalWidth(527);
        table.setLockedWidth(true);
        //table.setWidths(new int[]{3, 1, 1});

        table.getDefaultCell().setBackgroundColor(Color.LIGHT_GRAY);

        table.addCell("Date");
        table.addCell("Name");
        table.addCell("Service");
        table.addCell("Rate");

        table.getDefaultCell().setBackgroundColor(null);

        cell = new PdfPCell(new Phrase("Total(US$): "));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setColspan(3);
        cell.setBackgroundColor(Color.LIGHT_GRAY);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(selectedInvoice.getAmount())));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBackgroundColor(Color.LIGHT_GRAY);
        table.addCell(cell);

        // There are three special rows
        table.setHeaderRows(2);
        // One of them is a footer
        table.setFooterRows(1);
        Font f = FontFactory.getFont(FontFactory.HELVETICA, 10);
        //add remaining
        for (AgencyBilling billing : selectedInvoice.getBillings()) {
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.getDefaultCell().setIndent(2);
            table.getDefaultCell().setFixedHeight(20);

            table.addCell(new Phrase(DateUtility.dateFormat(billing.getBillingDate()), f));
            table.addCell(new Phrase(billing.getPerson().getName(), f));
            table.addCell(new Phrase(billing.getTariff().getTariffType().getDescription(), f));
            cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(billing.getRate()), f));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

            table.addCell(cell);

        }

        pdf.add(table);

        writer.getAcroForm().setNeedAppearances(true);

        //document.add(new Phrase(TEXT));
        //Keep modifying your pdf file (add pages and more)
        pdf.close();

        writePDFToResponse(context.getExternalContext(), baos, fileName);

        context.responseComplete();

    } catch (Exception e) {
        //e.printStackTrace();          
    }
}

From source file:com.qcadoo.mes.deliveries.print.DeliveryReportPdf.java

License:Open Source License

private void addTotalRow(final PdfPTable productsTable, final Locale locale, final List<String> columnsName,
        Entity delivery) {// www.  ja v a2s .c  o m
    DeliveryPricesAndQuantities deliveryPricesAndQuantities = new DeliveryPricesAndQuantities(delivery,
            numberService);

    PdfPCell total = new PdfPCell(
            new Phrase(translationService.translate("deliveries.delivery.report.totalCost", locale),
                    FontUtils.getDejavuRegular7Dark()));

    total.setColspan(2);
    total.setHorizontalAlignment(Element.ALIGN_LEFT);
    total.setVerticalAlignment(Element.ALIGN_MIDDLE);
    total.setBackgroundColor(null);
    total.disableBorderSide(Rectangle.RIGHT);
    total.disableBorderSide(Rectangle.LEFT);
    total.setBorderColor(ColorUtils.getLineLightColor());

    productsTable.addCell(total);

    for (int i = 2; i < columnsName.size(); i++) {
        if (columnsName.contains(OrderedProductFields.ORDERED_QUANTITY)
                && columnsName.indexOf(OrderedProductFields.ORDERED_QUANTITY) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            productsTable.addCell(
                    new Phrase(numberService.format(deliveryPricesAndQuantities.getOrderedCumulatedQuantity()),
                            FontUtils.getDejavuRegular7Dark()));
        } else if (columnsName.contains(DeliveredProductFields.DELIVERED_QUANTITY)
                && columnsName.indexOf(DeliveredProductFields.DELIVERED_QUANTITY) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            productsTable.addCell(new Phrase(
                    numberService.format(deliveryPricesAndQuantities.getDeliveredCumulatedQuantity()),
                    FontUtils.getDejavuRegular7Dark()));
        } else if (columnsName.contains(DeliveredProductFields.TOTAL_PRICE)
                && columnsName.indexOf(DeliveredProductFields.TOTAL_PRICE) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            productsTable.addCell(
                    new Phrase(numberService.format(deliveryPricesAndQuantities.getDeliveredTotalPrice()),
                            FontUtils.getDejavuRegular7Dark()));
        } else if (columnsName.contains(L_CURRENCY) && columnsName.indexOf(L_CURRENCY) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            productsTable.addCell(
                    new Phrase(deliveriesService.getCurrency(delivery), FontUtils.getDejavuRegular7Dark()));
        } else {
            productsTable.addCell("");
        }
    }
}

From source file:com.qcadoo.mes.deliveries.print.DeliveryReportPdf.java

License:Open Source License

private void prepareProductColumnAlignment(final PdfPCell cell, final ColumnAlignment columnAlignment) {
    if (ColumnAlignment.LEFT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    } else if (ColumnAlignment.RIGHT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    }/* ww  w  .jav  a 2s  .  c om*/
}

From source file:com.qcadoo.mes.deliveries.print.OrderReportPdf.java

License:Open Source License

private void addTotalRow(final PdfPTable productsTable, final Locale locale, final List<String> columnsName,
        Entity delivery) {//from  w w w  .  j  ava  2  s.  c  o m
    DeliveryPricesAndQuantities deliveryPricesAndQuantities = new DeliveryPricesAndQuantities(delivery,
            numberService);

    PdfPCell total = new PdfPCell(
            new Phrase(translationService.translate("deliveries.delivery.report.totalCost", locale),
                    FontUtils.getDejavuRegular7Dark()));

    total.setColspan(2);
    total.setHorizontalAlignment(Element.ALIGN_LEFT);
    total.setVerticalAlignment(Element.ALIGN_MIDDLE);
    total.setBackgroundColor(null);
    total.disableBorderSide(Rectangle.RIGHT);
    total.disableBorderSide(Rectangle.LEFT);
    total.setBorderColor(ColorUtils.getLineLightColor());

    productsTable.addCell(total);

    for (int i = 2; i < columnsName.size(); i++) {
        if (columnsName.contains(OrderedProductFields.ORDERED_QUANTITY)
                && columnsName.indexOf(OrderedProductFields.ORDERED_QUANTITY) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            productsTable.addCell(
                    new Phrase(numberService.format(deliveryPricesAndQuantities.getOrderedCumulatedQuantity()),
                            FontUtils.getDejavuRegular7Dark()));
        } else if (columnsName.contains(OrderedProductFields.TOTAL_PRICE)
                && columnsName.indexOf(OrderedProductFields.TOTAL_PRICE) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            productsTable.addCell(
                    new Phrase(numberService.format(deliveryPricesAndQuantities.getOrderedTotalPrice()),
                            FontUtils.getDejavuRegular7Dark()));
        } else if (columnsName.contains(L_CURRENCY) && columnsName.indexOf(L_CURRENCY) == i) {
            productsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            productsTable.addCell(
                    new Phrase(deliveriesService.getCurrency(delivery), FontUtils.getDejavuRegular7Dark()));
        } else {
            productsTable.addCell("");
        }
    }
}

From source file:com.qcadoo.mes.orders.print.OrderReportPdf.java

License:Open Source License

private PdfPCell createCell(String content, int alignment) {
    PdfPCell cell = new PdfPCell();
    float border = 0.2f;
    cell.setPhrase(new Phrase(content, FontUtils.getDejavuRegular7Dark()));
    cell.setHorizontalAlignment(alignment);
    cell.setBorderWidth(border);//from ww  w  . j  a v a2 s.  co m
    cell.disableBorderSide(PdfPCell.RIGHT);
    cell.disableBorderSide(PdfPCell.LEFT);
    cell.setPadding(5);
    return cell;
}

From source file:com.qcadoo.mes.warehouseMinimalState.print.DocumentPdf.java

License:Open Source License

public void addSmallCell(PdfPTable table, BigDecimal content) {
    PdfPCell cell = new PdfPCell(table.getDefaultCell());
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    String value = numberService.formatWithMinimumFractionDigits(content, 0);
    cell.setPhrase(new Phrase(value, FontUtils.getDejavuRegular7Dark()));
    table.addCell(cell);/*from  w  ww. j a v  a 2 s  . co m*/
}

From source file:com.qcadoo.mes.workPlans.pdf.document.operation.component.OperationProductInTable.java

License:Open Source License

private void alignColumn(final PdfPCell cell, final ColumnAlignment columnAlignment) {
    if (ColumnAlignment.LEFT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    } else if (ColumnAlignment.RIGHT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    }//  ww  w.jav  a2  s  .c  om
}

From source file:com.qcadoo.mes.workPlans.pdf.document.WorkPlanPdfForDivision.java

License:Open Source License

private void addOperationTable(PdfWriter pdfWriter, GroupingContainer groupingContainer, Document document,
        OrderOperationComponent orderOperationComponent, Locale locale) throws DocumentException {

    Map<Long, Map<OperationProductColumn, ColumnAlignment>> outputProductsMap = groupingContainer
            .getOperationComponentIdProductOutColumnToAlignment();

    Map<Long, Map<OperationProductColumn, ColumnAlignment>> inputProductsMap = groupingContainer
            .getOperationComponentIdProductInColumnToAlignment();

    Entity operationComponent = orderOperationComponent.getOperationComponent();
    Entity order = orderOperationComponent.getOrder();
    Entity product = order.getBelongsToField(OrderFields.PRODUCT);

    Map<OperationProductColumn, ColumnAlignment> inputProductColumnAlignmentMap = inputProductsMap
            .get(operationComponent.getId());
    Map<OperationProductColumn, ColumnAlignment> outputProductColumnAlignmentMap = outputProductsMap
            .get(operationComponent.getId());

    PdfPTable table = pdfHelper.createPanelTable(3);

    PdfPCell headerCell = new PdfPCell();
    headerCell.setBorder(Rectangle.NO_BORDER);
    headerCell.setColspan(2);//from   w  ww.ja  v  a  2s  .  co m

    PdfPCell inputCell = new PdfPCell();
    inputCell.setBorder(Rectangle.NO_BORDER);
    PdfPCell outputCell = new PdfPCell();
    outputCell.setBorder(Rectangle.NO_BORDER);
    PdfPCell codeCell = new PdfPCell();
    codeCell.setBorder(Rectangle.NO_BORDER);
    codeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    codeCell.setVerticalAlignment(Element.ALIGN_TOP);
    codeCell.setRowspan(2);

    // addOperationSummary(headerCell, operationComponent);

    addOrderSummary(headerCell, order, product, operationComponent);

    addOperationProductsTable(inputCell, operationProductInComponents(operationComponent, order),
            inputProductColumnAlignmentMap, ProductDirection.IN, locale);
    addOperationProductsTable(outputCell, operationProductOutComponents(operationComponent, order),
            outputProductColumnAlignmentMap, ProductDirection.OUT, locale);

    codeCell.addElement(createBarcode(pdfWriter, operationComponent));

    float[] tableColumnWidths = new float[] { 70f, 70f, 10f };
    table.setWidths(tableColumnWidths);
    table.setTableEvent(null);
    table.addCell(headerCell);
    table.addCell(codeCell);
    table.addCell(inputCell);
    table.addCell(outputCell);
    table.setKeepTogether(true);
    document.add(table);
}

From source file:com.qcadoo.mes.workPlans.pdf.document.WorkPlanPdfForDivision.java

License:Open Source License

private void alignColumn(final PdfPCell cell, final ColumnAlignment columnAlignment) {
    if (ColumnAlignment.LEFT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    } else if (ColumnAlignment.RIGHT.equals(columnAlignment)) {
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    }/*from   ww w  .j  a  va 2 s  .  c  om*/
    cell.setBorder(Rectangle.BOX);
    cell.setPadding(2f);
}

From source file:com.slamd.report.PDFReportGenerator.java

License:Open Source License

/**
 * Performs the appropriate action necessary when starting a new page.  In
 * this case, we will write the SLAMD header to the top of the page.
 *
 * @param  writer    The writer used to write the PDF document.
 * @param  document  The PDF document being written.
 *//*from w w w .ja v a 2 s .c o  m*/
public void onStartPage(PdfWriter writer, Document document) {
    try {
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);

        PdfPCell blueCell = new PdfPCell(new Phrase(" \n "));
        blueCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        blueCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        blueCell.setBackgroundColor(new Color(0x59, 0x4F, 0xBF));
        blueCell.setBorderWidth(inchesToPoints(1.0 / 16));
        blueCell.setBorderColor(new Color(0xFF, 0xFF, 0xFF));
        blueCell.setPadding(inchesToPoints(1.0 / 16));
        table.addCell(blueCell);

        Phrase titlePhrase = new Phrase("SLAMD Generated Report",
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(0x59, 0x4F, 0xBF)));
        PdfPCell yellowCell = new PdfPCell(titlePhrase);
        yellowCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        yellowCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        yellowCell.setBackgroundColor(new Color(0xFB, 0xE2, 0x49));
        yellowCell.setBorderWidth(inchesToPoints(1.0 / 16));
        yellowCell.setBorderColor(new Color(0xFF, 0xFF, 0xFF));
        yellowCell.setPadding(inchesToPoints(1.0 / 16));
        table.addCell(yellowCell);

        Phrase versionPhrase = new Phrase("Version " + DynamicConstants.SLAMD_VERSION,
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(0xFF, 0xFF, 0xFF)));
        PdfPCell redCell = new PdfPCell(versionPhrase);
        redCell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
        redCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        redCell.setBackgroundColor(new Color(0xD1, 0x21, 0x24));
        redCell.setBorderWidth(inchesToPoints(1.0 / 16));
        redCell.setBorderColor(new Color(0xFF, 0xFF, 0xFF));
        redCell.setPadding(inchesToPoints(1.0 / 16));
        table.addCell(redCell);

        document.add(table);
    } catch (Exception e) {
        e.printStackTrace();
    }
}