Example usage for com.itextpdf.text.pdf PdfWriter getInstance

List of usage examples for com.itextpdf.text.pdf PdfWriter getInstance

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter getInstance.

Prototype


public static PdfWriter getInstance(final Document document, final OutputStream os) throws DocumentException 

Source Link

Document

Use this method to get an instance of the PdfWriter.

Usage

From source file:com.test.itext.Renderer.java

private byte[] flattenXfa(byte[] sourceXfaBytes) throws RuntimeException {

    // Create an output stream for the flattened doc
    ByteArrayOutputStream flattened = new ByteArrayOutputStream();

    try {//  www  .j a v  a 2  s  .  com
        Document document = new Document();
        PdfReader reader = new PdfReader(sourceXfaBytes);
        PdfWriter writer = PdfWriter.getInstance(document, flattened);
        XFAFlattener flattener = new XFAFlattener(document, writer);
        System.out.println("flattener instantiated");
        flattener.flatten(reader);
        document.close();
    } catch (IOException e) {
        String msg = "An IOException was thrown while trying to flatten the XFA form. Msg=" + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (DocumentException e) {
        String msg = "A DocumentException was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (InterruptedException e) {
        String msg = "An InterruptedException was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (NoClassDefFoundError e) {
        String msg = "A NoClassDefFoundError was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (Exception e) {
        String msg = "A general Exception was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    }

    return flattened.toByteArray();
}

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

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
    if (this.txtExamCode.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Hy nhp m ?");
    } else {/*from ww w  .  j a  v  a2 s. com*/
        JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF files (*.pdf)", "pdf");
        fc.setFileFilter(filter);
        int rt = fc.showSaveDialog(this);
        if (rt == JFileChooser.APPROVE_OPTION) {
            String filepath = fc.getSelectedFile().getPath();
            if (filepath.indexOf(".") == -1) {
                filepath = filepath + ".pdf";
            }
            String dafilepath = (new StringBuffer(filepath)).insert(filepath.length() - 4, ".da").toString();
            try {
                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(filepath));
                document.open();

                // Add title
                Paragraph p = new Paragraph("Tr?ng ?i h?c Bch Khoa H Ni", timesBold_25);
                p.setAlignment(Element.ALIGN_CENTER);
                p.setSpacingAfter(5);
                document.add(p);

                p = new Paragraph("?? thi mn " + this.lbSubject.getText(), timesBold_25);
                p.setAlignment(Element.ALIGN_CENTER);
                p.setSpacingAfter(25);
                document.add(p);

                p = new Paragraph(
                        "Ng?i ra ?: " + Controller.getCurrentAdmin().getAdminName()
                                + ", Ngy ra ?: "
                                + (new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime())),
                        times_15);
                p.setAlignment(Element.ALIGN_LEFT);
                p.setSpacingAfter(5);
                document.add(p);

                p = new Paragraph("M ?: " + this.txtExamCode.getText() + "    Th?i gian lm bi: "
                        + spTime.getValue().toString() + " pht", timesBold_15);
                p.setSpacingAfter(25);
                document.add(p);

                for (int i = 0; i < this.questionList.size(); i++) {
                    Question question = this.questionList.get(i);
                    ArrayList<Answer> answerList = AnswerSql
                            .getAnswersByQuestionId(question.getQuestionId().trim());
                    Phrase questionNumber = new Phrase("Cu " + (i + 1) + ": ", timesBold_15);
                    Phrase questionContent = new Phrase(question.getQuestion(), times_15);

                    Paragraph questionTitle = new Paragraph();
                    questionTitle.add(questionNumber);
                    questionTitle.add(questionContent);

                    document.add(questionTitle);
                    PdfPTable table = new PdfPTable(2);

                    for (int j = 0; j < answerList.size(); j++) {
                        Answer ans = answerList.get(j);
                        Phrase cap = new Phrase(Character.toString((char) (j + 65)) + ". ", timesBold_15);
                        Phrase answerContent = new Phrase(ans.getAnswer(), times_15);
                        Phrase elem = new Phrase();
                        elem.add(cap);
                        elem.add(answerContent);
                        PdfPCell cell = new PdfPCell();

                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setPhrase(elem);
                        cell.setFixedHeight(25);
                        table.addCell(cell);
                    }
                    document.add(table);
                }
                document.close();
                //                    if (Desktop.isDesktopSupported()) {
                //                        try {
                //                            File myFile = new File(filepath);
                //                            Desktop.getDesktop().open(myFile);
                //                        } catch (IOException ex) {
                //                            // no application registered for PDFs
                //                            System.out.println(ex.toString());
                //                        }
                //                    }

                document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(dafilepath));
                document.open();

                // Add title
                p = new Paragraph("Tr?ng ?i h?c Bch Khoa H Ni", timesBold_25);
                p.setAlignment(Element.ALIGN_CENTER);
                p.setSpacingAfter(5);
                document.add(p);

                p = new Paragraph("?? thi mn " + this.lbSubject.getText(), timesBold_25);
                p.setAlignment(Element.ALIGN_CENTER);
                p.setSpacingAfter(25);
                document.add(p);

                p = new Paragraph(
                        "Ng?i ra ?: " + Controller.getCurrentAdmin().getAdminName()
                                + ", Ngy ra ?: "
                                + (new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime())),
                        times_15);
                p.setAlignment(Element.ALIGN_LEFT);
                p.setSpacingAfter(5);
                document.add(p);

                p = new Paragraph("M ?: " + this.txtExamCode.getText() + "    Th?i gian lm bi: "
                        + spTime.getValue().toString() + " pht", timesBold_15);
                p.setSpacingAfter(25);
                document.add(p);

                for (int i = 0; i < this.questionList.size(); i++) {
                    Question question = this.questionList.get(i);
                    ArrayList<Answer> answerList = AnswerSql
                            .getAnswersByQuestionId(question.getQuestionId().trim());
                    Phrase questionNumber = new Phrase("Cu " + (i + 1) + ": ", timesBold_15);
                    Phrase questionContent = new Phrase(question.getQuestion(), times_15);

                    Paragraph questionTitle = new Paragraph();
                    questionTitle.add(questionNumber);
                    questionTitle.add(questionContent);

                    document.add(questionTitle);
                    PdfPTable table = new PdfPTable(2);

                    for (int j = 0; j < answerList.size(); j++) {
                        Answer ans = answerList.get(j);
                        Phrase cap = new Phrase(Character.toString((char) (j + 65)) + ". ", timesBold_15);
                        Phrase answerContent = null;
                        if (ans.getYesNo()) {
                            answerContent = new Phrase(ans.getAnswer(), timesBold_15);
                        } else {
                            answerContent = new Phrase(ans.getAnswer(), times_15);
                        }
                        Phrase elem = new Phrase();
                        elem.add(cap);
                        elem.add(answerContent);
                        PdfPCell cell = new PdfPCell();

                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setPhrase(elem);
                        cell.setFixedHeight(25);
                        table.addCell(cell);
                    }
                    document.add(table);
                }
                document.close();

            } catch (FileNotFoundException | DocumentException ex) {
                Logger.getLogger(PreviewExam.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

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

private void btnExportReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportReportActionPerformed
    Document document = new Document() {
    };/*  www. j  a v  a2s  .  co  m*/
    try {
        JFileChooser jfc = new JFileChooser("Save File");
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            //  String content = this.txtReport.getText();
            jfc.setDialogTitle("Save File");
            FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
            PdfWriter.getInstance(document, fos);
            //                Font rfont = FontFactory.getFont("resources/fonts/vuArial.ttf", IDENTITY_H, true);
            Font rfont = new Font(BaseFont.createFont("resources/fonts/vuTimesBold.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED));
            document.open();
            document.add(new Paragraph(
                    "                                                    TRNG ?I HC B?CH KHOA H NI",
                    rfont));

            if (k == 1) {
                document.add(new Paragraph(
                        "\t\t                                                                   TH?NG K MN HC\n",
                        rfont));
                String sqlSubject1 = "SELECT Count(SubjectId) AS subjectAll FROM Subjects";
                String sqlSubject2 = "SELECT Subjects.SubjectName AS subjectChap, Count(*) AS subjectNumber FROM Chaps,Subjects WHERE Chaps.SubjectId = Subjects.SubjectId GROUP BY Subjects.SubjectName";
                ResultSet rs1 = LoadTable.Display(sqlSubject1);
                ResultSet rs2 = LoadTable.Display(sqlSubject2);
                try {
                    if (rs1.next()) {
                        document.add(new Paragraph(
                                "Tng s mn h?c l " + rs1.getInt("subjectAll") + "\n", rfont));
                    }
                    document.add(new Paragraph("Thng k s chng ca mn h?c: \n\n", rfont));
                    PdfPTable table = new PdfPTable(2);
                    PdfPCell header1 = new PdfPCell(new Paragraph("Mn h?c", rfont));
                    PdfPCell header2 = new PdfPCell(new Paragraph("S chng", rfont));
                    table.addCell(header1);
                    table.addCell(header2);
                    while (rs2.next()) {
                        PdfPCell header3 = new PdfPCell(new Paragraph(rs2.getString("subjectChap"), rfont));
                        table.addCell(header3);
                        PdfPCell header4 = new PdfPCell(new Paragraph("" + rs2.getInt("subjectNumber")));
                        table.addCell(header4);
                    }
                    document.add(table);
                } catch (SQLException ex) {
                    Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (k == 2) {
                try {
                    document.add(new Paragraph(
                            "\t\t                                                                 TH?NG K CU HI\n",
                            rfont));
                    String sqlQuestion1 = "SELECT Count(QuestionId) AS questionAll FROM Questions";
                    String sqlQuestion2 = "SELECT SubjectName AS subjectName, Count(QuestionId) AS questionSubject FROM Questions,Subjects WHERE Subjects.SubjectId = Questions.SubjectId GROUP BY Subjects.SubjectName";
                    ResultSet rs1 = LoadTable.Display(sqlQuestion1);
                    ResultSet rs2 = LoadTable.Display(sqlQuestion2);
                    if (rs1.next()) {
                        document.add(new Paragraph(
                                "Tng s cu h?i l " + rs1.getInt("questionAll") + "\n", rfont));
                    }
                    document.add(new Paragraph("Thng k s cu h?i ca mn h?c: \n\n", rfont));
                    PdfPTable table = new PdfPTable(2);
                    PdfPCell header1 = new PdfPCell(new Paragraph("Mn h?c", rfont));
                    PdfPCell header2 = new PdfPCell(new Paragraph("S cu h?i", rfont));
                    table.addCell(header1);
                    table.addCell(header2);
                    while (rs2.next()) {
                        PdfPCell header3 = new PdfPCell(new Paragraph(rs2.getString("subjectName"), rfont));
                        table.addCell(header3);
                        PdfPCell header4 = new PdfPCell(new Paragraph("" + rs2.getInt("questionSubject")));
                        table.addCell(header4);
                    }
                    document.add(table);
                } catch (SQLException ex) {
                    Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (k == 3) {
                try {
                    document.add(new Paragraph(
                            "\t\t                                                                  TH?NG K NGI DNG\n",
                            rfont));
                    String sqlUser1 = "SELECT Count(UserId) AS userAll FROM Users";
                    String sqlUser2 = "SELECT Class AS className, Count(UserId) AS userClass FROM Users GROUP BY Class";
                    ResultSet rs1 = LoadTable.Display(sqlUser1);
                    ResultSet rs2 = LoadTable.Display(sqlUser2);
                    if (rs1.next()) {
                        document.add(new Paragraph(
                                "Tng s ng?i dng l " + rs1.getInt("userAll") + "\n", rfont));
                    }
                    document.add(new Paragraph("Thng k s h?c sinh ca lp: \n\n", rfont));
                    PdfPTable table = new PdfPTable(2);
                    PdfPCell header1 = new PdfPCell(new Paragraph("Lp", rfont));
                    PdfPCell header2 = new PdfPCell(new Paragraph("S h?c sinh", rfont));
                    table.addCell(header1);
                    table.addCell(header2);
                    while (rs2.next()) {
                        PdfPCell header3 = new PdfPCell(new Paragraph(rs2.getString("className"), rfont));
                        table.addCell(header3);
                        PdfPCell header4 = new PdfPCell(new Paragraph("" + rs2.getInt("userClass")));
                        table.addCell(header4);
                    }
                    document.add(table);
                } catch (SQLException ex) {
                    Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (k == 4) {
                document.add(new Paragraph(
                        "\t\t                                                                        TH?NG K KT QU\n",
                        rfont));
                String sqlResult1 = "SELECT Count(*) AS resultAll FROM Results";
                String sqlResult2 = "SELECT Count(*) AS exam1 FROM Results WHERE Result <= 4";
                String sqlResult3 = "SELECT Count(*) AS exam2 FROM Results WHERE Result > 4 AND Result < 6";
                String sqlResult4 = "SELECT Count(*) AS exam3 FROM Results WHERE Result >= 6 AND Result < 8";
                String sqlResult5 = "SELECT Count(*) AS exam4 FROM Results WHERE Result >= 8";
                ResultSet rs1 = LoadTable.Display(sqlResult1);
                ResultSet rs2 = LoadTable.Display(sqlResult2);
                ResultSet rs3 = LoadTable.Display(sqlResult3);
                ResultSet rs4 = LoadTable.Display(sqlResult4);
                ResultSet rs5 = LoadTable.Display(sqlResult5);
                if (rs1.next()) {
                    document.add(
                            new Paragraph("Tng s bi thi l " + rs1.getInt("resultAll") + "\n", rfont));
                }
                document.add(new Paragraph("Thng k im thi: \n\n", rfont));
                PdfPTable table = new PdfPTable(2);
                PdfPCell header1 = new PdfPCell(new Paragraph("?im", rfont));
                PdfPCell header2 = new PdfPCell(new Paragraph("S bi thi", rfont));
                table.addCell(header1);
                table.addCell(header2);
                PdfPCell header3 = new PdfPCell(new Paragraph("<= 4"));
                table.addCell(header3);
                if (rs2.next()) {
                    PdfPCell header4 = new PdfPCell(new Paragraph(rs2.getInt("exam1")));
                    table.addCell(header4);
                }
                PdfPCell header5 = new PdfPCell(new Paragraph("> 4 v < 6"));
                table.addCell(header5);
                if (rs3.next()) {
                    PdfPCell header6 = new PdfPCell(new Paragraph(rs3.getInt("exam2")));
                    table.addCell(header6);
                }
                PdfPCell header7 = new PdfPCell(new Paragraph(">= 6 v < 8"));
                table.addCell(header7);
                if (rs4.next()) {
                    PdfPCell header8 = new PdfPCell(new Paragraph(rs4.getInt("exam3")));
                    table.addCell(header8);
                }
                PdfPCell header9 = new PdfPCell(new Paragraph(">= 8"));
                table.addCell(header9);
                if (rs5.next()) {
                    PdfPCell header10 = new PdfPCell(new Paragraph(rs5.getInt("exam4")));
                    table.addCell(header10);
                }
                document.add(table);
            }
            document.add(new Paragraph(
                    "                                                                                                  H Ni, ngy 09 thng 12 nm 2016\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                               Gio Vin\n",
                    rfont));
            document.add(new Paragraph(
                    "                                                                                                          "
                            + Controller.getCurrentAdmin().getAdminName() + "\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);
    } catch (SQLException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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 ww  .j a v  a 2s.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(
                    "                                                            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() {
    };//from ww w  .j ava  2  s.com
    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() {
    };// w  ww  .  j  av a 2s. 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(
                    "\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);
    }
}

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    Document document = new Document() {
    };//from www  . j av  a2s  .com
    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 NGI DNG\n",
                    rfont));
            switch (k) {
            case 1:
                document.add(new Paragraph(
                        "                Tm kim theo m ng?i dng : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            case 2:
                document.add(new Paragraph(
                        "                Tm kim theo mt khu : " + txtSearch.getText() + "\n\n",
                        rfont));
                break;
            case 3:
                document.add(new Paragraph("                Tm kim theo tn ng?i dng : "
                        + txtSearch.getText() + "\n\n", rfont));
                break;
            case 4:
                document.add(new Paragraph(
                        "                Tm kim theo lp :" + txtSearch.getText() + "\n\n", rfont));
                break;
            }
            PdfPTable table = new PdfPTable(5);
            PdfPCell header1 = new PdfPCell(new Paragraph("UserId", rfont));
            PdfPCell header2 = new PdfPCell(new Paragraph("Password", rfont));
            PdfPCell header3 = new PdfPCell(new Paragraph("UserName", rfont));
            PdfPCell header4 = new PdfPCell(new Paragraph("BirthDay", rfont));
            PdfPCell header5 = new PdfPCell(new Paragraph("Class", rfont));
            table.addCell(header1);
            table.addCell(header2);
            table.addCell(header3);
            table.addCell(header4);
            table.addCell(header5);

            TableModel tableModel = tbUser.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)));
                table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 2), rfont)));
                SimpleDateFormat sdf = new SimpleDateFormat();
                table.addCell(new PdfPCell(new Paragraph(sdf.format(tableModel.getValueAt(i, 3)), rfont)));
                table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 4), 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);
    }
}

