Example usage for com.itextpdf.text.pdf PdfPCell setHorizontalAlignment

List of usage examples for com.itextpdf.text.pdf PdfPCell setHorizontalAlignment

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPCell setHorizontalAlignment.

Prototype

public void setHorizontalAlignment(int horizontalAlignment) 

Source Link

Document

Sets the horizontal alignment for the cell.

Usage

From source file:CTD.planer2.util.ExportToPdf.java

/**
 * We create the table we want to add to our pdf
 * //from www.  ja  va2s .  co m
 * @param subCatPart
 * @throws BadElementException
 */
private static void createTable(Section subCatPart) throws BadElementException {

    // Get the number of days per Week from the Settings
    int daysPerWeek = App.theSettings.getDaysPerWeek();

    PdfPTable table = new PdfPTable(daysPerWeek + 1);

    String[] WeekDay = { "Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag",
            "Sonntag" };

    for (int i = 0; i < daysPerWeek + 1; i++) {
        PdfPCell c1 = new PdfPCell(new Phrase(WeekDay[i]));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        if (i == daysPerWeek) {
            table.setHeaderRows(1);
        }
    }

    // We take the first day to get the number of units and so the number of
    // times we have to run this.
    for (int j = 0; j < planExport.getWeekday(1).size(); j++) {
        // For each day available, we run this loop once.
        for (int k = 1; k < daysPerWeek + 1; k++) {
            // Read one unit per day and write it in a new cell of the pdf
            List<Units> tmp = planExport.getWeekday(k);
            // If it's the first day, we also want a first cell per line
            // containing the time this unit starts
            if (k == 1) {
                SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                String time = df.format(tmp.get(j).getTime());
                table.addCell(time);
                String room = " ";
                // Check whether a room is given
                if (tmp.get(j).getRooms().size() < 1) {
                    room = " ";
                } else {
                    room = tmp.get(j).getRooms().get(0).toString();
                }
                String teacher = " ";
                if (tmp.get(j).getPerson() == null) {
                    teacher = " ";
                } else {
                    teacher = tmp.get(j).toString();
                }
                String subject = " ";
                if (tmp.get(j).getSubjects().size() < 1) {
                    subject = " ";
                } else {
                    subject = tmp.get(j).getSubjects().get(0).toString();
                }
                // If the plan is for a room, we just need the time and the
                // teachers name
                if (CPlaner.selRoom != null) {
                    table.addCell(teacher);
                    // If the plan is for a class, we need the time, the
                    // room and the teachers name
                } else if (CPlaner.selClass != null) {
                    table.addCell(room + ", " + teacher);
                    // For a teachers plan, we need the room and the subject
                } else {
                    table.addCell(room + ", " + subject);
                }
            } else {
                // If it's not the first day, we don't need to print the
                // start time again
                String room = " ";
                // Check again whether a room is given
                if (tmp.get(j).getRooms().size() < 1) {
                    room = " ";
                } else {
                    room = tmp.get(j).getRooms().get(0).toString();
                }
                String teacher = " ";
                if (tmp.get(j).getPerson() == null) {
                    teacher = " ";
                } else {
                    teacher = tmp.get(j).toString();
                }
                String subject = " ";
                if (tmp.get(j).getSubjects().size() < 1) {
                    subject = " ";
                } else {
                    subject = tmp.get(j).getSubjects().get(0).toString();
                }
                // If the plan is for a room, we just need the time and the
                // teachers name
                if (CPlaner.selRoom != null) {
                    table.addCell(teacher);
                    // If the plan is for a class, we need the time, the
                    // room and the teachers name
                } else if (CPlaner.selClass != null) {
                    table.addCell(room + ", " + teacher);
                    // For a teachers plan, we need the room and the subject
                } else {
                    table.addCell(room + ", " + subject);
                }
            }
        }
    }

    // Add the whole table we've just created to the pdf file
    subCatPart.add(table);

}

From source file:CTD.planer2.util.ExportToPdf.java

