通常,我会尝试在网站上搜索解决方案。但是在这里找不到解决方案。我想允许用户选择pdf文件的保存位置和文件名。我通过使用iTextpdf库生成pdf。按照我使用的代码,它将pdf文件保存在预定义的名称和根文件夹中。这是代码
try{
Document document = new Document();
PdfWriter writer =PdfWriter.getInstance(document, new FileOutputStream("Supplier Details Report.pdf"));
document.open();
//code for generate pdf
document.close();
JOptionPane.showMessageDialog(null, "PDF Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
我在您的代码中看到您正在使用Swing。您可以使用JFileChooser类。它具有一些基本的文件选择器布局。其中之一是保存对话框。
JFrame parentComponent = new JFrame();
JFileChooser fileChooser= new JFileChooser();
// Some init code, if you need one, like setting title
int returnVal = fileChooser.showOpenDialog(parentComponent)
if ( returnValue == == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
try{
Document document = new Document();
PdfWriter writer =PdfWriter.getInstance(document, new FileOutputStream(fileToSave ));
document.open();
//code for generate pdf
document.close();
JOptionPane.showMessageDialog(null, "PDF Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}