Not Printing created iText pdf file by ByteArrayOutputStream

爱⌒轻易说出口 提交于 2019-12-11 03:03:36

问题


I coded for creating iText PDF file in the memory by ByteArrayOutputStream by click of a button and print that PDF file when click of that same button same time..Code is error free..

Below is my code for the specific button;

int print = JOptionPane.showConfirmDialog(null, "Do you really want to Print the Invoice ?","Print Confirmation",JOptionPane.YES_NO_OPTION);
    if((print)==0){
       try{

        String saledate = ((JTextField)dayChooser.getDateEditor().getUiComponent()).getText();
        String invoice = InvoiceNo_txt.getText();
        String citems = countitems_txt.getText();
        String tDis =totalDiscount_txt.getText();
        String ntotal = NetTotal_txt.getText();

        //setting data to saleinfo db table
        try{
            String sql = "Insert into saleinfo (SaleDate,InvoiceNo,TotalItems,TotalDiscount,NetTotal)values (?,?,?,?,?)";

            pst=conn.prepareStatement(sql);


            pst.setString(1, saledate);
            pst.setString(2, invoice);
            pst.setString(3, citems);
            pst.setString(4, tDis);
            pst.setString(5, ntotal);

            pst.execute();

        }catch(Exception e){

        }


        //creting itext report for prining


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document salepdf = new Document();
        PdfWriter.getInstance(salepdf,baos);
        //salepdf.setPageSize(PageSize.A7);



        salepdf.open();
       //file content added here
        salepdf.close();


        byte[] pdfbyte = baos.toByteArray();

        ByteArrayInputStream bis = new ByteArrayInputStream(pdfbyte);
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        AttributeSet attributeSet = new HashAttributeSet();
        attributeSet.add(new PrinterName("NPI8DA48A", null));
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();

        DocPrintJob job= service.createPrintJob();
        Doc pdfp = new SimpleDoc(bis, flavor, null);
        PrintJobWatcher watcher = new PrintJobWatcher(job);
        job.print(pdfp, null); 
        watcher.waitForDone(); 



    }catch(DocumentException | PrintException ex){
        Logger.getLogger(Newsale.class.getName()).log(Level.SEVERE, null, ex);

    }
    //set null  feilds after printing
    DefaultTableModel model = (DefaultTableModel) tableSale.getModel();
    model.setRowCount(0);
    countitems_txt.setText("");
    totalDiscount_txt.setText("");
    NetTotal_txt.setText("");
    }

The class PrintJobWatcher ;

static class PrintJobWatcher {
    // true iff it is safe to close the print job's input stream
    boolean done = false;

    PrintJobWatcher(DocPrintJob job) {
        // Add a listener to the print job
        job.addPrintJobListener(new PrintJobAdapter() {
            public void printJobCanceled(PrintJobEvent pje) {
                allDone();
            }
            public void printJobCompleted(PrintJobEvent pje) {
                allDone();
            }
            public void printJobFailed(PrintJobEvent pje) {
                allDone();
            }
            public void printJobNoMoreEvents(PrintJobEvent pje) {
                allDone();
            }
            void allDone() {
                synchronized (PrintJobWatcher.this) {
                    done = true;
                    PrintJobWatcher.this.notify();
                }
            }
        });
    }
    public synchronized void waitForDone() {
        try {
            while (!done) {
                wait();
            }
        } catch (InterruptedException e) {
        }
    }
}

Note this clearly this;
The problem is that when I click Print Button ,The file is added to the printing queue and disappears instantly..

Why the code is not Working ?

来源:https://stackoverflow.com/questions/31699267/not-printing-created-itext-pdf-file-by-bytearrayoutputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!