/**
 * We create the table we want to add to our pdf
 * //from  www  .  j a  va  2  s.c om
 * @param subCatPart
 * @throws BadElementException
 */
private static void createTable(Section subCatPart, Room room) throws BadElementException {
    // Get the number of days per Week from the Settings
    int daysPerWeek = App.theSettings.getDaysPerWeek();

    PdfPTable table = new PdfPTable(daysPerWeek + 1);

    String[] WeekDay = { "Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag",
            "Sonntag" };

    for (int i = 0; i < daysPerWeek + 1; i++) {
        PdfPCell c1 = new PdfPCell(new Phrase(WeekDay[i]));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        if (i == daysPerWeek) {
            table.setHeaderRows(1);
        }
    }
    double calc = 0;
    // Simplify the Date since we just need the time
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    Date start = null;
    Date uSize = null;
    try {
        start = df.parse(App.theSettings.getStartingTimeH() + ":" + App.theSettings.getStartingTimeM());
        uSize = new Date(MINUTES.toMillis(App.theSettings.getUnitsSize()));
    } catch (ParseException e) {
    }
    // We take the first day to get the number of units and so the number of
    // times we have to run this.
    for (int j = 0; j < Tools.calcRows(); j++) {
        calc = j == 0 ? start.getTime() : (calc + (uSize.getTime()));

        // For each day available, we run this loop once.
        for (int k = 1; k < daysPerWeek + 1; k++) {
            Units u = room.getUnitByRowCol(k, j);
            // If it's the first day, we also want a first cell per line
            // containing the time this unit starts
            if (k == 1) {
                table.addCell(df.format(calc));
                // Check whether a room is given
                String teacher = " ";
                if (u != null) {
                    teacher = u.toString();
                }
                table.addCell(teacher);
            } else {
                // If it's not the first day, we don't need to print the
                // start time again
                String teacher = " ";
                if (u != null) {
                    teacher = u.toString();
                }
                table.addCell(teacher);
            }
        }
    }

    // Add the whole table we've just created to the pdf file
    subCatPart.add(table);

}

From source file:CTD.planer2.util.ExportToPdf.java

private static void createDayTable(Section subCatPart) throws BadElementException {

    List<Person> personList = App.theSemester.getPersonList();

    PdfPTable table = new PdfPTable(personList.size() + 1);

    PdfPCell c1 = new PdfPCell(new Phrase(" "));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);// www. j a v  a  2  s  .  co m

    for (Person p : personList) {

        c1 = new PdfPCell(new Phrase(p.toString()));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        table.setHeaderRows(1);
        int k = 1;
        // We take the first person to get the number of units and so the
        // number
        // of
        // times we have to run this.

        for (int j = 1; j < p.getEntity().size(); j++) {
            // For each person available, we run this loop once.

            List<Units> tmp = p.getEntity();

            if (k == 1) {
                SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                String time = df.format(tmp.get(j).getTime());
                table.addCell(time);
                String classes = " ";
                // Check whether a class is given
                if (tmp.get(j).getClasses().size() < 1) {
                    classes = " ";
                } else {
                    classes = tmp.get(j).getClasses().get(0).toString();
                }
                table.addCell(classes);
            } else {
                // If it's not the first day, we don't need to print the
                // start time again
                String classes = " ";
                // Check whether a class is given
                if (tmp.get(j).getClasses().size() < 1) {
                    classes = " ";
                } else {
                    classes = tmp.get(j).getClasses().get(0).toString();
                }
                table.addCell(classes);
            }
            k = k + 1;
        }

    }

    // Add the whole table we've just created to the pdf file
    subCatPart.add(table);

}

From source file:cz.zcu.kiv.eegdatabase.logic.pdf.ReservationPDF.java

License:Apache License

