打印由ByteArrayOutputStream创建的iText pdf文件

时间:2015-07-29 06:15:27

标签: java pdf itext

我编码通过ByteArrayOutputStream点击按钮在内存中创建iText PDF文件。

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

以下是特定按钮的代码;

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

}                                      

但是上面的代码显示了如下的异常;

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);

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

1 个答案:

答案 0 :(得分:1)

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

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