Example usage for com.itextpdf.text Element ALIGN_LEFT

List of usage examples for com.itextpdf.text Element ALIGN_LEFT

Introduction

In this page you can find the example usage for com.itextpdf.text Element ALIGN_LEFT.

Prototype

int ALIGN_LEFT

To view the source code for com.itextpdf.text Element ALIGN_LEFT.

Click Source Link

Document

A possible value for paragraph alignment.

Usage

From source file:org.me.modelos.PDFHelper.java

public void tablaToPdf(JTable jTable, File pdfNewFile, String title) {
    try {/*from  w  w w. j av  a  2s  .co m*/
        Font subCategoryFont = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
        Document document = new Document(PageSize.LETTER.rotate());
        PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(pdfNewFile));
        } catch (FileNotFoundException fileNotFoundException) {
            Message.showErrorMessage(fileNotFoundException.getMessage());
        }

        writer.setBoxSize("art", new Rectangle(150, 10, 700, 580));
        writer.setPageEvent(new HeaderFooterPageEvent());
        document.open();
        document.addTitle(title);

        document.addSubject("Reporte");
        document.addKeywords("reportes, gestion, pdf");
        document.addAuthor("Gestion de Proyectos de software");
        document.addCreator("gestion de proyectos");

        Paragraph parrafo = new Paragraph(title, subCategoryFont);

        PdfPTable table = new PdfPTable(jTable.getColumnCount());
        table.setWidthPercentage(100);
        PdfPCell columnHeader;

        for (int column = 0; column < jTable.getColumnCount(); column++) {
            Font font = new Font(Font.FontFamily.HELVETICA);
            font.setColor(255, 255, 255);
            columnHeader = new PdfPCell(new Phrase(jTable.getColumnName(column), font));
            columnHeader.setHorizontalAlignment(Element.ALIGN_LEFT);
            columnHeader.setBackgroundColor(new BaseColor(96, 125, 139));
            table.addCell(columnHeader);
        }
        table.getDefaultCell().setBackgroundColor(new BaseColor(255, 255, 255));
        table.setHeaderRows(1);
        BaseColor verdad = new BaseColor(255, 255, 255);
        BaseColor falso = new BaseColor(214, 230, 244);
        boolean bandera = false;
        for (int row = 0; row < jTable.getRowCount(); row++) {

            for (int column = 0; column < jTable.getColumnCount(); column++) {

                if (bandera) {
                    table.getDefaultCell().setBackgroundColor(verdad);
                } else {
                    table.getDefaultCell().setBackgroundColor(falso);
                }
                table.addCell(jTable.getValueAt(row, column).toString());
                bandera = !bandera;
            }

        }

        parrafo.add(table);
        document.add(parrafo);
        document.close();
        //JOptionPane.showMessageDialog(null, "Se ha creado el pdf en " + pdfNewFile.getPath(),
        //        "RESULTADO", JOptionPane.INFORMATION_MESSAGE);
    } catch (DocumentException documentException) {
        System.out.println("Se ha producido un error " + documentException);
        JOptionPane.showMessageDialog(null, "Se ha producido un error" + documentException, "ERROR",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.openlmis.web.view.pdf.requisition.RequisitionCellFactory.java

License:Open Source License

public static PdfPCell textCell(String value) {
    PdfPCell cell = getPdfPCell(value);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    return cell;
}

From source file:org.openlmis.web.view.pdf.requisition.RequisitionCellFactory.java

License:Open Source License

public static PdfPCell headingCell(String value) {
    Chunk chunk = new Chunk(value, H2_FONT);
    PdfPCell cell = new PdfPCell(new Phrase(chunk));
    cell.setPadding(CELL_PADDING);/*ww  w  . j  a v a  2 s. co m*/
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    return cell;
}

From source file:org.opensrp.web.utils.PdfUtil.java

License:Open Source License

public static ByteArrayOutputStream generatePdf(List<String> data, int width, int height, int copiesImage,
        int columnLimit) {
    try {/* ww w.  j ava  2  s .  c o m*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Document document = new Document();
        document.setMargins(MARGINS[0], MARGINS[1], MARGINS[2], MARGINS[3]);

        PdfWriter.getInstance(document, byteArrayOutputStream);
        document.open();

        PdfPTable table = new PdfPTable(columnLimit);
        table.setTotalWidth(TABLE_WIDTH);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);

        int length = 0;
        int count = 0;

        for (String str : data) {
            if (str.length() > 0 && str.length() <= 5) {
                length = 54;
            } else if (str.length() >= 6 && str.length() <= 9) {
                length = 44;
            } else if (str.length() >= 10 && str.length() <= 11) {
                length = 36;
            } else if (str.length() >= 12 && str.length() <= 14) {
                length = 27;
            } else if (str.length() >= 15 && str.length() <= 17) {
                length = 22;
            } else {
                length = 15;
            }
            Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix byteMatrix = null;

            byteMatrix = qrCodeWriter.encode(str, BarcodeFormat.QR_CODE, width, height, hintMap);

            int matrixWidth = byteMatrix.getWidth();
            int matrixHeight = byteMatrix.getHeight();
            BufferedImage image = new BufferedImage(matrixWidth, matrixHeight, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();
            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, matrixWidth + 5, matrixHeight + 5);
            graphics.setFont(graphics.getFont().deriveFont(13f));
            graphics.setColor(Color.BLACK);
            graphics.drawString(str, length, height - 10);
            for (int i = 0; i < matrixHeight; i++) {
                for (int j = 0; j < matrixHeight; j++) {
                    if (byteMatrix.get(i, j)) {
                        graphics.fillRect((i), j, 1, 1);
                    }
                }
            }
            Image itextImage = null;
            itextImage = Image.getInstance(Toolkit.getDefaultToolkit().createImage(image.getSource()), null);

            for (int i = 0; i < copiesImage; i++) {
                PdfPCell cell = new PdfPCell(itextImage);
                cell.setBorder(Rectangle.NO_BORDER);
                count++;
                table.addCell(cell);
            }
        }
        for (int i = 0; i < 6; i++) {
            if (count % columnLimit != 0) {
                PdfPCell cell = new PdfPCell(new Phrase());
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);
                count++;
            }
        }
        document.add(table);
        document.close();

        return byteArrayOutputStream;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.patientview.radar.service.impl.PdfDocumentDataBuilder.java

License:Open Source License

private PdfPCell createBaseCell(String text, Font font) {
    PdfPCell cell = new PdfPCell(new Phrase(text, font));
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setPadding(4f);/*from  w w  w.jav  a 2s.c o m*/
    return cell;
}

From source file:org.qnot.passtab.PDFOutput.java

License:Open Source License

private void addCell(PdfPTable table, String str, boolean bold, boolean fill, boolean rightBorder,
        boolean bottomBorder) {
    Phrase phrase = withColor ? new Phrase(str, bold ? fontBold : font) : new Phrase(str, font);

    PdfPCell cell = new PdfPCell(phrase);

    cell.setBorder(Rectangle.NO_BORDER);
    cell.setPadding(0f);/*from  ww  w. jav a  2  s . c o m*/
    cell.setPaddingTop(2f);

    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

    if (rightBorder) {
        cell.setBorderWidthRight(1f);
        cell.setPaddingRight(3f);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    }
    if (bottomBorder) {
        cell.setBorderWidthBottom(1f);
        cell.setPaddingBottom(3f);
    }

    if (fill && this.withColor) {
        cell.setGrayFill(0.80f);
    }

    table.addCell(cell);
}

From source file:org.restate.project.controller.PaymentReceiptController.java

License:Open Source License

@RequestMapping(method = RequestMethod.GET, value = "receipt.print")
public void downloadDocument(HttpServletResponse response,
        @RequestParam(value = "id", required = false) Integer id) throws Exception {

    if (id == null) {
        return;/*  w w w. j  a  v a  2  s . c o  m*/
    }

    // creation of the document with a certain size and certain margins
    // may want to use PageSize.LETTER instead
    // Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    Document doc = new Document();
    PdfWriter docWriter = null;

    DecimalFormat df = new DecimalFormat("0.00");
    File tmpFile = File.createTempFile("paymentReceipt", ".pdf");

    try {

        //special font sizes
        Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);

        //file path

        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(tmpFile));

        //document header attributes
        doc.addAuthor("betterThanZero");
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator("MySampleCode.com");
        doc.addTitle("Report with Column Headings");
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        Paragraph paragraph = new Paragraph("iText  is a library that allows you to create and "
                + "manipulate PDF documents. It enables developers looking to enhance web and other "
                + "applications with dynamic PDF document generation and/or manipulation.");

        //specify column widths
        float[] columnWidths = { 1.5f, 2f, 5f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(90f);

        //insert column headings
        insertCell(table, "Order No", Element.ALIGN_RIGHT, 1, bfBold12);
        insertCell(table, "Account No", Element.ALIGN_LEFT, 1, bfBold12);
        insertCell(table, "Account Name", Element.ALIGN_LEFT, 1, bfBold12);
        insertCell(table, "Order Total", Element.ALIGN_RIGHT, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
        //create section heading by cell merging
        insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
        double orderTotal, total = 0;

        //just some random data to fill
        for (int x = 1; x < 5; x++) {

            insertCell(table, "10010" + x, Element.ALIGN_RIGHT, 1, bf12);
            insertCell(table, "ABC00" + x, Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, "This is Customer Number ABC00" + x, Element.ALIGN_LEFT, 1, bf12);

            orderTotal = Double.valueOf(df.format(Math.random() * 1000));
            total = total + orderTotal;
            insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);

        }
        //merge the cells to create a footer for that section
        insertCell(table, "New York Total...", Element.ALIGN_RIGHT, 3, bfBold12);
        insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);

        //repeat the same as above to display another location
        insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
        insertCell(table, "California Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
        orderTotal = 0;

        for (int x = 1; x < 7; x++) {

            insertCell(table, "20020" + x, Element.ALIGN_RIGHT, 1, bf12);
            insertCell(table, "XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, "This is Customer Number XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);

            orderTotal = Double.valueOf(df.format(Math.random() * 1000));
            total = total + orderTotal;
            insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);

        }
        insertCell(table, "California Total...", Element.ALIGN_RIGHT, 3, bfBold12);
        insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);

        //add the PDF table to the paragraph
        paragraph.add(table);
        // add the paragraph to the document
        doc.add(paragraph);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();

        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }

        response.setHeader("Content-disposition", "attachment; filename=" + "sampleDoc" + ".pdf");
        response.setContentType("application/pdf");
        OutputStream outputStream = response.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(tmpFile);

        IOUtils.copy(fileInputStream, outputStream);
        fileInputStream.close();
        outputStream.flush();

        tmpFile.delete();
    }

}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addTopTable(Section subCatPart, Json data) throws DocumentException {
    PdfPTable table = new PdfPTable(4);
    table.setWidthPercentage(100);/*w  ww .  j a  va 2  s . co m*/
    int[] widths = { 12, 55, 15, 20 };
    table.setWidths(widths);

    PdfPCell cell = null;

    OWLNamedIndividual typeInd = individual(legacyPrefix + data.at("type").asString());
    addCell(cell, table, "Type:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEntityLabel(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "SR #:", Element.ALIGN_RIGHT, boldFont, null);
    if (data.at("properties").has("hasCaseNumber"))
        addCell(cell, table, data.at("properties").at("hasCaseNumber").asString(), Element.ALIGN_LEFT,
                normalFont, null);
    else
        addCell(cell, table, GenUtils.makeCaseNumber(data.at("boid").asLong()), Element.ALIGN_LEFT, normalFont,
                null);
    //02-20-2014 - Syed add providedby to report
    addCell(cell, table, "Area:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getArea(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Priority:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, data.at("properties").at("hasPriority").at("label").asString(), Element.ALIGN_LEFT,
            normalFont, null);
    addCell(cell, table, "Group:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getGroup(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Status:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEntityLabel(individual(data.at("properties"), "hasStatus")), Element.ALIGN_LEFT,
            normalFont, null);

    addCell(cell, table, "Jurisdiction:", Element.ALIGN_RIGHT, boldFont, null);
    OWLLiteral jurisdiction = dataProperty(typeInd, legacyPrefix + "hasJurisdictionDescription");
    addCell(cell, table, jurisdiction != null ? jurisdiction.getLiteral() : emptyField, Element.ALIGN_LEFT,
            normalFont, null);
    addCell(cell, table, "Status Date:", Element.ALIGN_RIGHT, boldFont, null);
    String modifiedDate = data.at("properties").has("hasDateLastModified")
            ? data.at("properties").at("hasDateLastModified").asString()
            : "";
    addCell(cell, table, formatDate(modifiedDate, true, false), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "Input By:", Element.ALIGN_RIGHT, boldFont, null);
    String inputBy = data.at("properties").has("isCreatedBy")
            ? data.at("properties").at("isCreatedBy").asString()
            : emptyField;
    addCell(cell, table, inputBy, Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Created Date:", Element.ALIGN_RIGHT, boldFont, null);
    String createdDate = data.at("properties").has("hasDateCreated")
            ? data.at("properties").at("hasDateCreated").asString()
            : "";
    addCell(cell, table, formatDate(createdDate, true, false), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "CC Groups:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Created By:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEmployeeName(inputBy), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "Location:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table,
            data.at("properties").has("atAddress") ? buildAddress(data.at("properties").at("atAddress"))
                    : blankField,
            Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Method Received:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, data.at("properties").at("hasIntakeMethod").at("label").asString(), Element.ALIGN_LEFT,
            normalFont, null);

    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addDescription(Section subCatPart, Json data) throws DocumentException {
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);//www .  ja v  a  2  s  . co  m
    int[] widths = { 15, 85 };
    table.setWidths(widths);

    PdfPCell cell = null;

    addCell(cell, table, "Description:", Element.ALIGN_LEFT, bigBoldFont, null);
    if (data.at("properties").has("hasDetails"))
        addCell(cell, table, data.at("properties").at("hasDetails").asString(), Element.ALIGN_LEFT, normalFont,
                null);

    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addQuestions(Section subCatPart, Json data)
        throws DocumentException, UnsupportedEncodingException {
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);/*from   ww  w  .  j a va  2s.c  om*/
    int[] widths = { 65, 35 };
    table.setWidths(widths);

    PdfPCell cell = null;

    addCell(cell, table, "SR Questions", Element.ALIGN_LEFT, bigBoldFont, null);
    addCell(cell, table, "Answers", Element.ALIGN_LEFT, bigBoldFont, null);

    subCatPart.add(table);
    //subCatPart.add(Chunk.NEWLINE);
    //subCatPart.add(new Chunk(new LineSeparator()));

    table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setWidths(widths);

    OWLNamedIndividual typeInd = individual(legacyPrefix + data.at("type").asString());
    List<Json> allServiceAnswers = new ArrayList<Json>();
    LegacyEmulator.getAllServiceFields(typeInd, allServiceAnswers, true);

    List<Json> saList = getAllServiceAnswers(data);

    for (int k = 0; k < allServiceAnswers.size(); k++) {
        Json serviceAnswer = allServiceAnswers.get(k);
        BaseColor color = addColor(k);
        int counter = 0;
        addCell(cell, table, serviceAnswer.at("hasServiceField").at("label").asString(), Element.ALIGN_LEFT,
                normalFont, color);
        for (Json sa : saList) {
            if (sa.at("hasServiceField").at("iri").asString()
                    .equals(serviceAnswer.at("hasServiceField").at("iri").asString())) {
                OWLIndividual qtn = individual(sa.at("hasServiceField").at("iri").asString());
                OWLObjectProperty cvl = objectProperty(legacyPrefix + "hasChoiceValueList");

                if (sa.has("hasAnswerValue")) {
                    if (sa.at("hasAnswerValue").at("literal").isArray()) {
                        List<Json> literalList = sa.at("hasAnswerValue").at("literal").asJsonList();
                        for (int i = 0; i < literalList.size(); i++) {
                            String eachAns = literalList.get(i).asString();
                            addCell(cell, table, eachAns, Element.ALIGN_LEFT, normalFont, color);
                            if (i != literalList.size() - 1)
                                addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
                        }
                    } else {
                        String ans = sa.at("hasAnswerValue").at("literal").asString();
                        if (serviceAnswer.at("hasDataType").asString().equals("NUMBER"))
                            ans = Long.toString(new BigDecimal(ans).longValue());
                        if (serviceAnswer.at("hasDataType").asString().equals("DATE"))
                            ans = formatDate(ans, false, false);
                        addCell(cell, table, ans, Element.ALIGN_LEFT, normalFont, color);

                    }
                } else if (sa.has("hasAnswerObject")) {
                    if (sa.at("hasAnswerObject").isArray()) {
                        List<Json> answerObjectList = sa.at("hasAnswerObject").asJsonList();
                        for (int i = 0; i < answerObjectList.size(); i++) {
                            Json eachAnswerObject = answerObjectList.get(i);
                            //OWL.objectProperties(qtn, cvl.getIRI().toString());
                            if (!OWL.collectObjectProperties(qtn, cvl).isEmpty()
                                    && !containsWhiteSpace(eachAnswerObject.at("iri").asString())) {
                                addCell(cell, table,
                                        getEntityLabel(individual(eachAnswerObject.at("iri").asString())),
                                        Element.ALIGN_LEFT, normalFont, color);
                                if (i != answerObjectList.size() - 1)
                                    addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
                            }
                        }
                    } else {
                        String ans = sa.at("hasAnswerObject").at("iri").asString();
                        if (!OWL.collectObjectProperties(qtn, cvl).isEmpty() && !containsWhiteSpace(ans)) {
                            addCell(cell, table, getEntityLabel(individual(ans)), Element.ALIGN_LEFT,
                                    normalFont, color);
                        } else
                            addCell(cell, table, ans, Element.ALIGN_LEFT, normalFont, color);
                    }
                }
                ++counter;
            } else
                continue;
        }
        if (counter == 0)
            addCell(cell, table, emptyField, Element.ALIGN_LEFT, normalFont, color);
    }
    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}