From source file:com.thelinh.views.Statistics.java

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
    Document document = new Document() {
    };//from  ww  w  . ja  v a2  s.  c o  m
    try {
        JFileChooser jfc = new JFileChooser("Save File");
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            String content = this.taReport.getText();
            jfc.setDialogTitle("Save File");
            FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
            PdfWriter.getInstance(document, fos);
            document.open();
            document.add(new Paragraph(content));
            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.thomasmore.service.PdfCreateServiceImpl.java

public void readPdf() {
    try {//from w  w  w  .j a v  a  2 s. c o m
        OutputStream file = new FileOutputStream(new File("D:\\SamplePDFje.pdf"));

        Document document = new Document();
        PdfWriter.getInstance(document, file);

        document.open();
        document.add(new Paragraph("First iText PDF"));
        document.add(new Paragraph(new Date().toString()));

        document.addAuthor("Krishna Sfsfsfsn");
        document.addCreationDate();
        document.addCreator("JavaBeat");
        document.addTitle("Sample PDF");

        //Create Paragraph
        Paragraph paragraph = new Paragraph("Title 1", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));

        //New line
        paragraph.add(new Paragraph(" "));
        paragraph.add("Test Paragraph");
        paragraph.add(new Paragraph(" "));
        document.add(paragraph);

        //Create a table in PDF
        PdfPTable pdfTable = new PdfPTable(3);
        PdfPCell cell1 = new PdfPCell(new Phrase("Table Header 1"));
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell1);

        cell1 = new PdfPCell(new Phrase("Table Header 2"));
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell1);

        cell1 = new PdfPCell(new Phrase("Table Header 3"));
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell1);
        pdfTable.setHeaderRows(1);

        pdfTable.addCell("Row 1 Col 1");
        pdfTable.addCell("Row 1 Col 2");
        pdfTable.addCell("Row 1 Col 3");

        pdfTable.addCell("Row 2 Col 1");
        pdfTable.addCell("Row 2 Col 2");
        pdfTable.addCell("Row 2 Col 3");

        document.add(pdfTable);

        document.close();
        file.close();

    } catch (Exception e) {

        e.printStackTrace();
    }
}

