打印由ByteArrayOutputStream创建的iText pdf文件 [英] Printing created iText pdf file by ByteArrayOutputStream

查看:596
本文介绍了打印由ByteArrayOutputStream创建的iText pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码通过 ByteArrayOutputStream 在内存中创建iText PDF文件,只需点击一下按钮即可。

I coded for creating iText PDF file in the memory by ByteArrayOutputStreamby click of a button.

然后编码以在同一时间点击同一按钮时打印该PDF文件。

以下是特定按钮的代码;

Below is my code for the specific button;

 private void ok_btnActionPerformed(java.awt.event.ActionEvent evt) {                                       

    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
        String sql1 = "Select * from supplierinfo";

        pst=conn.prepareStatement(sql1);
        rs=pst.executeQuery();

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


        salepdf.open();
        //I added content here for the PDF file
        salepdf.close();

      try{ 
        byte[] pdfbyte = baos.toByteArray();
        //System.out.println(pdf);
        InputStream bis = new ByteArrayInputStream(pdfbyte);
        SimpleDoc pdfp = new SimpleDoc(bis, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        DocPrintJob printjob= printService.createPrintJob();
        printjob.print(pdfp, new HashPrintRequestAttributeSet());
        bis.close();

      }catch(IOException e){
            JOptionPane.showMessageDialog(null, "EEE :"+e);
            e.printStackTrace();


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

}                                      

但上面的代码显示异常为下面;

But above code shows an Exception as below;

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(SimpleDoc.java:103)
at com.bit.project.Newsale.ok_btnActionPerformed(Newsale.java:850)
at com.bit.project.Newsale.access$900(Newsale.java:51)
at com.bit.project.Newsale$12.actionPerformed(Newsale.java:504)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

第850行是 SimpleDoc pdfp = new SimpleDoc(bis, DocFlavor.BYTE_ARRAY.AUTOSENSE,null);

我在代码中做了什么错误?

What is error I done in the code?

推荐答案

由于您正在使用Stream,DocFlavor的正确格式是 DocFlavor.INPUT_STREAM 而不是 DocFlavor.BYTE_ARRAY

Since you are using Stream the right format for DocFlavor is DocFlavor.INPUT_STREAM instead of DocFlavor.BYTE_ARRAY:

SimpleDoc pdfp = new SimpleDoc(bis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);

这篇关于打印由ByteArrayOutputStream创建的iText pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