public static PdfPTable formatReservation(Reservation reservation) {
    int padding = 5;

    GregorianCalendar created = new GregorianCalendar();
    created.setTime(reservation.getCreationTime());
    GregorianCalendar startTime = new GregorianCalendar();
    startTime.setTime(reservation.getStartTime());
    GregorianCalendar endTime = new GregorianCalendar();
    endTime.setTime(reservation.getEndTime());

    String personName = BookingRoomUtils.formatPersonName(reservation.getPerson());

    Font title = FontFactory.getFont("Trebuchet MS", "utf-8", 15, Font.BOLD, new BaseColor(59, 70, 00));
    Font header = FontFactory.getFont(FontFactory.TIMES_BOLD, 13, Font.BOLD, BaseColor.BLACK);
    Font value = FontFactory.getFont(FontFactory.TIMES, 13);

    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;
    Phrase phrase;//  ww w.ja v  a 2  s.  c  om

    phrase = new Phrase("Reservation by " + personName);
    phrase.setFont(title);
    cell = new PdfPCell(phrase);
    cell.setColspan(2);
    cell.setBorder(0);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorderWidthTop(1);
    cell.setBorderWidthLeft(1);
    cell.setBorderWidthRight(1);
    cell.setPadding(padding);
    cell.setPaddingBottom(5);
    table.addCell(cell);

    phrase = new Phrase("Date: ");
    phrase.setFont(header);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthLeft(1);
    cell.setPadding(padding);
    table.addCell(cell);

    phrase = new Phrase(BookingRoomUtils.getDate(startTime));
    phrase.setFont(value);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthRight(1);
    cell.setPadding(padding);
    table.addCell(cell);

    phrase = new Phrase("Start: ");
    phrase.setFont(header);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthLeft(1);
    cell.setPadding(padding);
    table.addCell(cell);

    phrase = new Phrase(BookingRoomUtils.getHoursAndMinutes(startTime));
    phrase.setFont(value);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthRight(1);
    cell.setPadding(padding);
    table.addCell(cell);

    phrase = new Phrase("End: ");
    phrase.setFont(header);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthLeft(1);
    cell.setBorderWidthBottom(1);
    cell.setPadding(padding);
    table.addCell(cell);

    phrase = new Phrase(BookingRoomUtils.getHoursAndMinutes(endTime));
    phrase.setFont(value);
    cell = new PdfPCell(phrase);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setBorderWidthRight(1);
    cell.setBorderWidthBottom(1);
    cell.setPadding(padding);
    table.addCell(cell);

    table.setSpacingBefore(10);
    table.setSpacingAfter(10);

    return table;
}

From source file:dbedit.actions.ExportPdfAction.java

License:Open Source License

@Override
protected void performThreaded(ActionEvent e) throws Exception {
    boolean selection = false;
    JTable table = ResultSetTable.getInstance();
    if (table.getSelectedRowCount() > 0 && table.getSelectedRowCount() != table.getRowCount()) {
        Object option = Dialog.show("PDF", "Export", Dialog.QUESTION_MESSAGE,
                new Object[] { "Everything", "Selection" }, "Everything");
        if (option == null || "-1".equals(option.toString())) {
            return;
        }/* w  ww . jav a 2 s  . c o m*/
        selection = "Selection".equals(option);
    }
    List list = ((DefaultTableModel) table.getModel()).getDataVector();
    int columnCount = table.getColumnCount();
    PdfPTable pdfPTable = new PdfPTable(columnCount);
    pdfPTable.setWidthPercentage(100);
    pdfPTable.getDefaultCell().setPaddingBottom(4);
    int[] widths = new int[columnCount];

    // Row Header
    pdfPTable.getDefaultCell().setBorderWidth(2);
    for (int i = 0; i < columnCount; i++) {
        String columnName = table.getColumnName(i);
        pdfPTable.addCell(new Phrase(columnName, ROW_HEADER_FONT));
        widths[i] = Math.min(50000, Math.max(widths[i], ROW_HEADER_BASE_FONT.getWidth(columnName + " ")));
    }
    pdfPTable.getDefaultCell().setBorderWidth(1);
    if (!list.isEmpty()) {
        pdfPTable.setHeaderRows(1);
    }

    // Body
    for (int i = 0; i < list.size(); i++) {
        if (!selection || table.isRowSelected(i)) {
            List record = (List) list.get(i);
            for (int j = 0; j < record.size(); j++) {
                Object o = record.get(j);
                if (o != null) {
                    if (ResultSetTable.isLob(j)) {
                        o = Context.getInstance().getColumnTypeNames()[j];
                    }
                } else {
                    o = "";
                }
                PdfPCell cell = new PdfPCell(new Phrase(o.toString()));
                cell.setPaddingBottom(4);
                if (o instanceof Number) {
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                }
                pdfPTable.addCell(cell);
                widths[j] = Math.min(50000, Math.max(widths[j], BASE_FONT.getWidth(o.toString())));
            }
        }
    }

    // Size
    pdfPTable.setWidths(widths);
    int totalWidth = 0;
    for (int width : widths) {
        totalWidth += width;
    }
    Rectangle pageSize = PageSize.A4.rotate();
    pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
    pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));

    // Document
    Document document = new Document(pageSize);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    pdfTemplate = writer.getDirectContent().createTemplate(100, 100);
    pdfTemplate.setBoundingBox(new Rectangle(-20, -20, 100, 100));
    writer.setPageEvent(this);
    document.add(pdfPTable);
    document.close();
    FileIO.saveAndOpenFile("export.pdf", byteArrayOutputStream.toByteArray());
}

