Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write data from a TableView to the pdf?

I tried a lot and got itext pdf code below. However I want to write data from a TableView to the pdf. How can this be achieved ?

  public class Pdf2 {
    public static final String RESULT
        = "c:/temp/FirstPdf.pdf";

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws IOException, DocumentException {
        new Pdf2().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * @param    filename the name of the PDF file that will be created.
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename)
        throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(createFirstTable());
        // step 5
        document.close();
    }

    /**
     * Creates our first table
     * @return our first table
     */
    public static PdfPTable createFirstTable() {
        PdfPTable table = new PdfPTable(5);

    PdfPCell c1 = new PdfPCell(new Phrase("QTY"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Item"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Description"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
     c1 = new PdfPCell(new Phrase("Unit Price"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase("Total"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(1);

    table.addCell("1");
    table.addCell("2");
    table.addCell("3");
     table.addCell("4");
    table.addCell("5");


        return table;
    }
} 

This code above is itext pdf in java. Can I create a PDF with data from a javafx table inserted? Is there any other way to build pdf using JavaFX application?


1 Answers

Using itext pdf you can create pdf in javafx.Here is the sample code that write database values into pdf table cell.You can use the same sql query that writes data to the tableview in javafx.Download itextpdf and sqlconnector jar files.

 @FXML
       private void pdfs() 
          throws Exception{
        try {
           Class.forName("com.mysql.jdbc.Driver");
               Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "");
  Statement stmt = con.createStatement();
                    /* Define the SQL query */
                    ResultSet query_set = stmt.executeQuery("SELECT *From tablename");
                    /* Step-2: Initialize PDF documents - logical objects */
                    Document my_pdf_report = new Document();
                    PdfWriter.getInstance(my_pdf_report, new FileOutputStream("pdf_report_from_sql_using_java.pdf"));
                    my_pdf_report.open();            
                    //we have four columns in our table
                    PdfPTable my_report_table = new PdfPTable(4);
                    //create a cell object
                    PdfPCell table_cell;

                    while (query_set.next()) {                
                                    String dept_id = query_set.getString("code");
                                    table_cell=new PdfPCell(new Phrase(dept_id));
                                    my_report_table.addCell(table_cell);
                                    String dept_name=query_set.getString("category");
                                    table_cell=new PdfPCell(new Phrase(dept_name));
                                    my_report_table.addCell(table_cell);
                                    String manager_id=query_set.getString("total");
                                    table_cell=new PdfPCell(new Phrase(manager_id));
                                    my_report_table.addCell(table_cell);
                                    String location_id=query_set.getString("Sum");
                                    table_cell=new PdfPCell(new Phrase(location_id));
                                    my_report_table.addCell(table_cell);
                                    }
                    /* Attach report table to PDF */
                    my_pdf_report.add(my_report_table);                       
                    my_pdf_report.close();

                    /* Close all DB related objects */
                    query_set.close();
                    stmt.close(); 
                    con.close();               



    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

i think this will help you !any doubts comment.

like image 184
Seban Avatar answered Oct 25 '25 07:10

Seban