java - PdfBox,将程序用作JAR文件时不保存pdf文件
问题描述
我正在尝试创建一个将生成发票的 JAVA 程序。我正在使用 PDFBox 创建 PDF 文件。在 Netbeans 中一切正常,但是,当我将程序编译成 JAR 文件时,不会创建 PDF 文件。此外,一旦创建文件,它就会同时执行。一切都发生在 1 点击。正如我之前提到的,它在 Netbeans 中确实有效,但在将文件编译为 JAR 文件时无效。我是JAVA的初学者,所以对它了解不多。一个简单快捷的解决方案或想法将非常有帮助并受到高度赞赏。提前致谢。
private void createPDF() throws IOException {
File file = new File("invoice.pdf");
PDDocument doc = PDDocument.load(file);
PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
String logoImage = "logo.jpg";
try {
PDXObjectImage imageLogo = new PDJpeg(doc, new FileInputStream(logoImage));
PDPageContentStream content = new PDPageContentStream(doc, page, true, true);
content.drawImage(imageLogo, 175, 675);
content.close();
File f = new File("D:/invoiceGenerator/invoices/" + invoiceNo + ".pdf");
FileOutputStream fOut = new FileOutputStream(f);
try {
doc.save(fOut);
JOptionPane.showMessageDialog(null,"file created");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
doc.close();
rowCount = 0;
String fileName = "D:/invoiceGenerator/invoices/" + invoiceNo + ".pdf";
try {
File fileToOpen = new File(fileName);
if (fileToOpen.exists()) {
Desktop.getDesktop().open(fileToOpen);
} else {
JOptionPane.showMessageDialog(null, "File not exits -> " + fileToOpen.getAbsolutePath());
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
解决方案
推荐阅读
- django - nginx/django 文件下载权限
- vue.js - 过渡组在 Vue 的嵌套组件中不起作用
- java - JSON 中的阿拉伯字符
- oracle - 月份的日期必须介于 1 和月份的最后一天之间
- haskell - Haskell 接收两个带有 int 的列表并返回一个元组
- excel - ADODB 到查询表:运行时错误 1004
- visual-studio-code - 如何让 VS Code 复合调试器默认运行?
- ios - iOS13 - 具有自定义 hidesSearchBarWhenScrolling 行为的 UITableView + UISearchController
- flutter - 使用手机信号塔持续更新位置
- sql-server - 在存储过程中执行多个命令