Example usage for com.itextpdf.text Document setPageSize

List of usage examples for com.itextpdf.text Document setPageSize

Introduction

In this page you can find the example usage for com.itextpdf.text Document setPageSize.

Prototype


public boolean setPageSize(Rectangle pageSize) 

Source Link

Document

Sets the pagesize.

Usage

From source file:ci_eletronico.FXMLMainController.java

@FXML
private void handleBtnImprimirCI(ActionEvent event) throws IOException {
    if (null == TbViewGeral.getSelectionModel().getSelectedItem()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Erro");
        alert.setHeaderText("CI no foi selecionada.");
        alert.setContentText("Favor selecionar uma CI da tabela");
        alert.showAndWait();/*from w  w  w.  j  a v  a  2 s .c o  m*/
    } else {

        File outfile = null; // variavel para abrir documento pdf aps sua criao

        String strFileName = this.lblNumeroSequencialCI.getText();
        String strUserHome = System.getProperty("user.home") + "\\Downloads\\" + strFileName + ".pdf";
        final Document document = new Document();
        try {
            document.setPageSize(PageSize.A4);
            document.setMargins(10f, 10f, 70f, 40f);
            String strCIAssinatura = TbViewGeral.getSelectionModel().getSelectedItem().getStrp_CoinAssinatura();

            //PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/Temp/teste.pdf"));
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(strUserHome));

            MyFooter evento = new MyFooter(strCIAssinatura);
            writer.setPageEvent(evento);

        } catch (IOException | DocumentException e) {
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }
        document.open();

        //String htmlString = htmlEditorCI.getHtmlText();
        String strlCITitulo = "";
        strlCITitulo = "<br /><p align=\"center\"><b>" + strFileName + "</b></p>";
        String htmlString = TbViewGeral.getSelectionModel().getSelectedItem().getStrp_Conteudo();
        //String strCIAssinatura = TbViewGeral.getSelectionModel().getSelectedItem().getStrp_CoinAssinatura();

        //            htmlString = htmlString.replace("<br>", "\n");
        //            htmlString = htmlString.replace("<br/>", "\n");
        //            htmlString = htmlString.replace("<br />", "\n");

        htmlString = htmlString.replace("<br>", "<br />");

        //            htmlString = htmlString.replace("<hr>", "<p></p>");
        //            htmlString = htmlString.replace("<hr/>", "<p></p>");
        //            htmlString = htmlString.replace("<hr />", "<p></p>");

        htmlString = htmlString.replace("<hr>", "<hr />");

        htmlString = strlCITitulo.concat(htmlString);

        StringReader in = new StringReader(htmlString);

        try {
            final Paragraph test = new Paragraph();
            XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
                @Override
                public void add(final Writable w) {
                    if (w instanceof WritableElement) {
                        List<Element> elements = ((WritableElement) w).elements();
                        for (Element e : elements) {
                            test.add(e);
                        }
                    }
                }
            }, in);

            document.add(test);
        } catch (IOException | DocumentException e) {
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }

        document.close();

        outfile = new File(strUserHome);

        //            Alert alert = new Alert(Alert.AlertType.INFORMATION);
        //            alert.setTitle("Informao");
        //            alert.setHeaderText("Imprimir CI: " + strFileName );
        //            alert.setContentText("Arquivo pdf criado com sucesso na pasta Downloads");
        //            alert.showAndWait();

        openArquivo(outfile); // Abrimos o pdf criado para ser visualizado
    }
}

From source file:client.welcome0.java

private void genRepButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genRepButton1ActionPerformed

    Document doc = new Document();
    PdfWriter docWriter = null;//from   www . ja va2s  .  co  m
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();
    String fileName = null;
    job_title = JobTitleChoice.getSelectedItem();
    try {

        //special font sizes
        com.itextpdf.text.Font bfBold12 = new com.itextpdf.text.Font(
                com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 12, com.itextpdf.text.Font.BOLD,
                new BaseColor(0, 0, 0));
        com.itextpdf.text.Font bf12 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN,
                12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Workers Report- " + dt + " Job Title " + job_title + " .pdf";
        String path = "src/WorkerReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();
        //create a paragraph

        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance("src/Images/logo for pdf.png");
        com.itextpdf.text.Font font1 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.HELVETICA,
                25, com.itextpdf.text.Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see Yatzig's workers by the job title you chose");
        image.setAlignment(com.itextpdf.text.Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 3f, 3f, 3f, 4f, 3f, 3f, 5f, 3f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Worker ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "First Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Last Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Phone", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Address", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Birth Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Email", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Job Title", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5, add6, add7, add8;

        if (job_title.equals("All"))
            try {

                String sql = "select worker_id,first_name,last_name,worker_phone,worker_add,birth_date,email,job_title from workers order by job_title ";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("worker_id");
                    add2 = rs.getString("first_name");
                    add3 = rs.getString("last_name");
                    add4 = rs.getString("worker_phone");
                    add5 = rs.getString("worker_add");
                    add6 = dateFormat.format(rs.getDate("birth_date"));
                    add7 = rs.getString("email");
                    add8 = rs.getString("job_title");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add6, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add7, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add8, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }

        else
            try {

                String sql = "select worker_id,first_name,last_name,worker_phone,worker_add,birth_date,email,job_title from workers where job_title='"
                        + job_title + "' ";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("worker_id");
                    add2 = rs.getString("first_name");
                    add3 = rs.getString("last_name");
                    add4 = rs.getString("worker_phone");
                    add5 = rs.getString("worker_add");
                    add6 = dateFormat.format(rs.getDate("birth_date"));
                    add7 = rs.getString("email");
                    add8 = rs.getString("job_title");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add6, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add7, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add8, Element.ALIGN_CENTER, 1, bf12);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }

        //add the PDF table to the paragraph 
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nWorkers Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(job_title, date, repID);

}

