如何将数据从 TableView 写入 pdf?
How to write data from a TableView to the pdf?
我尝试了很多,得到了下面的 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 table 中的数据创建 PDF 吗?还有其他方法可以使用 JavaFX 应用程序构建 pdf 吗?
Using itext pdf you can create pdf in javafx.Here is the sample code write database values into pdf table cell.You can use sql 查询将数据写入 javafx.Download itextpdf 和 sql 连接器 jar 文件中的 table 视图。
@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();
}
我想这对你有帮助!有任何疑问请评论。
我尝试了很多,得到了下面的 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 table 中的数据创建 PDF 吗?还有其他方法可以使用 JavaFX 应用程序构建 pdf 吗?
Using itext pdf you can create pdf in javafx.Here is the sample code write database values into pdf table cell.You can use sql 查询将数据写入 javafx.Download itextpdf 和 sql 连接器 jar 文件中的 table 视图。
@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();
}
我想这对你有帮助!有任何疑问请评论。