使用程序作为JAR文件时,PdfBox,pdf文件未保存

时间:2018-07-01 11:52:35

标签: java pdf netbeans jar pdfbox

我正在尝试创建一个将生成发票的JAVA程序。我正在使用PDFBox创建PDF文件。在Netbeans中,一切正常,但是,当我将该程序编译为JAR文件时,不会创建PDF文件。而且,文件一旦创建,就将同时执行。一切只需点击一下即可。正如我之前提到的,它在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);
    }

0 个答案:

没有答案
相关问题