From source file:client.welcome1.java

private void genrateItemRepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genrateItemRepButtonActionPerformed
    Document doc = new Document();
    PdfWriter docWriter = null;/*from w w  w  . ja  v  a 2 s  .  c  om*/
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();

    try {

        //special font sizes
        com.itextpdf.text.Font bfBold12 = new com.itextpdf.text.Font(FontFamily.TIMES_ROMAN, 12,
                com.itextpdf.text.Font.BOLD, new BaseColor(0, 0, 0));
        com.itextpdf.text.Font bf12 = new com.itextpdf.text.Font(FontFamily.TIMES_ROMAN, 12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Items Report- " + dt + " Status " + itemRepChoice + " .pdf";
        String path = "src/ItemsReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        Image image = Image.getInstance("src/Images/logo for pdf.png");
        Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see " + itemRepChoice + " Items");
        image.setAlignment(Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 2f, 2f, 2f, 3f, 2f, 2f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Item ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Item Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Quantity", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Min Quantity Level", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Warehouse", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Row", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Shelf", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5, add6, add7;

        if (itemRepChoice.equals("All")) {
            try {

                String sql = "select item_id,item_name,quantity,min_quantity_level,warehouse,row,shelf from items";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("item_id");
                    add2 = rs.getString("item_name");
                    add3 = rs.getString("quantity");
                    add4 = rs.getString("min_quantity_level");
                    add5 = rs.getString("warehouse");
                    add6 = rs.getString("row");
                    add7 = rs.getString("shelf");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add6, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add7, Element.ALIGN_CENTER, 1, bf12);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else if (itemRepChoice.equals("Warehouse")) {
            String wh = wareHouschoice.getSelectedItem();
            try {

                String sql = "select item_id,item_name,quantity,min_quantity_level,warehouse,row,shelf from items where warehouse='"
                        + wh + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("item_id");
                    add2 = rs.getString("item_name");
                    add3 = rs.getString("quantity");
                    add4 = rs.getString("min_quantity_level");
                    add5 = rs.getString("warehouse");
                    add6 = rs.getString("row");
                    add7 = rs.getString("shelf");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add6, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add7, Element.ALIGN_CENTER, 1, bf12);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else if (itemRepChoice.equals("Min Level")) {
            String wh = wareHouschoice.getSelectedItem();
            try {

                String sql = "select item_id,item_name,quantity,min_quantity_level,warehouse,row,shelf from items where min_quantity_level>=quantity";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("item_id");
                    add2 = rs.getString("item_name");
                    add3 = rs.getString("quantity");
                    add4 = rs.getString("min_quantity_level");
                    add5 = rs.getString("warehouse");
                    add6 = rs.getString("row");
                    add7 = rs.getString("shelf");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add6, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add7, Element.ALIGN_CENTER, 1, bf12);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        }

        //add the PDF table to the paragraph 
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nItems Status Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(itemRepChoice, date, repID);
}

From source file:client.welcome2.java

private void genRepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genRepButtonActionPerformed
    Document doc = new Document();
    PdfWriter docWriter = null;/*from   w w w.j  ava  2s .  com*/
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();

    try {

        //special font sizes
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Project Report- " + dt + " Status " + report_status + " .pdf";
        String path = "src/ProjectReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
        String sd2 = dateFormat2.format(pBeginDateChooser.getDate());
        String ed2 = dateFormat2.format(pEndDateChooser.getDate());
        Image image = Image.getInstance("src/Images/logo for pdf.png");
        Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see all projects from " + sd2 + " to " + ed2);
        image.setAlignment(Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 2f, 2f, 2f, 2f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Project ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Project Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Start Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Due Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Status", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5;
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String sd = dateFormat1.format(pBeginDateChooser.getDate());
        String ed = dateFormat1.format(pEndDateChooser.getDate());

        if (report_status.equals("All")) {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where due_date >= '"
                        + sd + "' and due_date <= '" + ed + "' and start_date >= '" + sd
                        + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where status = '"
                        + report_status + "' and due_date >= '" + sd + "' and due_date <= '" + ed
                        + "' and start_date >= '" + sd + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        }

        //add the PDF table to the paragraph
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nProject Status Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(report_status, date, repID);
}

From source file:client.welcome3.java

private void genRepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genRepButtonActionPerformed
    Document doc = new Document();
    PdfWriter docWriter = null;//from   w w  w  .j a  va  2 s .co  m
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();

    try {

        //special font sizes
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Project Report- " + dt + " Status " + report_status + " .pdf";
        String path = "src/ProjectReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
        String sd2 = dateFormat2.format(pBeginDateChooser.getDate());
        String ed2 = dateFormat2.format(pEndDateChooser.getDate());
        Image image = Image.getInstance("src/Images/logo for pdf.png");
        Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see all projects from " + sd2 + " to " + ed2);
        image.setAlignment(Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 2f, 2f, 2f, 2f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Project ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Project Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Start Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Due Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Status", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5;
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String sd = dateFormat1.format(pBeginDateChooser.getDate());
        String ed = dateFormat1.format(pEndDateChooser.getDate());

        if (report_status.equals("All")) {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where due_date >= '"
                        + sd + "' and due_date <= '" + ed + "' and start_date >= '" + sd
                        + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where status = '"
                        + report_status + "' and due_date >= '" + sd + "' and due_date <= '" + ed
                        + "' and start_date >= '" + sd + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        }

        //add the PDF table to the paragraph 
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nProject Status Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(report_status, date, repID);
}

From source file:co.com.realtech.mariner.util.files.PDFUtils.java

public static File agregarTexto(byte[] bytes, String text) {
    File temp = null;/* w  w w . j a  va2 s.c  om*/
    try {
        temp = File.createTempFile("archivo", ".pdf");
        OutputStream oos = new FileOutputStream(temp);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, oos);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        //InputStream targetStream = new FileInputStream(initialFile);
        // Load existing PDF
        PdfReader reader = new PdfReader(bis);
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        // Copy first page of existing PDF into output PDF
        document.setPageSize(reader.getPageSize(1));
        document.newPage();
        cb.addTemplate(page, 0, 0);

        ColumnText ct = new ColumnText(cb);
        Phrase myText = new Phrase(text);
        Font fuente = new Font();
        fuente.setSize(6);
        myText.setFont(fuente);
        ct.setSimpleColumn(myText, 0, -1, document.right(), document.top(), -10, Element.ALIGN_RIGHT);
        ct.go();

        ColumnText ct2 = new ColumnText(cb);
        Phrase myText2 = new Phrase(text);
        Font fuente2 = new Font();
        fuente2.setSize(6);
        myText2.setFont(fuente);
        ct2.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 248, Element.ALIGN_RIGHT);
        ct2.go();

        ColumnText ct3 = new ColumnText(cb);
        Phrase myText3 = new Phrase(text);
        Font fuente3 = new Font();
        fuente3.setSize(6);
        myText3.setFont(fuente);
        ct3.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 505, Element.ALIGN_RIGHT);
        ct3.go();

        document.close();
    } catch (Exception e) {
        System.out.println("e = " + e);
    }
    return temp;
}

From source file:co.ordinate.printer.NewJFrame.java

private void A4BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_A4BTNActionPerformed

    new SwingWorker<Object, Object>() {
        String filename;/*  ww w  .java 2s  . c om*/

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");

        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A4.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.A4);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 2);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 15;
                    }
                    h += 2;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");

            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute();

    // TODO add your handling code here:
}

