gpt4 book ai didi

java - 通过 JFilechooser 选择一个目录并将 .pdf 中的 Itext Report 保存到 Java 中的该目录

转载 作者:行者123 更新时间:2023-11-30 06:59:24 27 4
gpt4 key购买 nike

enter image description here

我为 Netbeans 的备份功能创建了这个表单。

当我从 Jdaychooser 中选择日期(我的代码中包含日期验证)并单击“确定”按钮时,JFilechooser 打开以询问保存目录。(如下面的屏幕截图所示)。

我想完成以下功能;

当点击确定时,文本生成的报告必须保存到从 JFilchooser 中选择的目录中。

我编码但不是 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,假设您只允许目录选择

关于java - 通过 JFilechooser 选择一个目录并将 .pdf 中的 Itext Report 保存到 Java 中的该目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591378/

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