Example usage for com.itextpdf.text Paragraph add

List of usage examples for com.itextpdf.text Paragraph add

Introduction

In this page you can find the example usage for com.itextpdf.text Paragraph add.

Prototype

@Override
public boolean add(Element o) 

Source Link

Document

Adds an Element to the Paragraph.

Usage

From source file:Controllers.ExportController.java

public static void exportExam(String path) {
    model = (DefaultTableModel) tableExam.getModel();
    int selectRow = tableExam.getSelectedRow();

    if (selectRow != -1) {
        int idExam = (int) model.getValueAt(selectRow, 0) - 1;
        exams = ExamModel.readExam();/* ww w .j  ava 2 s.c  o m*/
        Exam ex = exams.getExam(idExam);

        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(path));
            document.open();
            BaseFont f = BaseFont.createFont("/font/vuArial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font titleExamFont = new Font(f, 25.0f, Font.BOLD);
            Font titlePartFont = new Font(f, 18.0f, Font.BOLD);
            Font headFont = new Font(f, 13.0f, Font.BOLD);
            Font suggestionFont = new Font(f, 13.0f, Font.ITALIC);
            Font contentFont = new Font(f, 13.0f, Font.NORMAL);
            Paragraph align = new Paragraph(" ");
            Paragraph title = new Paragraph(ex.getNameExam(), titleExamFont);
            title.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(title);
            document.add(align);

            ArrayList<Question> question = ex.getQuestions();

            boolean haveMultipleChoice = false;
            boolean haveEssay = false;
            // Kim tra xem c phn t lun khng
            for (Question q : question) {
                if (q instanceof Essay) {
                    haveEssay = true;
                }
                // Kim tra xem c phn trc nghim khng}
                else {
                    haveMultipleChoice = true;
                }
                if (haveEssay && haveMultipleChoice)
                    break;
            }

            int count;
            if (haveMultipleChoice) {
                count = 0;
                Paragraph titlePart = new Paragraph("Trc nghim", titlePartFont);
                document.add(align);
                document.add(titlePart);
                document.add(align);
                for (Question q : question)
                    if (q instanceof MultipleChoice) {
                        count++;
                        Phrase numberQuestion = new Phrase("Cu " + count + ": ", headFont);
                        Phrase contentQuestion = new Phrase(q.getContentQuestion(), contentFont);
                        Paragraph questionParagraph = new Paragraph();
                        questionParagraph.add(numberQuestion);
                        questionParagraph.add(contentQuestion);
                        document.add(questionParagraph);

                        MultipleChoice mc = (MultipleChoice) q;
                        ArrayList<Answer> answers = mc.getAnswers();

                        boolean ok = true;
                        for (int i = 0; i < answers.size(); ++i) {
                            Answer answer = answers.get(i);
                            if (answer.getContentAnswer().length() > 30)
                                ok = false;
                        }

                        if (ok == true) {
                            PdfPTable table = new PdfPTable(2);
                            for (int i = 0; i < answers.size(); ++i) {
                                Answer answer = answers.get(i);
                                PdfPCell answerParagraph = new PdfPCell(new Paragraph(
                                        (char) (65 + i) + ". " + answer.getContentAnswer(), contentFont));
                                answerParagraph.setBorder(Rectangle.NO_BORDER);
                                table.addCell(answerParagraph);
                            }
                            document.add(table);
                        } else {
                            PdfPTable table = new PdfPTable(1);
                            for (int i = 0; i < answers.size(); ++i) {
                                Answer answer = answers.get(i);
                                PdfPCell answerParagraph = new PdfPCell(new Paragraph(
                                        (char) (65 + i) + ". " + answer.getContentAnswer(), contentFont));
                                answerParagraph.setBorder(Rectangle.NO_BORDER);
                                table.addCell(answerParagraph);
                            }
                            document.add(table);
                        }
                    }
            }

            if (haveEssay) {
                count = 0;
                Paragraph titlePart = new Paragraph("T Lun", titlePartFont);
                document.add(align);
                document.add(titlePart);
                document.add(align);
                for (Question q : question)
                    if (q instanceof Essay) {
                        count++;
                        Phrase numberQuestion = new Phrase("Cu " + count + ": ", headFont);
                        Phrase contentQuestion = new Phrase(q.getContentQuestion(), contentFont);
                        Paragraph questionParagraph = new Paragraph();
                        questionParagraph.add(numberQuestion);
                        questionParagraph.add(contentQuestion);

                        Essay es = (Essay) q;
                        Phrase headerSuggestion = new Phrase("Gi : ", suggestionFont);
                        Phrase contentSuggestion = new Phrase(es.getSuggest(), contentFont);
                        Paragraph suggestion = new Paragraph();
                        suggestion.add(headerSuggestion);
                        suggestion.add(contentSuggestion);
                        document.add(questionParagraph);
                        document.add(suggestion);
                    }
            }

            document.close();
        } catch (FileNotFoundException exp) {
            exp.printStackTrace();
        } catch (DocumentException exp) {
            exp.printStackTrace();
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
}

From source file:crearpdf.CrearPDF.java

private void crearReportePDForacle(String select) throws Exception {

    Class.forName("oracle.jdbc.OracleDriver"); //Invocamos el Driver de Oracle
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "willson"/*usuario*/,
            "123456"/*contrasea*/); //Creamos la conexion para acceder a la base de datos
    Statement stmt = con.createStatement();
    ResultSet resultados = stmt.executeQuery(select); //Hacemos el select de la info que extraeremos de la base de datos
    Document reportePDF = new Document(PageSize.LETTER, 88, 88, 80, 15); //Creamos un nuevo Documento usando la libreria itextpdf importada.
    PdfWriter.getInstance(reportePDF, new FileOutputStream("REPORTE.pdf"));//Instanciamos y creamos el archivo
    reportePDF.open(); //Abrimos el archivo creado arriba.

    Paragraph parrafo = new Paragraph("Reporte del dia");
    parrafo.add("Este es el repote que nos da los datos");
    parrafo.setAlignment(Element.ALIGN_CENTER);

    PdfPTable Treportes = new PdfPTable(5);//Aqui asignamos el numero de columnas que tendra la tabla, en este casi 5.
    PdfPCell celdas_tabla;//Creamos las celdas que seran llenadas con los datos extraidos

    //Agregamos los titulos de cada Columna                   
    Treportes.addCell("ID");
    Treportes.addCell("Nombre");
    Treportes.addCell("Edad");
    Treportes.addCell("Mes Nacimiento");
    Treportes.addCell("Universidad");

    //ciclo para empezar a aadir los datos de la base de datos a las celdas correspondientes en el PDF
    ///System.out.println("Llenando pdf");
    while (resultados.next()) {

        String id = resultados.getString("ID");
        celdas_tabla = new PdfPCell(new Phrase(id));
        Treportes.addCell(celdas_tabla);
        String nombre = resultados.getString("NOMBRE");
        celdas_tabla = new PdfPCell(new Phrase(nombre));
        Treportes.addCell(celdas_tabla);
        String edad = resultados.getString("EDAD");
        celdas_tabla = new PdfPCell(new Phrase(edad));
        Treportes.addCell(celdas_tabla);
        String nacimiento = resultados.getString("MES_NACIMIENTO");
        celdas_tabla = new PdfPCell(new Phrase(nacimiento));
        Treportes.addCell(celdas_tabla);
        String universidad = resultados.getString("UNIVERSIDAD");
        celdas_tabla = new PdfPCell(new Phrase(universidad));
        Treportes.addCell(celdas_tabla);
        //System.out.println(id+" "+nombre+" "+edad);
    }/*from ww  w .j ava2  s .  c  o m*/
    // System.out.println("Listo!");
    parrafo.add(Treportes);
    reportePDF.add(parrafo);

    //reportePDF.add(Treportes); //Insertamos los datos de la tabla en el PDF.                      
    reportePDF.close(); //Cerramos el archivo PDF una vez completado

    //Cerrar todas las conexiones
    resultados.close();
    stmt.close();
    con.close();

}