From source file:dbms_prj.Display.java

public void create_pdf() throws Exception {
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(S1 + ".pdf"));
    Image image = Image.getInstance("src/Travel partners.png");
    image.scaleAbsolute(500f, 200f);//from   www .ja va  2s  . c  om
    Image ima = Image.getInstance("src/images.jpg");
    ima.scaleAbsolute(100f, 100f);
    ima.setAlignment(Element.ALIGN_CENTER);
    PdfPTable table = new PdfPTable(9);
    PdfPCell cell = new PdfPCell(new Paragraph("Ticket"));
    cell.setColspan(9);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setPadding(10.0f);
    cell.setBackgroundColor(new BaseColor(140, 221, 8));
    table.addCell(cell);
    table.addCell("PNR");
    table.addCell("BOOK_ID");
    table.addCell("FLIGHT_NO");
    table.addCell("NAME");
    table.addCell("SOURCE");
    table.addCell("DESTINATION");
    table.addCell("STATUS");
    table.addCell("FARE");
    table.addCell("Date");
    table.addCell(S1);
    table.addCell(S11);
    table.addCell(S4);
    table.addCell(S6 + " " + S7);
    table.addCell(S8);
    table.addCell(S9);
    table.addCell(S5);
    table.addCell(S10);
    table.addCell(S12);
    table.setSpacingBefore(30.0f); // Space Before table starts, like margin-top in CSS
    table.setSpacingAfter(30.0f);
    doc.open();
    //doc.add(new Paragraph("Hello!! Yo this is my first pdf!"));
    doc.add(image);
    doc.add(new Paragraph("Ticket created on " + new Date().toString()));
    doc.add(table);
    //doc.add(list);
    doc.add(ima);
    doc.close();
}

From source file:dbms_prj.Status.java