From source file:com.tomasz.drag.triballocommanderro.controller.MakerPDF.java

public static void printToPDFBoss(ArrayList<Person> workers, EventGig event)
        throws IOException, DocumentException {
    String filename = event.getName() + " Boss" + ".pdf";
    String path = event.getData() + " " + event.getName() + "//";
    //Rectangle rect = new Rectangle(0, 595, 8, 0);
    Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
    //Document document = new Document(rect);
    //document.setMargins(0, 0, 0, 0);
    BaseFont bf;/*from  www  .  ja  va  2 s. c  o m*/
    //BaseFont bf2;
    // bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    bf = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H,
            BaseFont.EMBEDDED);
    //bf2 = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    com.itextpdf.text.Font ft = new com.itextpdf.text.Font(bf, 9);
    //com.itextpdf.text.Font ft2 = new com.itextpdf.text.Font(bf2,9);

    PdfWriter.getInstance(document, new FileOutputStream(path + filename));

    document.open();
    Paragraph p = new Paragraph(event.getName() + " " + event.getData(), ft);
    p.setAlignment(Element.ALIGN_CENTER);
    document.add(p);
    document.add(new Paragraph(" "));
    PdfPTable table = new PdfPTable(workers.size() + 1);
    table.setWidthPercentage(98);
    PdfPCell cell = new PdfPCell(new Phrase("    "));
    table.addCell(cell);
    for (Person tempPerson : workers) {
        cell = new PdfPCell(new Paragraph(tempPerson.getName(), ft));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);

    }
    if (event.isZaladunekScenaDach()) {
        cell = new PdfPCell(new Paragraph("Za. Sc./Dach", ft));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getZaladunekScenaDachWyplata() > 0) {
                cell = new PdfPCell(
                        new Paragraph(String.format("%.2f", tempPerson.getZaladunekScenaDachWyplata()), ft));
                cell.setVerticalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }
    if (event.isZaladunekTechniki()) {
        cell = new PdfPCell(new Phrase("Za. Tech.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getZaladunekTechnikiWyplata() > 0) {
                cell = new PdfPCell(
                        new Paragraph(String.format("%.2f", tempPerson.getZaladunekTechnikiWyplata()), ft));
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isRozladunekScenaDach()) {
        cell = new PdfPCell(new Phrase("Roz Sc/Dach", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getRozladunekScenaDachWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getRozladunekScenaDachWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }
    if (event.isRozladunekTechniki()) {
        cell = new PdfPCell(new Phrase("Roz. Tech.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getRozladunekTechnikiWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getRozladunekTechnikiWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isMontazDachu()) {
        cell = new PdfPCell(new Phrase("Mon. Dach", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getMontazDachuWyplata() > 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazDachuWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isMontazDzwieku()) {
        cell = new PdfPCell(new Phrase("Mon. Dw.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getMontazDzwiekuWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getMontazDzwiekuWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isMontazSceny()) {
        cell = new PdfPCell(new Phrase("Mon. Sc.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getMontazScenyWyplata() > 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazScenyWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isMontazSwiatla()) {
        cell = new PdfPCell(new Phrase("Mon. w.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getMontazSwiatlaWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getMontazSwiatlaWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isMontazTechniki()) {
        cell = new PdfPCell(new Phrase("Mon. Tech.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getMontazTechnikiWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getMontazTechnikiWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDemontazDachu()) {
        cell = new PdfPCell(new Phrase("Dem. Dach", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDemontazDachuWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getDemontazDachuWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDemontazDzwieku()) {
        cell = new PdfPCell(new Phrase("Dem. Dw.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDemontazDzwiekuWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getDemontazDzwiekuWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDemontazSceny()) {
        cell = new PdfPCell(new Phrase("Dem. Sc.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDemontazScenyWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getDemontazScenyWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDemontazSwiatla()) {
        cell = new PdfPCell(new Phrase("Dem. w.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDemontazSwiatlaWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getDemontazSwiatlaWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase("   "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDemontazTechniki()) {
        cell = new PdfPCell(new Phrase("Dem. Tech.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDemontazTechnikiWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getDemontazTechnikiWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isDyzur()) {
        cell = new PdfPCell(new Phrase("Dyur", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getDyzurWyplata() > 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getDyzurWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(""));
                table.addCell(cell);
            }
        }
    }

    if (event.isTechnikSceny()) {
        cell = new PdfPCell(new Phrase("Technik Sc.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getTechnikScenyWyplata() > 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getTechnikScenyWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isRealizacjaFOH()) {
        cell = new PdfPCell(new Phrase("Real. FOH", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getRealizacjaFOHWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getRealizacjaFOHWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isRealizacjaMON()) {
        cell = new PdfPCell(new Phrase("Real. MON", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getRealizacjaMONWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getRealizacjaMONWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (event.isRealizacjaSwiatla()) {
        cell = new PdfPCell(new Phrase("Real. w.", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getRealizacjaSwiatlaWyplata() > 0) {
                cell = new PdfPCell(
                        new Phrase(String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    if (true) {
        cell = new PdfPCell(new Phrase("Premia", ft));
        table.addCell(cell);
        for (Person tempPerson : workers) {
            if (tempPerson.getPremia() > 0) {
                cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getPremia()), ft));
                table.addCell(cell);
            } else {
                cell = new PdfPCell(new Phrase(" "));
                table.addCell(cell);
            }
        }
    }

    Paragraph ph = new Paragraph("Razem", ft);

    //cell = new PdfPCell(new Phrase(ph));
    cell.addElement(ph);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(cell);
    for (Person tempPerson : workers) {
        if (tempPerson.getSumaWyplata() >= 0) {
            cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getSumaWyplata()) + " z", ft));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
        } else {
            cell = new PdfPCell(new Phrase(" "));
            table.addCell(cell);
        }
    }
    //        for (Object obj : table.getChunks()) {
    //            if (obj instanceof PdfPCell) {
    //                System.out.println("jest PdfCell");
    //                PdfPCell tempCell = (PdfPCell) obj;
    //                tempCell.setVerticalAlignment(Element.ALIGN_CENTER);
    //                //tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    //            }
    //            
    //        }
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    document.add(table);
    document.close();

}