0

我是编程的初学者,我正在使用包括 WindowBuilder 在内的 eclipse kepler 我有一个问题,在我的应用程序中,在部署之前我以 pdf 格式从数据库(MySQl)打印数据,它运行良好,但在部署之后(使用 Lauch4j 和 InnoSetup 编译器)它不可能,当我点击打印按钮时没有反应!你能帮我吗?这是我用来写pdf的功能代码

公共无效 imprimerFacture() 抛出 DocumentException {

    Document document = new Document(PageSize.A4);
    Date str = new Date();
    SimpleDateFormat sdt = new SimpleDateFormat("dd/MM/yyyy");
    String strdat = sdt.format(str);
    Table tableau = new Table(8,2);

    try {

     PdfWriter.getInstance(document, new FileOutputStream("src/images/facture.pdf"));
     document.open()

    Paragraph phrase = new Paragraph(new Chunk("Le "+strdat,FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE)).setBackground(Color.yellow));
    phrase.setAlignment(Element.ALIGN_TOP);
    phrase.setAlignment(Element.ALIGN_RIGHT);
    document.add(phrase);

    Paragraph ftitre = new Paragraph("Magasin   :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
    //ftitre.setUnderline(2f, -5f);
    ftitre.setAlignment(Element.ALIGN_TOP);
    ftitre.setAlignment(Element.ALIGN_LEFT);
    document.add(ftitre);

    Paragraph ftitre1 = new Paragraph("Adresse   :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
    //ftitre.setUnderline(2f, -5f);
    ftitre1.setAlignment(Element.ALIGN_TOP);
    ftitre1.setAlignment(Element.ALIGN_LEFT);
    document.add(ftitre1);


    Paragraph ftitre2 = new Paragraph("Téléphone :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
    //ftitre.setUnderline(2f, -5f);
    ftitre2.setAlignment(Element.ALIGN_TOP);
    ftitre2.setAlignment(Element.ALIGN_LEFT);
    document.add(ftitre2);

    Chunk chunk = new Chunk("Commandes disponibles",FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, Color.BLUE));
            chunk.setUnderline(0.2f,-2f);

            //chunk.ALIGN_CENTER
            document.add(chunk);

        tableau.setAutoFillEmptyCells(true);



    } catch (DocumentException de) {de.printStackTrace();} 
      catch (IOException ioe) {ioe.printStackTrace();}
    try {
        Statement state = Connection_bd.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet res = state.executeQuery("select ReferenceCommande,Designation,Quantite,DateLivraison,Categorie,MontantCommande,Date,IdentifiantClient from Commande");
        //ResultSetMetaData meta = res.getMetaData();
        tableau.addCell("ReferenceCommande");
        tableau.addCell("Designation");
        tableau.addCell("Quantite");
        tableau.addCell("DateLivraison");
        tableau.addCell("Categorie");
        tableau.addCell("MontantCommande");
        tableau.addCell("Date");
        tableau.addCell("IdentifiantClient");
        tableau.setWidth(110);

        while(res.next()){


            tableau.addCell(res.getString("ReferenceCommande"));
            tableau.addCell(res.getString("Designation"));
            tableau.addCell(res.getString("Quantite"));
            tableau.addCell(res.getString("DateLivraison"));
            tableau.addCell(res.getString("Categorie"));
            tableau.addCell(res.getString("MontantCommande"));
            tableau.addCell(res.getString("Date"));
            tableau.addCell(res.getString("IdentifiantClient"));



        }  
        document.add(tableau);
        //on ferme le tout                         
        res.close();
        state.close();
        }catch(SQLException e){}
    Paragraph ftitre = new Paragraph("Magasinier",FontFactory.getFont(FontFactory.COURIER,10,Font.BOLD,Color.BLUE));
    ftitre.setAlignment(Element.ALIGN_RIGHT);
    document.add(ftitre);
     document.close();

}

我在此按钮中调用的函数

btnImprimer_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {

             int result=JOptionPane.showConfirmDialog(null,"Voulez-vous réellement imprimer ce(s) commande(s) ?","GE-SHOP1.0 SOFTWARE_MESSAGE",JOptionPane.YES_NO_OPTION); 
                if(result==JOptionPane.OK_OPTION) 
                {
                    String str="select * from commande";
                    String strin=AfficherChaine(str);
                    if(strin.equals(""))
                    { JOptionPane.showMessageDialog(null,"Cette action ne peut pas aboutir du fait qu'il n'y a rien à imprimer!!!\n Veuillez saisir la(les) nouvelle(es) commande(es) et puis proceder a l'impression !","GE-SHOP1.0 SOFTWARE_MESSAGE",JOptionPane.WARNING_MESSAGE);}




                else {try {

                //if (listachat.size() > 0){
                      imprimerFacture();


                        if(Desktop.isDesktopSupported()){
                            if(Desktop.getDesktop().isSupported(java.awt.Desktop.Action.OPEN)){
                            try {
                                java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));
                            } catch (IOException ex) {
                                //Traitement de l'exception
                            }
                            catch (IllegalArgumentException e){}
                            } else {
                                JOptionPane.showMessageDialog(null, "La fonction n'est pas supportée par votre système d'exploitation","GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.ERROR_MESSAGE);
                            }
                        }else{
                            JOptionPane.showMessageDialog(null, "Desktop pas supportée par votre système d'exploitation", "GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.ERROR_MESSAGE);
                        }


                } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                  }

                }} 

                else{if(result==JOptionPane.NO_OPTION)
                {JOptionPane.showMessageDialog(null, "L'operation d'impression vient d'etre annulée", "GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.INFORMATION_MESSAGE);}

                }







        }
    });
4

2 回答 2

0
java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));

我认为问题就在这里。当您部署为 jar 文件时,您的资源无法像那样被访问。

您应该使用 getResource。参考

你可以试试

String path =Yourclass.class.getResource("/images/facture.pdf").getPath();
java.awt.Desktop.getDesktop().open(new File(path));
于 2013-07-15T09:47:40.967 回答
0

路径是问题。

PdfWriter.getInstance(document, new FileOutputStream("src/images/facture.pdf"));

更好地保留您自己的目录用于读/写数据,通常在用户的家中。您还可以创建一个临时文件(请参阅文件)。

File applicationDir = new File(System.getProperty("user.home")
    + File.sepator + ".GE-Shop");
applicationDir.mkdirs();
File pdfFile = new File(applicationDir, "facture.pdf");
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));

...

Desktop.getDesktop().open(pdfFile);
于 2013-07-15T09:49:33.477 回答