Example usage for com.itextpdf.text Chunk append

List of usage examples for com.itextpdf.text Chunk append

Introduction

In this page you can find the example usage for com.itextpdf.text Chunk append.

Prototype

public StringBuffer append(final String string) 

Source Link

Document

appends some text to this Chunk.

Usage

From source file:fll.web.playoff.ScoresheetGenerator.java

License:Open Source License

/**
 * Stores the goal cells that are inserted into the output after the team name
 * headers and before the scoring/initials blanks at the bottom of the
 * scoresheet./*  ww w.j a  v a 2  s  . c o m*/
 */
private void setChallengeInfo(final ChallengeDescription description) {
    setPageTitle(description.getTitle());

    if (null != description.getRevision()) {
        setRevisionInfo(description.getRevision());
    }

    if (null != description.getCopyright()) {
        m_copyright = description.getCopyright();
    } else {
        m_copyright = null;
    }

    final PerformanceScoreCategory performanceElement = description.getPerformance();
    // use ArrayList as we will be doing indexed access in the loop
    final List<AbstractGoal> goals = new ArrayList<>(performanceElement.getGoals());

    final float[] relativeWidths = new float[3];
    relativeWidths[0] = 4;
    relativeWidths[1] = 48;
    relativeWidths[2] = 48;
    m_goalsTable = new PdfPTable(relativeWidths);

    String prevCategory = null;
    for (int goalIndex = 0; goalIndex < goals.size(); ++goalIndex) {
        final AbstractGoal goal = goals.get(goalIndex);
        if (!goal.isComputed()) {
            final String category = goal.getCategory();

            // add category cell if needed
            boolean firstRowInCategory = false;
            if (!StringUtils.equals(prevCategory, category)) {
                if (!StringUtils.isEmpty(category)) {

                    // find out how many future goals have the same category
                    int categoryRowSpan = 1;
                    for (int otherIndex = goalIndex + 1; otherIndex < goals.size(); ++otherIndex) {
                        final AbstractGoal otherGoal = goals.get(otherIndex);
                        if (!otherGoal.isComputed()) {
                            if (StringUtils.equals(category, otherGoal.getCategory())) {
                                ++categoryRowSpan;
                            } else {
                                break;
                            }
                        }
                    }

                    final Paragraph catPara = new Paragraph(category, ARIAL_10PT_NORMAL);
                    final PdfPCell categoryCell = new PdfPCell(catPara);
                    categoryCell.setBorderWidthTop(1);
                    categoryCell.setBorderWidthBottom(0);
                    categoryCell.setBorderWidthLeft(0);
                    categoryCell.setBorderWidthRight(0);
                    categoryCell.setVerticalAlignment(Element.ALIGN_CENTER);
                    categoryCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    categoryCell.setRotation(90);
                    categoryCell.setRowspan(categoryRowSpan);
                    m_goalsTable.addCell(categoryCell);
                }

                // first row in a new category, which may be empty
                firstRowInCategory = true;
            }

            // This is the text for the left hand "label" cell
            final String title = goal.getTitle();
            final Paragraph p = new Paragraph(title, ARIAL_10PT_NORMAL);
            final PdfPCell goalLabel = new PdfPCell(p);
            goalLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
            goalLabel.setVerticalAlignment(Element.ALIGN_CENTER);
            if (firstRowInCategory) {
                goalLabel.setBorderWidthTop(1);
                goalLabel.setBorderWidthBottom(0);
                goalLabel.setBorderWidthLeft(0);
                goalLabel.setBorderWidthRight(0);
            } else {
                goalLabel.setBorder(0);
            }
            goalLabel.setPaddingRight(9);
            goalLabel.setVerticalAlignment(Element.ALIGN_TOP);
            if (StringUtils.isEmpty(category)) {
                // category column and goal label column
                goalLabel.setColspan(2);
            }
            m_goalsTable.addCell(goalLabel);

            // define the value cell
            final double min = goal.getMin();
            final String minStr = FP.equals(min, Math.round(min), 1E-6) ? String.valueOf((int) min)
                    : String.valueOf(min);
            final double max = goal.getMax();
            final String maxStr = FP.equals(max, Math.round(max), 1E-6) ? String.valueOf((int) max)
                    : String.valueOf(max);

            // If element has child nodes, then we have an enumerated list
            // of choices. Otherwise it is either yes/no or a numeric field.
            final PdfPCell goalValue = new PdfPCell();
            final Chunk choices = new Chunk("", COURIER_10PT_NORMAL);
            if (goal.isEnumerated()) {
                // replace spaces with "no-break" spaces
                boolean first = true;
                final List<EnumeratedValue> values = goal.getSortedValues();
                for (final EnumeratedValue value : values) {
                    if (!first) {
                        choices.append(" /" + Utilities.NON_BREAKING_SPACE);
                    } else {
                        first = false;
                    }
                    choices.append(value.getTitle().toUpperCase().replace(' ', Utilities.NON_BREAKING_SPACE));
                }
                goalValue.addElement(choices);

            } else {
                if (goal.isYesNo()) {
                    // order of yes/no needs to match ScoreEntry.generateYesNoButtons
                    final Paragraph q = new Paragraph("NO / YES", COURIER_10PT_NORMAL);
                    goalValue.addElement(q);

                } else {
                    final String range = "(" + minStr + " - " + maxStr + ")";
                    final PdfPTable t = new PdfPTable(2);
                    t.setHorizontalAlignment(Element.ALIGN_LEFT);
                    t.setTotalWidth(1 * POINTS_PER_INCH);
                    t.setLockedWidth(true);
                    final Phrase r = new Phrase("", ARIAL_8PT_NORMAL);
                    t.addCell(new PdfPCell(r));
                    final Phrase q = new Phrase(range, ARIAL_8PT_NORMAL);
                    t.addCell(new PdfPCell(q));
                    goalValue.setPaddingTop(9);
                    goalValue.addElement(t);
                }
            }

            if (firstRowInCategory) {
                goalValue.setBorderWidthTop(1);
                goalValue.setBorderWidthBottom(0);
                goalValue.setBorderWidthLeft(0);
                goalValue.setBorderWidthRight(0);
            } else {
                goalValue.setBorder(0);
            }
            goalValue.setVerticalAlignment(Element.ALIGN_MIDDLE);

            m_goalsTable.addCell(goalValue);

            // setup for next loop
            prevCategory = category;
        } // if not computed goal

    } // foreach goal

}