From source file:crs.Manager_DailyReturnController.java

@FXML
private void onDownload(ActionEvent event) {
    if (report_datepicker.getValue() == null)
        return;/*from w  ww  .  j ava2s. co m*/

    String date = report_datepicker.getValue().toString();

    Vlist = CRS.getDailyReturn(date, -1);
    bdata = getDailyReturnByBranch(date);
    cdata = getReturnByCategory(date);

    String filename = report_datepicker.getValue().toString() + "_DailyRental.pdf";
    DirectoryChooserBuilder builder = DirectoryChooserBuilder.create();
    builder.title("Hello World");
    String cwd = System.getProperty("user.dir");
    File file = new File(cwd);
    builder.initialDirectory(file);
    DirectoryChooser chooser = builder.build();
    File chosenDir = chooser.showDialog(null);
    if (chosenDir != null) {
        System.out.println(chosenDir.getAbsolutePath());
    } else {
        System.out.print("no directory chosen");
    }

    Document document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(chosenDir.getAbsolutePath() + "/" + filename));

        document.open();
        Paragraph paragraph = new Paragraph();
        paragraph.add("Daily Return Report of " + report_datepicker.getValue().toString());
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingAfter(40);
        document.add(paragraph);
        document.add(createPDFTableVehicle(Vlist));
        paragraph.clear();
        paragraph.add("  ");
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByCategory(cdata));

        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByBranch(bdata));
        document.close();
        label_result.setText("Download successfully");
        label_result.setVisible(true);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crs.Manager_Report_bybranchController.java

