samedi 30 mai 2015

what is the best way to print invoice in java

in my desktop application (POS System). I used IText api for generating invoices and printing, but my printer thermal invoice printer don't support .pdf file. only supporting text file and .docx file. i use simple text file printer print whole invoice in long vertical single word line and don't auto cut page. I used .docx file which works good, i got print as i designed. but my program first open document in ms word then give me print. my code is:

try

     {


            FileOutputStream output = new FileOutputStream(FILE);                   
                    XWPFDocument doc = new XWPFDocument();

                    CTBody body = doc.getDocument().getBody();
                    if(!body.isSetSectPr()){
                    body.addNewSectPr();
                    }

                    CTSectPr section = body.getSectPr();
                    if(!section.isSetPgSz()){
                    section.addNewPgSz();
                    }
                    CTPageSz pageSize = section.getPgSz();
                    pageSize.setOrient(STPageOrientation.PORTRAIT);

                    int value = 4000+(gui.model.getRowCount()*1000);

                    pageSize.setW(BigInteger.valueOf(4050));
                    pageSize.setH(BigInteger.valueOf(value));

                    CTPageMar pageMar = section.addNewPgMar();
                    pageMar.setLeft(BigInteger.valueOf(400L));
                    pageMar.setTop(BigInteger.valueOf(0L));
                    pageMar.setRight(BigInteger.valueOf(0L));
                    pageMar.setBottom(BigInteger.valueOf(0L));

                     XWPFParagraph para = doc.createParagraph();
                     para.setAlignment(ParagraphAlignment.LEFT);
                     XWPFRun run  = para.createRun();
                     para.setWordWrap(true);
                     run.setBold(true);
                     run.setFontSize(10);
                     run.setText("          "+address.shopName);
                     run.addBreak();
                     run.setText("                        "+address.phoneNo);
                     run.addBreak();
                     run.setText("   "+address.description);
                     run.addBreak();
                     para = doc.createParagraph();
                    para.setAlignment(ParagraphAlignment.LEFT);
                    run  = para.createRun();
                     para.setWordWrap(true);
                     run.setFontSize(10);
                     run.setText("Invoice No."+invoiceno);
                     run.addBreak();
                     run.setText("Type: "+table);
                     run.addBreak();
                     run.setText("Customer Name: "+name+"    "+tempObj);
                     run.addBreak();
                     run.setText("--------------------------------------------------------");
                     run.addBreak();
                     run.setText("Product              Qty          Price          Total");
                     run.addBreak();
                     run.setText("--------------------------------------------------------");
                     run.addBreak();

                String temp = null;
                for(int i = 0 ; i < gui.table.getRowCount(); i++){
                    temp = gui.table.getValueAt(i, 1).toString();
                    String quanstr = gui.table.getValueAt(i, 2)+"";
                    String unitPricestr = gui.table.getValueAt(i, 3)+"";
                    String totalstr =gui.table.getValueAt(i, 4)+"";

                    run.setText(temp);run.addBreak();
                     run.setText("                            "+quanstr+"          "+unitPricestr+"          "+totalstr);
                     run.addBreak();
                }
                double subTotal = tableTotalCounter();
                run.setText("--------------------------------------------------------");run.addBreak();
                run.setText("Discount: "+dis+"%");run.addBreak();
                run.setText("Sub total: "+(subTotal - (subTotal*dis/100)));run.addBreak();
                run.setText("Cash: "+cash);run.addBreak();
                run.setText("Balance: "+(cash-(subTotal - (subTotal*dis/100))));
                run.addBreak();
                doc.write(output); 
                output.close();

                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    System.out.println("Exception");
                    e1.printStackTrace();
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("Exception");
                }

            if(confirmation("Print invoice?","Confirmation")==0){
                Desktop desktop = Desktop.getDesktop();
                try {

                     desktop.print(new File(FILE));
                } catch (IOException e) {           
                    e.printStackTrace();
                }
      }

please tell me how to print without getting that file open. and there is any other way to print invoice.

Aucun commentaire:

Enregistrer un commentaire