From source file:reportes.ServletIntegral.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();
    ServletConfig config = getServletConfig();
    ServletContext context = config.getServletContext();
    String path = context.getContextPath();
    String realPath = context.getRealPath("/");
    response.setContentType("application/pdf");
    //Para descargar el PDF
    response.setHeader("Content-Disposition", "attachment; filename=\"ResultadoIntegral.pdf\"");
    // step 1: creation of a document-object
    try {/*from   w w  w  .  j a  va 2s.  c o m*/
        Document document = new Document(PageSize.LETTER);
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a temporary buffer
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, buffer);
        // step 3: we open the document
        document.open();
        // step 4: we add  content to the document
        Paragraph title = new Paragraph(
                (session.getAttribute("nEmpresa") != null) ? session.getAttribute("nEmpresa").toString()
                        : "Empresa");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(NORMAL);
        document.add(title);
        title = new Paragraph("Estado de resultado integral del 01 de Enero al 31 de Diciembre del "
                + (new Date().getYear() + 1900));
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(NORMAL);
        document.add(title);
        title = new Paragraph("Cifras en miles de pesos");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(NORMAL);
        document.add(title);
        for (int i = 0; i < 2; i++) {
            document.add(new Paragraph(" "));
        }
        PdfPTable table = new PdfPTable(2);
        //Obtenemos los datos de la clase ResultadoIntegral
        ResultadoIntegral r = new ResultadoIntegral();
        r.calculaResultado(Integer.parseInt(session.getAttribute("Empresa").toString()));
        for (int i = 0; i < r.getSaldos().size(); i++) {
            PdfPCell dato = new PdfPCell();
            if (i == 2 || i == 7 || i == 12 || i == 14 || i == 16 || i == 18 || i == 19 || i == 17) {
                Chunk ch = new Chunk(descripciones[i]);
                ch.setFont(BOLD_Tot);
                Phrase ph = new Phrase(ch);
                dato.setPhrase(ph);
            } else {
                Phrase ph = new Phrase(descripciones[i]);
                dato.setPhrase(ph);
            }
            dato.setBorder(Rectangle.NO_BORDER);
            dato.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(dato);
            if (r.getValores().size() > 0) {
                Chunk ch = new Chunk();
                if (r.getSaldos().get(i) < 0) {
                    String valor = "(" + (r.getSaldos().get(i) * (-1)) + ")";
                    ch.append(valor);
                    ch.setFont(NORMAL_Negative);
                } else {
                    dato.setPhrase(new Phrase("" + r.getSaldos().get(i)));
                }
            } else {
                dato.setPhrase(new Phrase("0.0"));
            }
            if (i == 1 || i == 6 || i == 11 || i == 13 || i == 15 || i == 17 || i == 18) {
                dato.setBorder(Rectangle.BOTTOM);
            } else {
                dato.setBorder(Rectangle.NO_BORDER);
            }
            dato.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(dato);
        }
        document.add(table);
        document.add(new Paragraph(""));
        // step 5: we close the document
        document.close();
        // step 6: we output the writer as bytes to the response output
        DataOutputStream output = new DataOutputStream(response.getOutputStream());
        byte[] bytes = buffer.toByteArray();
        response.setContentLength(bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            output.writeByte(bytes[i]);
        }
        output.close();
        response.getOutputStream();
        //session.removeAttribute("bean");
        //session.removeAttribute("producto");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}