Example usage for com.itextpdf.text FontFactory getFont

List of usage examples for com.itextpdf.text FontFactory getFont

Introduction

In this page you can find the example usage for com.itextpdf.text FontFactory getFont.

Prototype


public static Font getFont(final String fontname, final float size, final int style) 

Source Link

Document

Constructs a Font-object.

Usage

From source file:com.gp.cong.logisoft.reports.LclExportVoyageNotificationPdf.java

public PdfPTable reasonBlock(LclExportNotiFicationForm lclExportNotiFicationForm)
        throws IOException, BadElementException, DocumentException, Exception {
    User user = new UserDAO().findById(
            lclExportNotiFicationForm.getUserId() != null ? lclExportNotiFicationForm.getUserId().intValue()
                    : 0);//from   ww  w . ja va2  s.  com
    String userName = user != null ? user.getFirstName() : "";
    table = new PdfPTable(2);
    table.setWidthPercentage(100f);
    Font fontArialBold = FontFactory.getFont("Courier", 10f, Font.BOLD);
    Font fontArialNormal = FontFactory.getFont("Courier", 10f, Font.NORMAL);
    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(2);
    p = new Paragraph(9f, "Reason:", fontArialBold);
    cell.addElement(p);
    table.addCell(cell);
    String voyageChangeReason = CommonUtils.isNotEmpty(lclExportNotiFicationForm.getVoyageReason())
            ? lclExportNotiFicationForm.getVoyageReason().toUpperCase()
            : "";
    String voyageComment = CommonUtils.isNotEmpty(lclExportNotiFicationForm.getVoyageComment())
            ? lclExportNotiFicationForm.getVoyageComment().toUpperCase()
            : "";
    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(2);
    p = new Paragraph(9f, "" + voyageChangeReason + "\n" + voyageComment, fontArialNormal);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(20f, "Changes Made By:" + userName, fontArialBold);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(20f, "Contact#", fontArialBold);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(7f, null != user ? user.getTelephone() : "", fontArialNormal);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(2);
    String notes = "";
    if ("preview".equalsIgnoreCase(lclExportNotiFicationForm.getNoticeStatus())) {
        notes = lclExportNotiFicationForm.getRemarks() != null
                ? lclExportNotiFicationForm.getRemarks().toUpperCase()
                : "";
    }
    p = new Paragraph(7f, "Notes:", fontArialBold);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(2);
    p = new Paragraph(7f, " " + notes, fontArialNormal);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(2);
    p = new Paragraph(10f, " ", fontArialNormal);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(1);
    cell.setColspan(2);
    table.addCell(cell);
    return table;
}

From source file:com.gp.cong.logisoft.reports.LclExportVoyageNotificationPdf.java

public PdfPTable footerBlock() throws IOException, BadElementException, DocumentException, Exception {
    table = new PdfPTable(3);
    table.setWidthPercentage(100f);/*w  w w  .  j a  va2s  .com*/
    Font fontArialNormal = FontFactory.getFont("Courier", 10f, Font.NORMAL);
    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(9f, "Date:" + DateUtils.formatDate(new Date(), "MM/dd/yyyy"), fontArialNormal);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);

    table.addCell(cell);
    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(9f, "End Of Report", fontArialNormal);
    p.setAlignment(Element.ALIGN_LEFT);
    cell.addElement(p);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setBorder(0);
    cell.setColspan(0);
    p = new Paragraph(9f, "Time :" + new Date().getHours() + ":" + new Date().getMinutes(), fontArialNormal);
    p.setAlignment(Element.ALIGN_CENTER);
    cell.addElement(p);
    table.addCell(cell);
    return table;
}

From source file:com.microware.intrahealth.Createpdf2.java

