首页 > 解决方案 > 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);
    }

标签: javapdfnetbeansjarpdfbox

解决方案


推荐阅读