通过 JFilechooser 选择一个目录并将 .pdf 格式的 Itext Report 保存到 Java 中的该目录
Pick up a directory by JFilechooser and save Itext Report in .pdf to that directory in Java
我为 Netbeans 的备份功能创建了这个表格。
当我 select 从 Jdaychooser(我的代码中包含日期验证)日期并单击“确定”按钮时,JFilechooser 打开以询问保存目录。(如下面的屏幕截图所示)。
我想完成以下功能;
单击“确定”时,文本生成的报告必须从 JFilchooser 保存到 selected 目录中..
我编码但不是 100% 工作(它只在我的项目包含目录中保存 pdf)
帮助我更正我的代码...
执行确定按钮操作的方法;
private void backupOKActionPerformed(java.awt.event.ActionEvent evt) {
int result;
Date nowdate = new Date(System.currentTimeMillis());
Date backday = backup_date.getDate();
if(backday==null){//checking the date is null or not
JOptionPane.showMessageDialog(null, "Please Enter the Date ...");
}else if(!backday.before(nowdate)){//checking given date before todays date or not
JOptionPane.showMessageDialog(null, "Please Enter Date before Todays date...");
}else{
// backup function goes here
chooser = new JFileChooser();
// chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Save Backup");
chooser.setApproveButtonText("Save");
//disables the all filesoptioning here
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
// System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
// System.out.print("getSelectedFile() : "+chooser.getSelectedFile());
// creating the pdf for supplier details
try {
Document pdfsup = new Document();
PdfWriter.getInstance(pdfsup, new FileOutputStream("Supplier Details Report.pdf"));
pdfsup.open();
Image imgsup = Image.getInstance("hedder.png");
// pdfsup.add(new Paragraph("Suppliers"));
pdfsup.add(imgsup);
pdfsup.add(new Paragraph("Supplier Details Report",FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE)));
pdfsup.add(new Paragraph(new Date().toString()));
pdfsup.add(new Paragraph("----------------------------------------------------------------------------------------------------------------"));
PdfPTable tablesup= new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph("Title"));
cell.setColspan(4);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(BaseColor.PINK);
tablesup.addCell(cell);
tablesup.addCell("Supplier ID");
tablesup.addCell("Supplier ID2");
tablesup.addCell("Supplier ID3");
tablesup.addCell("Supplier ID4");
pdfsup.add(tablesup);
pdfsup.close();
JOptionPane.showMessageDialog(null, "Report Saved...");
} catch (DocumentException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
System.out.println("No Selection");
}
}
}
new FileOutputStream("Supplier Details Report.pdf")
将简单地创建对当前工作目录中文件的引用,它没有关联的路径信息,你基本上忽略了 JFileChooser
提供的任何内容。
考虑使用更像...
PdfWriter.getInstance(pdfsup, new FileOutputStream(new File(chooser.getSelectedFile(), "Supplier Details Report.pdf")));
它使用 JFileChooser
中的 selectedFile
,假设您只允许目录选择
我为 Netbeans 的备份功能创建了这个表格。
当我 select 从 Jdaychooser(我的代码中包含日期验证)日期并单击“确定”按钮时,JFilechooser 打开以询问保存目录。(如下面的屏幕截图所示)。
我想完成以下功能;
单击“确定”时,文本生成的报告必须从 JFilchooser 保存到 selected 目录中..
我编码但不是 100% 工作(它只在我的项目包含目录中保存 pdf)
帮助我更正我的代码...
执行确定按钮操作的方法;
private void backupOKActionPerformed(java.awt.event.ActionEvent evt) {
int result;
Date nowdate = new Date(System.currentTimeMillis());
Date backday = backup_date.getDate();
if(backday==null){//checking the date is null or not
JOptionPane.showMessageDialog(null, "Please Enter the Date ...");
}else if(!backday.before(nowdate)){//checking given date before todays date or not
JOptionPane.showMessageDialog(null, "Please Enter Date before Todays date...");
}else{
// backup function goes here
chooser = new JFileChooser();
// chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Save Backup");
chooser.setApproveButtonText("Save");
//disables the all filesoptioning here
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
// System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
// System.out.print("getSelectedFile() : "+chooser.getSelectedFile());
// creating the pdf for supplier details
try {
Document pdfsup = new Document();
PdfWriter.getInstance(pdfsup, new FileOutputStream("Supplier Details Report.pdf"));
pdfsup.open();
Image imgsup = Image.getInstance("hedder.png");
// pdfsup.add(new Paragraph("Suppliers"));
pdfsup.add(imgsup);
pdfsup.add(new Paragraph("Supplier Details Report",FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE)));
pdfsup.add(new Paragraph(new Date().toString()));
pdfsup.add(new Paragraph("----------------------------------------------------------------------------------------------------------------"));
PdfPTable tablesup= new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph("Title"));
cell.setColspan(4);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(BaseColor.PINK);
tablesup.addCell(cell);
tablesup.addCell("Supplier ID");
tablesup.addCell("Supplier ID2");
tablesup.addCell("Supplier ID3");
tablesup.addCell("Supplier ID4");
pdfsup.add(tablesup);
pdfsup.close();
JOptionPane.showMessageDialog(null, "Report Saved...");
} catch (DocumentException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
System.out.println("No Selection");
}
}
}
new FileOutputStream("Supplier Details Report.pdf")
将简单地创建对当前工作目录中文件的引用,它没有关联的路径信息,你基本上忽略了 JFileChooser
提供的任何内容。
考虑使用更像...
PdfWriter.getInstance(pdfsup, new FileOutputStream(new File(chooser.getSelectedFile(), "Supplier Details Report.pdf")));
它使用 JFileChooser
中的 selectedFile
,假设您只允许目录选择