gpt4 book ai didi

javafx - 如何将TableView中的数据写入pdf?

转载 作者:行者123 更新时间:2023-12-02 04:51:51 25 4
gpt4 key购买 nike

我尝试了很多并得到了下面的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 吗?

最佳答案

使用itext pdf,您可以在javafx中创建pdf。这是将数据库值写入pdf表格单元格的示例代码。您可以使用与在javafx中将数据写入 TableView 相同的sql查询。下载 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();
}

我认为这会对您有所帮助!如有疑问请评论。

关于javafx - 如何将TableView中的数据写入pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com