@SuppressLint("SdCardPath")
public boolean write(Context context, String fname, String[] Header, ArrayList<HashMap<String, String>> data,
        String[] Page2, int Flag) throws Exception {
    try {/*from   w  w w . j av  a2  s  . com*/
        //           file = new File(Environment.getExternalStorageDirectory()+IIHSPdf+ "/"+fname+".pdf");

        this.context = context;
        g = (Global) context.getApplicationContext();
        BaseFont urName = BaseFont.createFont("assets/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        urFontName = new Font(urName, 12);
        urName1 = FontFactory.getFont("assets/mangal.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        // urFontName = new Font(urName, 12);
        sHeader[0] = context.getResources().getString(R.string.hrpreport);
        sHeader[1] = context.getResources().getString(R.string.anmname) + " " + g.getsGlobalANMName() + " "
                + context.getResources().getString(R.string.distname);
        sHeader[2] = context.getResources().getString(R.string.Identificationcode);
        sHeader2[0] = context.getResources().getString(R.string.Identificationcode1);
        sHeader2[1] = context.getResources().getString(R.string.totalhrp);
        sHeader2[2] = context.getResources().getString(R.string.totalcheckup);
        sHeader2[3] = context.getResources().getString(R.string.anmsign);
        catFont2 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133));

        if (Flag == 2) {

            catFont = new Font(urName, 16, Font.BOLD);
            catFont1 = new Font(urName, 16, Font.BOLD, new BaseColor(0, 85, 133));
            subFont = new Font(urName, 14);
            smallBold = new Font(urName, 12, Font.BOLD, BaseColor.WHITE);
        } else {

            catFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
            catFont1 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133));
            subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14);
            smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.WHITE);
        }

        String fpath = "/sdcard/msakhi/Pdf";
        File path = new File(fpath);
        File file = new File(path, fname + ".pdf");

        if (!path.exists()) {
            path.mkdirs();
            if (!file.exists()) {
                file.createNewFile();
            }
        } else {
            if (!file.exists()) {
                file.createNewFile();
            }
        }

        //         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);

        Document document = new Document(A4.rotate());

        PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile()));

        document.open();
        //         addMetaData(document);
        //          addTitlePage(document);
        addContent(document, Header, data, sHeader2, Flag);
        document.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private Font getFont(String color, boolean bold) throws Exception {
    Font font = FontFactory.getFont(BscConstants.PDF_ITEXT_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    int rgb[] = SimpleUtils.getColorRGB2(color);
    BaseColor baseColor = new BaseColor(rgb[0], rgb[1], rgb[2]);
    font.setSize(9);//from  w ww .  j a va  2s.c  o m
    if (bold) {
        font.setSize(14);
        font.setStyle(Font.BOLD);
    }
    font.setColor(baseColor);
    return font;
}

From source file:com.netsteadfast.greenstep.bsc.command.OrganizationReportPdfCommand.java

License:Apache License

private Font getFont(String color, boolean bold) throws Exception {
    Font font = FontFactory.getFont(BscConstants.PDF_ITEXT_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    int rgb[] = SimpleUtils.getColorRGB2(color);
    BaseColor baseColor = new BaseColor(rgb[0], rgb[1], rgb[2]);
    font.setSize(9);//  www. j  a  va 2  s  .c  o  m
    font.setColor(baseColor);
    return font;
}

From source file:com.prjhuellvotweb.controlador.PDF.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //Preguntar por la sesion del usuario admin
    HttpSession sessionOk = request.getSession(true);
    if (sessionOk.getAttribute("admin") != null) {
        //cambiar a tipo application/pdf
        response.setContentType("application/pdf;charset=UTF-8");
        //flujo de salida
        OutputStream out = response.getOutputStream();
        String texto = request.getParameter("report");
        //texto = "Reporte de los proyectos Sena CTGI (Centro tecnologico de gestion industrial) donde se dan a conocer"
        //                   + " los nombres de los proyectos y cantidad de votos obtenidos para cada proyecto.";
        try {/* w w w.  j  a  v a  2s.  co  m*/
            Connection con = Conexion.conectar("mysql");
            DAOVoto dao = new DAOVoto();
            List<List> lista = dao.estadisticaNumeroVotos();
            Voto t = dao.contarVotos();
            int to = t.getIdUsuario();
            if (!lista.isEmpty() && lista.size() > 0) {
                try {
                    //programar pdf
                    Document documento = new Document();
                    //asosciar documento con la salida
                    PdfWriter.getInstance(documento, out);// salida del cocumento en pdf
                    //abrir documento
                    documento.open();
                    Paragraph par2 = new Paragraph();
                    Paragraph par4 = new Paragraph();
                    //agregar una imagen logo sena al pdf
                    Image imagenes = Image
                            .getInstance(getServletContext().getRealPath("") + "/Multimedia/reportes.png");
                    //Centrar la imagen
                    imagenes.setAlignment(Element.ALIGN_CENTER);
                    //tamao de la imagen
                    imagenes.scaleToFit(530, 520);
                    //agg imagen al documento F:\\Documentos\\yo\\huellvot 2 17-06-2016\\PrjHuellVotWeb\\web\\iCO.png
                    //documento.add(imghuellvot);
                    documento.add(imagenes);
                    //Agg salto de linea
                    par2.add(new Phrase(Chunk.NEWLINE));
                    par2.add(new Phrase(Chunk.NEWLINE));

                    //fuente del pdf, tipo de fuente famimilia tamao de letra
                    //Importar ttf que contiene el tipo de letra
                    FontFactory.register(
                            "C:\\Users\\pc\\Desktop\\PrjHuellVotWeb\\web\\fonts\\roboto\\Roboto-Bold.ttf",
                            "Roboto");
                    //Font fondescripcion = FontFactory.getFont("Roboto");
                    Font fondescripcion = new Font(Font.getFamily("Roboto"), 16, Font.NORMAL, BaseColor.BLACK);
                    //texto de la descripcion
                    par2.add(new Phrase(texto, fondescripcion));
                    //justificar descripcion
                    par2.setAlignment(Element.ALIGN_JUSTIFIED);
                    //Agg salto de linea
                    par2.add(new Phrase(Chunk.NEWLINE));
                    par2.add(new Phrase(Chunk.NEWLINE));
                    //agregar descripcion al documento
                    documento.add(par2);//agregar todas las propiedades de la descripcin
                    //crear una tabla
                    PdfPTable tabla = new PdfPTable(5);//( Numero de columnas de la tabla)
                    //columnas de la tabla, cabezera y agg un estilo
                    PdfPCell celda = new PdfPCell(
                            new Paragraph("Nmero", FontFactory.getFont("Roboto", 14, Font.BOLD)));
                    PdfPCell celda1 = new PdfPCell(
                            new Paragraph("Nombre", FontFactory.getFont("Roboto", 14, Font.BOLD)));
                    celda1.setColspan(3);
                    PdfPCell celda2 = new PdfPCell(
                            new Paragraph("Votos", FontFactory.getFont("Roboto", 14, Font.BOLD)));
                    //Color de fondo
                    celda.setBackgroundColor(new BaseColor(252, 115, 35));
                    celda1.setBackgroundColor(new BaseColor(252, 115, 35));
                    celda2.setBackgroundColor(new BaseColor(252, 115, 35));
                    //Centrar
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    //padding
                    celda.setPadding(8.0f);
                    celda1.setPadding(8.0f);
                    celda2.setPadding(8.0f);
                    //agg columna ala tabla
                    tabla.addCell(celda);
                    tabla.addCell(celda1);
                    tabla.addCell(celda2);

                    for (int i = 0; i < lista.size(); i++) {
                        List l = lista.get(i);
                        //Convertir el entero a string
                        String num = String.valueOf(l.get(0));
                        String nom = String.valueOf(l.get(1));
                        String tot = String.valueOf(l.get(2));
                        //Agregar valores a las celdas
                        PdfPCell c = new PdfPCell(
                                new Paragraph(num, FontFactory.getFont("Roboto", 12, Font.BOLD)));
                        PdfPCell c1 = new PdfPCell(
                                new Paragraph(nom, FontFactory.getFont("Roboto", 12, Font.BOLD)));
                        c1.setColspan(3);
                        PdfPCell c2 = new PdfPCell(
                                new Paragraph(tot, FontFactory.getFont("Roboto", 12, Font.BOLD)));
                        //Padding para las celdas
                        c.setPadding(4.0f);
                        c1.setPadding(4.0f);
                        c2.setPadding(4.0f);
                        //Centrar contenido de celda
                        c.setHorizontalAlignment(Element.ALIGN_CENTER);
                        c2.setHorizontalAlignment(Element.ALIGN_CENTER);
                        //mostrar los resultados de cada columna los agrega a la tabla
                        tabla.addCell(c);
                        tabla.addCell(c1);
                        tabla.addCell(c2);

                    }
                    PdfPCell c1 = new PdfPCell(new Paragraph("Total Votos: ", fondescripcion));
                    c1.setColspan(4);
                    PdfPCell c2 = new PdfPCell(new Paragraph("" + to, fondescripcion));

                    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    c2.setHorizontalAlignment(Element.ALIGN_CENTER);

                    c1.setPadding(4.0f);
                    c2.setPadding(4.0f);

                    tabla.addCell(c1);
                    tabla.addCell(c2);

                    //Agrega la tabla a el documento
                    documento.add(tabla);
                    //agregar fecha
                    Font fonfecha = new Font(Font.getFamily("Roboto"), 12, Font.NORMAL, BaseColor.LIGHT_GRAY);

                    par4.add(new Phrase("Expedido por HuellVot", fonfecha));
                    DateFormat formato = DateFormat.getDateInstance(DateFormat.FULL);
                    par4.add(new Paragraph(formato.format(new Date())));

                    documento.add(par4);

                    //cerrar el documento
                    documento.close();
                } catch (DocumentException | IOException e) {
                    e.getMessage();
                    System.out.println("Error al generar el reporte PDF" + e);
                }

            } else {
                try {
                    Document documento = new Document();
                    PdfWriter.getInstance(documento, out);// salida del cocumento en pdf
                    //abrir documento
                    documento.open();
                    System.out.println("no hat datos");
                    //agregar una imagen logo sena al pdf
                    Image imagenes = Image.getInstance(
                            "C:\\Users\\pc\\Desktop\\PrjHuellVotWeb\\web\\Multimedia\\reportes.png");
                    //Centrar la imagen
                    imagenes.setAlignment(Element.ALIGN_CENTER);
                    //tamao de la imagen
                    imagenes.scaleToFit(530, 520);
                    //agg imagen al documento F:\\Documentos\\yo\\huellvot 2 17-06-2016\\PrjHuellVotWeb\\web\\iCO.png
                    //documento.add(imghuellvot);
                    documento.add(imagenes);
                    Paragraph par1 = new Paragraph();
                    Paragraph par2 = new Paragraph();
                    //Agg salto de linea
                    par1.add(new Phrase(Chunk.NEWLINE));
                    par1.add(new Phrase(Chunk.NEWLINE));

                    //fuente del pdf, tipo de fuente famimilia tamao de letra
                    //Importar ttf que contiene el tipo de letra
                    FontFactory.register(
                            "C:\\Users\\pc\\Desktop\\PrjHuellVotWeb\\web\\fonts\\roboto\\Roboto-Bold.ttf",
                            "Roboto");
                    //Font fondescripcion = FontFactory.getFont("Roboto");
                    Font fondescripcion = new Font(Font.getFamily("Roboto"), 16, Font.NORMAL, BaseColor.BLACK);
                    //texto de la descripcion
                    par1.add(new Phrase("lo sentimos pero no hay datos para mostrar.!", fondescripcion));
                    //justificar descripcion
                    par1.setAlignment(Element.ALIGN_CENTER);
                    //Agg salto de linea
                    par1.add(new Phrase(Chunk.NEWLINE));
                    par1.add(new Phrase(Chunk.NEWLINE));
                    //agregar descripcion al documento
                    documento.add(par1);//agregar todas las propiedades de la descripcin
                    //agregar fecha
                    Font fonfecha = new Font(Font.getFamily("Roboto"), 12, Font.NORMAL, BaseColor.LIGHT_GRAY);

                    par2.add(new Phrase("Expedido por HuellVot", fonfecha));
                    DateFormat formato = DateFormat.getDateInstance(DateFormat.FULL);
                    par2.add(new Paragraph(formato.format(new Date())));
                    documento.add(par2);
                    //cerrar el documento
                    documento.close();
                } catch (DocumentException ex) {
                    ex.getMessage();
                    System.out.println("Error al generar el reporte PDF sin datos registrados" + ex);
                }

            }

        } finally {
            out.close();
        }
    } else {
        sessionOk.invalidate();
        response.sendRedirect("index.jsp");
    }
}

From source file:com.softwaremagico.tm.pdf.complete.elements.BaseElement.java

License:Open Source License

protected PdfPCell getCell(String text, int border, int colspan, int align, BaseColor color, String font,
        int fontSize, int fontType) {
    if (text == null) {
        text = "";
    }/*from   ww w.  j a v a  2 s. c om*/
    Paragraph p = new Paragraph(text, FontFactory.getFont(font, fontSize, fontType));
    PdfPCell cell = new PdfPCell(p);
    cell.setColspan(colspan);
    cell.setBorderWidth(border);
    cell.setHorizontalAlignment(align);
    cell.setBackgroundColor(color);

    return cell;
}

From source file:com.thelinh.gui.UpdateChap.java

private void btnPdfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPdfActionPerformed
    Document document = new Document() {
    };//from w w w  .j a  v a  2 s.co  m
    try {
        JFileChooser jfc = new JFileChooser("Save File");
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            jfc.setDialogTitle("Save File");
            FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
            PdfWriter.getInstance(document, fos);
            document.open();
            Font rfont = FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", IDENTITY_H, true);
            document.add(new Paragraph(
                    "                                                                TRNG ?I HC B?CH KHOA H NI",
                    rfont));
            document.add(new Paragraph(
                    "                                                            KT QU TM KIM C?C CHNG CA MN HC\n",
                    rfont));
            document.add(new Paragraph(
                    "                Tm kim theo m mn h?c : " + txtSearch.getText() + "\n\n",
                    rfont));

            TableModel tableModel = tbChap.getModel();
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                String subject = (String) tableModel.getValueAt(i, 0) + " : "
                        + (String) tableModel.getValueAt(i, 1) + "\n\n";
                String id = (String) tableModel.getValueAt(i, 0);
                String id1 = id;
                document.add(new Paragraph(subject, rfont));
                PdfPTable table = new PdfPTable(2);
                PdfPCell header1 = new PdfPCell(new Paragraph("Chapter", rfont));
                PdfPCell header2 = new PdfPCell(new Paragraph("ChapterName", rfont));
                table.addCell(header1);
                table.addCell(header2);
                while (id.equals(id1) && i < tableModel.getRowCount()) {
                    table.addCell(
                            new PdfPCell(new Paragraph(String.valueOf(tableModel.getValueAt(i, 2)), rfont)));
                    table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 3), rfont)));
                    i++;
                    if (i < tableModel.getRowCount())
                        id1 = (String) tableModel.getValueAt(i, 0);
                }
                document.add(table);
                document.add(new Paragraph("\n"));

            }

            document.add(new Paragraph(
                    "\n                                                                                                      Ha Noi, November 4th, 2016\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                                Teacher\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                            (Signed and Sealed)\n",
                    rfont));

            document.close();
            JOptionPane.showMessageDialog(null, "Save success");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.thelinh.gui.UpdateQuestion.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    Document document = new Document() {
    };/* w  w  w  .  j av  a  2  s . c  o  m*/
    try {
        JFileChooser jfc = new JFileChooser("Save File");
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            jfc.setDialogTitle("Save File");
            FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
            PdfWriter.getInstance(document, fos);
            document.open();
            Font rfont = FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", IDENTITY_H, true);
            document.add(new Paragraph(
                    "                                                TRNG ?I HC B?CH KHOA H NI",
                    rfont));
            document.add(new Paragraph(
                    "\t\t                                                            KT QU TM KIM CU HI\n",
                    rfont));
            switch (k) {
            case 1:
                document.add(new Paragraph(
                        "                Tm kim theo QuestionId : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            case 2:
                document.add(new Paragraph("                Tm kim theo ni dung cu h?i : "
                        + txtSearch.getText() + "\n\n", rfont));
                break;
            case 3:
                document.add(new Paragraph(
                        "                Tm kim theo level : " + txtSearch.getText() + "\n\n", rfont));
                break;
            case 4:
                document.add(new Paragraph(
                        "                Tm kim theo m mn h?c : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            case 5:
                document.add(new Paragraph(
                        "                Tm kim theo cu tr l?i : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            }

            TableModel tableModel = tbQuestion.getModel();
            int question = 1;
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                String id = (String) tableModel.getValueAt(i, 0);
                document.add(new Paragraph("\nCu " + question + " : ", rfont));
                document.add(new Paragraph((String) tableModel.getValueAt(i, 1) + "\n\n", rfont));
                PdfPTable table = new PdfPTable(3);
                PdfPCell header1 = new PdfPCell(new Paragraph("?p n s", rfont));
                PdfPCell header2 = new PdfPCell(new Paragraph("Ni dung p n", rfont));
                PdfPCell header3 = new PdfPCell(new Paragraph("?ng sai", rfont));
                table.addCell(header1);
                table.addCell(header2);
                table.addCell(header3);
                String id1 = id;
                char answer = 'A';
                while (id.equals(id1) && i < tableModel.getRowCount()) {
                    table.addCell(new PdfPCell(new Paragraph("" + answer)));
                    table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 6), rfont)));
                    table.addCell(
                            new PdfPCell(new Paragraph(String.valueOf(tableModel.getValueAt(i, 7)), rfont)));
                    i++;
                    if (i < tableModel.getRowCount())
                        id1 = (String) tableModel.getValueAt(i, 0);
                    answer++;
                }
                document.add(table);
                question++;

            }

            document.add(new Paragraph(
                    "\n                                                                                                  Ha Noi, November 4th, 2016\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                            Teacher\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                        (Signed and Sealed)\n",
                    rfont));

            document.close();
            JOptionPane.showMessageDialog(null, "Save success");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.thelinh.gui.UpdateSubject.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    Document document = new Document() {
    };//from   w w  w.  j a  v a2  s .c o  m
    try {
        JFileChooser jfc = new JFileChooser("Save File");
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            jfc.setDialogTitle("Save File");
            FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
            PdfWriter.getInstance(document, fos);
            document.open();
            Font rfont = FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", IDENTITY_H, true);
            document.add(new Paragraph(
                    "                                                                TRNG ?I HC B?CH KHOA H NI",
                    rfont));
            document.add(new Paragraph(
                    "\t\t                                                                    KT QU TM KIM MN HC\n",
                    rfont));
            switch (k) {
            case 1:
                document.add(new Paragraph(
                        "                Tm kim theo m mn h?c : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            case 2:
                document.add(new Paragraph(
                        "                Tm kim theo tn mn h?c : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            }
            PdfPTable table = new PdfPTable(2);
            PdfPCell header1 = new PdfPCell(new Paragraph("SubjectId", rfont));
            PdfPCell header2 = new PdfPCell(new Paragraph("SubjectName", rfont));

            table.addCell(header1);
            table.addCell(header2);

            TableModel tableModel = tbSubject.getModel();
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 0), rfont)));
                table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 1), rfont)));

            }
            document.add(table);
            document.add(new Paragraph(
                    "\n                                                                                              Ha Noi, November 4th, 2016\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                        Teacher\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                    (Signed and Sealed)\n",
                    rfont));

            document.close();
            JOptionPane.showMessageDialog(null, "Save success");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    }
}