@FXML
private void onDownload(ActionEvent event) {
    if (report_datepicker.getValue() == null || combo_loc.getValue() == null)
        return;//from  w ww.j a  v a2  s  .com
    String date = report_datepicker.getValue().toString();
    bid = getBranchid(combo_loc.getValue().toString());
    Vlist = CRS.getDailyRental(date, bid);
    cdata = getRentalByCategory(date);
    String filename = report_datepicker.getValue().toString() + "_DailyRental_"
            + combo_loc.getValue().toString() + ".pdf";
    DirectoryChooserBuilder builder = DirectoryChooserBuilder.create();
    builder.title("Hello World");
    String cwd = System.getProperty("user.dir");
    File file = new File(cwd);
    builder.initialDirectory(file);
    DirectoryChooser chooser = builder.build();
    File chosenDir = chooser.showDialog(null);
    if (chosenDir != null) {
        System.out.println(chosenDir.getAbsolutePath());
    } else {
        System.out.print("no directory chosen");
    }

    Document document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(chosenDir.getAbsolutePath() + "/" + filename));

        document.open();
        Paragraph paragraph = new Paragraph();
        paragraph.add("Daily Report of " + report_datepicker.getValue().toString() + "  location: "
                + combo_loc.getValue().toString());
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingAfter(40);
        document.add(paragraph);
        document.add(createPDFTableVehicle(Vlist));
        paragraph.clear();
        paragraph.add("  ");
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByCategory(cdata));

        document.close();
        label_result.setText("Download successfully");
        label_result.setVisible(true);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crs.Manager_Report_DailyController.java

@FXML
private void onDownload(ActionEvent event) {
    if (report_datepicker.getValue() == null)
        return;//w w w.j a v a 2 s  .c  o m
    String filename = report_datepicker.getValue().toString() + "_DailyRental.pdf";
    DirectoryChooserBuilder builder = DirectoryChooserBuilder.create();
    builder.title("Hello World");
    String cwd = System.getProperty("user.dir");
    File file = new File(cwd);
    builder.initialDirectory(file);
    DirectoryChooser chooser = builder.build();
    File chosenDir = chooser.showDialog(null);
    if (chosenDir != null) {
        System.out.println(chosenDir.getAbsolutePath());
    } else {
        System.out.print("no directory chosen");
    }

    Document document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(chosenDir.getAbsolutePath() + "/" + filename));

        document.open();
        Paragraph paragraph = new Paragraph();
        paragraph.add("Daily Report of " + report_datepicker.getValue().toString());
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingAfter(40);
        document.add(paragraph);
        document.add(createPDFTableVehicle(Vlist));
        paragraph.clear();
        paragraph.add("  ");
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByCategory(cdata));

        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByBranch(bdata));
        document.close();
        label_result.setText("Download successfully");
        label_result.setVisible(true);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crs.Manager_ReturnbybranchController.java

