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:org.unitime.timetable.webutil.PdfWebTable.java

License:Open Source License

private PdfPCell createCell() {
    PdfPCell cell = new PdfPCell();
    cell.setBorderColor(Color.BLACK);
    cell.setPadding(3);/*  w w w . j a  v  a 2s . co  m*/
    cell.setBorderWidth(0);
    cell.setVerticalAlignment(Element.ALIGN_TOP);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    return cell;
}

From source file:org.unitime.timetable.webutil.PdfWebTable.java

License:Open Source License

private float addImage(PdfPCell cell, String name) {
    try {// w  ww. java 2s. co m
        java.awt.Image awtImage = (java.awt.Image) iImages.get(name);
        if (awtImage == null)
            return 0;
        Image img = Image.getInstance(awtImage, Color.WHITE);
        Chunk ck = new Chunk(img, 0, 0);
        if (cell.getPhrase() == null) {
            cell.setPhrase(new Paragraph(ck));
            cell.setVerticalAlignment(Element.ALIGN_TOP);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } else {
            cell.getPhrase().add(ck);
        }
        return awtImage.getWidth(null);
    } catch (Exception e) {
        return 0;
    }
}

From source file:org.unitime.timetable.webutil.PdfWebTable.java

License:Open Source License

private float addText(PdfPCell cell, String text, boolean bold, boolean italic, boolean underline, Color color,
        Color bgColor) {/* w ww .  j  av a 2  s. c  om*/
    Font font = PdfFont.getFont(bold, italic, underline, color);
    Chunk chunk = new Chunk(text, font);
    if (bgColor != null)
        chunk.setBackground(bgColor);
    if (cell.getPhrase() == null) {
        cell.setPhrase(new Paragraph(chunk));
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } else {
        cell.getPhrase().add(chunk);
    }
    float width = 0;
    if (text.indexOf('\n') >= 0) {
        for (StringTokenizer s = new StringTokenizer(text, "\n"); s.hasMoreTokens();)
            width = Math.max(width, font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
    } else
        width = Math.max(width, font.getBaseFont().getWidthPoint(text, font.getSize()));
    return width;
}

From source file:org.unitime.timetable.webutil.PdfWebTable.java

License:Open Source License

/**
 * Prints pdf table. By default does not split table across
 * page boundaries /*  w  w  w.j av  a2s.  c  o m*/
 * @param ordCol
 * @param keepTogether true does not split table across pages
 * @return
 */
public PdfPTable printPdfTable(int ordCol, boolean keepTogether) {
    PdfPTable table = new PdfPTable(getNrColumns());
    table.setWidthPercentage(100);
    table.getDefaultCell().setPadding(3);
    table.getDefaultCell().setBorderWidth(0);
    table.setSplitRows(false);
    table.setKeepTogether(keepTogether);

    boolean asc = (ordCol == 0 || iAsc == null || iAsc.length <= Math.abs(ordCol) - 1 ? true
            : iAsc[Math.abs(ordCol) - 1]);
    if (ordCol < 0)
        asc = !asc;

    widths = new float[iColumns];
    for (int i = 0; i < iColumns; i++)
        widths[i] = 0f;

    String lastLine[] = new String[Math.max(iColumns, (iHeaders == null ? 0 : iHeaders.length))];

    if (iHeaders != null) {
        for (int i = 0; i < iColumns; i++) {
            if (isFiltered(i))
                continue;
            PdfPCell c = createCell();
            c.setBorderWidthBottom(1);
            float width = addText(c, iHeaders[i] == null ? "" : iHeaders[i], true);
            widths[i] = Math.max(widths[i], width);
            String align = (iAlign != null ? iAlign[i] : "left");
            if ("left".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_LEFT);
            if ("right".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_RIGHT);
            if ("center".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c);
        }
        table.setHeaderRows(1);
    }
    if (ordCol != 0) {
        Collections.sort(iLines, new WebTableComparator(Math.abs(ordCol) - 1, asc));
    }
    for (int el = 0; el < iLines.size(); el++) {
        WebTableLine wtline = (WebTableLine) iLines.elementAt(el);
        String[] line = wtline.getLine();
        boolean blank = iBlankWhenSame;
        for (int i = 0; i < iColumns; i++) {
            if (isFiltered(i))
                continue;
            if (blank && line[i] != null && !line[i].equals(lastLine[i]))
                blank = false;
            PdfPCell c = createCell();
            float width = addText(c, blank || line[i] == null ? "" : line[i], false);
            widths[i] = Math.max(widths[i], width);
            String align = (iAlign != null ? iAlign[i] : "left");
            if ("left".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_LEFT);
            if ("right".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_RIGHT);
            if ("center".equals(align))
                c.setHorizontalAlignment(Element.ALIGN_CENTER);
            applyPdfStyle(c, wtline, (el + 1 < iLines.size() ? (WebTableLine) iLines.elementAt(el + 1) : null),
                    ordCol);
            table.addCell(c);
            lastLine[i] = line[i];
        }
    }

    try {
        if (getNrFilteredColumns() < 0) {
            table.setWidths(widths);
        } else {
            float[] x = new float[getNrColumns()];
            int idx = 0;
            for (int i = 0; i < iColumns; i++) {
                if (isFiltered(i))
                    continue;
                x[idx++] = widths[i];
            }
            table.setWidths(x);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return table;
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

public PdfPCell createCell() {
    PdfPCell cell = new PdfPCell();
    cell.setBorderColor(sBorderColor);/*  www  .jav a2 s  .com*/
    cell.setPadding(3);
    cell.setBorderWidth(0);
    cell.setVerticalAlignment(Element.ALIGN_TOP);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorderWidthTop(1);
    cell.setBorderWidthBottom(1);
    cell.setBorderWidthLeft(1);
    cell.setBorderWidthRight(1);
    return cell;
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

public PdfPCell createCellNoBorder() {
    PdfPCell cell = new PdfPCell();
    cell.setBorderColor(sBorderColor);//from www. ja v a  2s .c  om
    cell.setPadding(3);
    cell.setBorderWidth(0);
    cell.setVerticalAlignment(Element.ALIGN_TOP);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    return cell;
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

public void addText(PdfPCell cell, String text, boolean bold) {
    if (text == null)
        return;// w  w w .  jav  a  2s.c o m
    if (text.indexOf("<span") >= 0)
        text = text.replaceAll("</span>", "").replaceAll("<span .*>", "");
    text = text.replaceAll("<br>", "\n");
    text = text.replaceAll("<BR>", "\n");
    if (cell.getPhrase() == null) {
        cell.setPhrase(new Paragraph(text, PdfFont.getFont(bold)));
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } else {
        cell.getPhrase().add(new Chunk("\n" + text, PdfFont.getFont(bold)));
    }
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

private void addLegendRow(String color, String text) {
    PdfPCell c = createCellNoBorder();
    c.setBorderWidth(1);/*w ww . ja v a2  s . co  m*/
    c.setBackgroundColor(getColor(color));
    iPdfTable.addCell(c);
    c = createCellNoBorder();
    addText(c, "  " + text);
    c.setHorizontalAlignment(Element.ALIGN_LEFT);
    iPdfTable.addCell(c);
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

public void printLegend() throws Exception {
    iPdfTable = new PdfPTable(2);
    iPdfTable.setWidths(new float[] { 10f, 200f });
    iPdfTable.getDefaultCell().setPadding(3);
    iPdfTable.getDefaultCell().setBorderWidth(1);
    iPdfTable.setHorizontalAlignment(Element.ALIGN_LEFT);
    iPdfTable.setSplitRows(false);/*from w  ww.j a v a2 s .  c o  m*/
    iPdfTable.setSpacingBefore(10);
    iPdfTable.setKeepTogether(true);

    if (iForm.getBackground() != sBgNone) {
        PdfPCell c = createCellNoBorder();
        c.setColspan(2);
        addText(c, "Assigned examinations:");
        c.setHorizontalAlignment(Element.ALIGN_LEFT);
        iPdfTable.addCell(c);
    }
    if (iForm.getBackground() == sBgPeriodPref) {
        addLegendRow(pref2color(PreferenceLevel.sRequired), "Required period");
        addLegendRow(pref2color(PreferenceLevel.sStronglyPreferred), "Strongly preferred period");
        addLegendRow(pref2color(PreferenceLevel.sPreferred), "Preferred period");
        addLegendRow(pref2color(PreferenceLevel.sNeutral), "No period preference");
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "Discouraged period");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged), "Strongly discouraged period");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "Prohibited period");
    } else if (iForm.getBackground() == sBgRoomPref) {
        addLegendRow(pref2color(PreferenceLevel.sRequired), "Required room");
        addLegendRow(pref2color(PreferenceLevel.sStronglyPreferred), "Strongly preferred room");
        addLegendRow(pref2color(PreferenceLevel.sPreferred), "Preferred room");
        addLegendRow(pref2color(PreferenceLevel.sNeutral), "No room preference");
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "Discouraged room");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged), "Strongly discouraged room");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "Prohibited room");
    } else if (iForm.getBackground() == sBgInstructorConfs) {
        addLegendRow(pref2color(PreferenceLevel.sNeutral), "No instructor conflict");
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "One or more instructor back-to-back conflicts");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged),
                "One or more instructor three or more exams a day conflicts");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "One or more instructor direct conflicts");
    } else if (iForm.getBackground() == sBgStudentConfs) {
        addLegendRow(pref2color(PreferenceLevel.sNeutral), "No student conflict");
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "One or more student back-to-back conflicts");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged),
                "One or more student three or more exams a day student conflicts");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "One or more student direct conflicts");
    } else if (iForm.getBackground() == sBgDirectInstructorConfs) {
        for (int nrConflicts = 0; nrConflicts <= 6; nrConflicts++) {
            String color = lessConflicts2color(nrConflicts);
            addLegendRow(color, "" + nrConflicts + " " + (nrConflicts == 6 ? "or more " : "")
                    + "instructor direct conflicts");
        }
    } else if (iForm.getBackground() == sBgMoreThanTwoADayInstructorConfs) {
        for (int nrConflicts = 0; nrConflicts <= 15; nrConflicts++) {
            String color = conflicts2color(nrConflicts);
            addLegendRow(color, "" + nrConflicts + " " + (nrConflicts == 15 ? "or more " : "")
                    + "instructor more than two exams a day conflicts");
        }
    } else if (iForm.getBackground() == sBgBackToBackInstructorConfs) {
        for (int nrConflicts = 0; nrConflicts <= 15; nrConflicts++) {
            String color = conflicts2color(nrConflicts);
            addLegendRow(color, "" + nrConflicts + " " + (nrConflicts == 15 ? "or more " : "")
                    + "instructor back to back conflicts");
        }
    } else if (iForm.getBackground() == sBgDirectStudentConfs) {
        for (int nrConflicts = 0; nrConflicts <= 6; nrConflicts++) {
            String color = lessConflicts2color(nrConflicts);
            addLegendRow(color,
                    "" + nrConflicts + " " + (nrConflicts == 6 ? "or more " : "") + "student direct conflicts");
        }
    } else if (iForm.getBackground() == sBgMoreThanTwoADayStudentConfs) {
        for (int nrConflicts = 0; nrConflicts <= 15; nrConflicts++) {
            String color = conflicts2color(nrConflicts);
            addLegendRow(color, "" + nrConflicts + " " + (nrConflicts == 15 ? "or more " : "")
                    + "student more than two exams a day conflicts");
        }
    } else if (iForm.getBackground() == sBgBackToBackStudentConfs) {
        for (int nrConflicts = 0; nrConflicts <= 15; nrConflicts++) {
            String color = conflicts2color(nrConflicts);
            addLegendRow(color, "" + nrConflicts + " " + (nrConflicts == 15 ? "or more " : "")
                    + "student back to back conflicts");
        }
    } else if (iForm.getBackground() == sBgDistPref) {
        addLegendRow(pref2color(PreferenceLevel.sNeutral), "No violated constraint<i>(distance=0)</i>");
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "Discouraged/preferred constraint violated");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged),
                "Strongly discouraged/preferred constraint violated</i>");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "Required/prohibited constraint violated</i>");
    }
    PdfPCell c = createCellNoBorder();
    c.setColspan(2);
    addText(c, "Free times:");
    c.setHorizontalAlignment(Element.ALIGN_LEFT);
    iPdfTable.addCell(c);
    addLegendRow(sBgColorNotAvailable, "Period not available");
    if (iForm.getBgPreferences() && iForm.getBackground() == sBgPeriodPref) {
        addLegendRow(pref2color(PreferenceLevel.sStronglyPreferred), "Strongly preferred period");
        addLegendRow(pref2color(PreferenceLevel.sPreferred), "Preferred period");
    }
    addLegendRow(pref2color(PreferenceLevel.sNeutral), "No period preference");
    if (iForm.getBgPreferences() && iForm.getBackground() == sBgPeriodPref) {
        addLegendRow(pref2color(PreferenceLevel.sDiscouraged), "Discouraged period");
        addLegendRow(pref2color(PreferenceLevel.sStronglyDiscouraged), "Strongly discouraged period");
        addLegendRow(pref2color(PreferenceLevel.sProhibited), "Prohibited period");
    }

    iDocument.add(iPdfTable);
}

From source file:org.unitime.timetable.webutil.timegrid.PdfTimetableGridTable.java

License:Open Source License

public void addText(PdfPCell cell, String text, boolean bold) {
    if (text == null)
        return;//from   w  w  w . ja  v  a 2  s  . com
    if (text.indexOf("<span") >= 0)
        text = text.replaceAll("</span>", "").replaceAll("<span .*>", "");
    if (cell.getPhrase() == null) {
        cell.setPhrase(new Paragraph(text, PdfFont.getSmallFont(bold)));
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } else {
        cell.getPhrase().add(new Chunk("\n" + text, PdfFont.getSmallFont(bold)));
    }
}