如何在部署应用程序后以pdf格式打印

时间:2013-07-15 09:39:57

标签: java

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

public void 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);}

                }







        }
    });

2 个答案:

答案 0 :(得分:0)

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

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

你应该使用getResource。 Reference

您可以尝试

String path =Yourclass.class.getResource("/images/facture.pdf").getPath();
java.awt.Desktop.getDesktop().open(new File(path));

答案 1 :(得分: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);