@FXML
private void onDownload(ActionEvent event) {
    if (report_datepicker.getValue() == null || combo_loc.getValue() == null)
        return;/* w w w . j  a  v  a 2  s .c o m*/
    String date = report_datepicker.getValue().toString();
    bid = getBranchid(combo_loc.getValue().toString());
    Vlist = CRS.getDailyReturn(date, bid);
    cdata = getReturnByCategory(date);
    String filename = report_datepicker.getValue().toString() + "_DailyRental_"
            + combo_loc.getValue().toString() + ".pdf";
    DirectoryChooserBuilder builder = DirectoryChooserBuilder.create();
    builder.title("Hello World");
    String cwd = System.getProperty("user.dir");
    File file = new File(cwd);
    builder.initialDirectory(file);
    DirectoryChooser chooser = builder.build();
    File chosenDir = chooser.showDialog(null);
    if (chosenDir != null) {
        System.out.println(chosenDir.getAbsolutePath());
    } else {
        System.out.print("no directory chosen");
    }

    Document document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(chosenDir.getAbsolutePath() + "/" + filename));

        document.open();
        Paragraph paragraph = new Paragraph();
        paragraph.add("Daily Report of " + report_datepicker.getValue().toString() + "  location: "
                + combo_loc.getValue().toString());
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingAfter(40);
        document.add(paragraph);
        document.add(createPDFTableVehicle(Vlist));
        paragraph.clear();
        paragraph.add("  ");
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        document.add(createPDFTableByCategory(cdata));

        document.close();
        label_result.setText("Download successfully");
        label_result.setVisible(true);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Manager_ReportController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

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

License:Apache License

public Paragraph setHeader(Document document, String title) throws IOException, BadElementException {
    Paragraph paragraph = new Paragraph(title,
            FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, BaseColor.BLACK));
    paragraph.setAlignment(Element.ALIGN_CENTER);

    Image img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + HEADERIMG));
    float topMargin = img.getHeight() + 2 * PADDING[0];
    img.setAbsolutePosition(PADDING[3], (PageSize.A4.getHeight() - img.getHeight()) - PADDING[0]);
    paragraph.add(img);
    img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + FOOTERIMG));
    img.setAbsolutePosition((PageSize.A4.getWidth() - img.getWidth()) / 2, PADDING[2]);
    paragraph.add(img);//  w  w w  .ja v  a  2  s. co m
    float bottomMargin = img.getHeight() + 2 * PADDING[2];

    document.setMargins(topMargin, PADDING[1], bottomMargin, PADDING[3]);

    paragraph.setSpacingAfter(1.5f * PADDING[0]);

    return paragraph;
}

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

License:Open Source License

/**
 * Erstellt die Ueberschrift//from   ww w.ja  va 2s .co m
 *
 * @param header
 *         Uebeschrift
 * @throws DocumentException
 *         Wenn das pdf nicht erstellt werden kann
 */
public void addDocumentHeader(@NonNull String header) throws DocumentException {
    Paragraph preface = new Paragraph();
    addEmptyLine(preface, 1);
    preface.add(new Paragraph(header, smallBold));
    mDocument.add(preface);
}

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

License:Open Source License

/**
 * Fuegt einen Titel zum Document hinzu/*from   www.j  a  v a 2s  .  c o  m*/
 *
 * @param titel
 *         Titel des Documents
 * @throws DocumentException
 *         Wenn das pdf nicht erstellt werden kann
 */
public void addDocumentTitle(String titel) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);
    // Lets write a big header
    preface.add(new Paragraph(titel, catFont));
    addEmptyLine(preface, 1);
    mDocument.add(preface);
}

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

License:Open Source License

/**
 * Fuegt leere Zeilen hinzu/*from   w  w w .j  a v a  2  s.  c o  m*/
 *
 * @param paragraph
 *         Paragraph
 * @param number
 *         Anzahl der leeren Zeilen
 */
public void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
        paragraph.add(new Paragraph(" "));
    }
}