From source file:co.ordinate.printer.NewJFrame.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    new SwingWorker<Object, Object>() {
        String filename;/*  ww  w .jav a2s  .c  o  m*/

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");

        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("letter.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.LETTER);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 4);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 25;
                    }
                    h += 5;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");

            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute();

    // TODO add your handling code here:
}

From source file:co.ordinate.printer.NewJFrame.java

private void printA6Sheet() {
    new SwingWorker<Object, Object>() {
        String filename;// ww w  .  j a  va 2s  .  co  m

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");
            System.err.println("Printing Done");
        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A6.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.A5);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 4);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 30;
                    }
                    h += 8;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");
            System.err.println("Printing Done");
            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute(); // TODO add your handling code here:

}

From source file:com.algoboss.erp.util.report.PDFExporter2.java

License:Apache License

public void export(FacesContext context, DataTable table, String filename, String encodingType)
        throws IOException {
    try {//from  w  w  w .j av  a2s  .  com
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);

        if (!document.isOpen()) {
            document.open();
        }
        document.setMargins(1f, 1f, 1f, 1f);
        document.setPageSize(PageSize.A4);
        document.add(exportPDFTable(context, table, false, false, true, encodingType));
        document.setMargins(1f, 1f, 1f, 1f);
        document.setPageSize(PageSize.A4);

        document.close();

        //writePDFToResponse(context.getExternalContext(), baos, filename);
        writePDFToResponseNew(context.getExternalContext(), baos, filename);

    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
}