如何将数据从TableView写入pdf?

时间:2015-10-28 07:41:44

标签: javafx javafx-2 itext

我尝试了很多,并在下面获得了itext pdf代码。但是我想将数据从TableView写入pdf。如何实现这一目标?

  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;
    }
} 

上面的代码是java中的itext pdf。我可以创建一个包含javafx表中数据的PDF吗?有没有其他方法可以使用JavaFX应用程序构建pdf?

1 个答案:

答案 0 :(得分:3)

使用 itext pdf ,您可以在javafx中创建pdf。这是将数据库值写入pdf表格单元格的示例代码。您可以使用相同的SQL查询将数据写入javafx中的tableview。下载itextpdf和sqlconnector jar文件。

 @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();
    }

我认为这会对你有所帮助!任何疑问评论。