public void create_pdf() throws Exception {
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(S1 + ".pdf"));
    Image image = Image.getInstance("src/Travel partners.png");
    image.scaleAbsolute(500f, 200f);/*from  ww  w  .j a v a 2 s .  co m*/
    Image ima = Image.getInstance("src/images.jpg");
    ima.scaleAbsolute(100f, 100f);
    ima.setAlignment(Element.ALIGN_CENTER);
    PdfPTable table = new PdfPTable(9);
    PdfPCell cell = new PdfPCell(new Paragraph("Ticket"));
    cell.setColspan(9);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setPadding(10.0f);
    table.setTotalWidth(300.0f);

    cell.setBackgroundColor(new BaseColor(140, 221, 8));
    table.addCell(cell);
    table.addCell("PNR");
    table.addCell("BOOK_ID");
    table.addCell("FLIGHT_NO");
    table.addCell("NAME");
    table.addCell("SOURCE");
    table.addCell("DESTINATION");
    table.addCell("STATUS");
    table.addCell("FARE");
    table.addCell("Date");
    table.addCell(S1);
    table.addCell(S11);
    table.addCell(S4);
    table.addCell(S6 + S7);
    table.addCell(S8);
    table.addCell(S9);
    table.addCell(S5);
    table.addCell(S10);
    table.addCell(S2);
    table.setSpacingBefore(50.0f); // Space Before table starts, like margin-top in CSS
    table.setSpacingAfter(50.0f);
    doc.open();
    //doc.add(new Paragraph("Hello!! Yo this is my first pdf!"));
    doc.add(image);
    //doc.add(new Paragraph("Ticket created on " + new Date().toString()));
    doc.add(table);
    //doc.add(list);
    doc.add(ima);
    doc.close();
}

From source file:de.aidger.utils.pdf.ActivityReportConverter.java

License:Open Source License

/**
 * Writes the logos and the address of the institute.
 *//* w ww .  ja  v a  2 s.  co m*/
private void writeLogo() {
    try {
        Font generatedByFont = new Font(
                BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 8);
        Image aidger = Image.getInstance(getClass().getResource("/de/aidger/res/pdf/AidgerLogo.png"));
        aidger.scaleAbsolute(80.0f, 20.0f);
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(reader.getPageSize(1).getRight());
        PdfPCell cell = new PdfPCell(new Phrase(_("Generated by: "), generatedByFont));
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPaddingBottom(5);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        table.addCell(cell);
        cell = new PdfPCell(Image.getInstance(aidger));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(0);
        table.addCell(cell);
        table.writeSelectedRows(0, -1, 0, 25, contentByte);
    } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:de.aw.awlib.pdf.PDFDocument.java

License:Open Source License

/**
 * Erstellt eine Tabelle im pdf/*from  ww  w.j a  v  a 2s .  c  o  m*/
 *
 * @param columnHeaders
 *         Header der Tabellenspalten
 * @param rowCount
 *         Anzahl der Tabellenzeilen
 * @throws DocumentException
 *         wenn das Document nicht erstellt werden kann.
 */
public void createTable(@NonNull String[] columnHeaders, @Nullable String summary, int rowCount,
        CellCreator creator) throws DocumentException {
    if (summary != null) {
        addTableTitle(summary);
    }
    PdfPTable table = new PdfPTable(columnHeaders.length);
    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);
    for (String columnHeader : columnHeaders) {
        PdfPCell c1 = new PdfPCell(new Phrase(columnHeader));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
    }
    table.setHeaderRows(1);
    for (int row = 0; row < rowCount; row++) {
        for (int column = 0; column < columnHeaders.length; column++) {
            table.addCell(creator.getCellContent(row, column));
        }
    }
    mDocument.add(table);
}

From source file:de.aw.awlib.pdf.PDFDocument.java

License:Open Source License

/**
 * Erstellt eine Tabelle im pdf/*from  w w w  . j a  v a  2  s.  c  o m*/
 *
 * @param columnHeaders
 *         Header der Tabellenspalten
 * @param cellcontent
 *         Inhalte der Tabellenzeilen
 * @throws DocumentException
 *         wenn das Document nicht erstellt werden kann.
 */
public void createTable(@NonNull String[] columnHeaders, @NonNull List<List<String>> cellcontent)
        throws DocumentException {
    PdfPTable table = new PdfPTable(columnHeaders.length);
    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);
    for (String columnHeader : columnHeaders) {
        PdfPCell c1 = new PdfPCell(new Phrase(columnHeader));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
    }
    table.setHeaderRows(1);
    for (List<String> cells : cellcontent) {
        for (String cell : cells) {
            table.addCell(cell);
        }
    }
    mDocument.add(table);
}