通过 ByteArrayOutputStream 打印创建的 iText pdf 文件

Printing created iText pdf file by ByteArrayOutputStream

我通过 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.access0(Newsale.java:51)
at com.bit.project.Newsale.actionPerformed(Newsale.java:504)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

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

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

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

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