1

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

Then coded to print that PDF file when click of that same button same time.

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)

Line 850 is SimpleDoc pdfp = new SimpleDoc(bis, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);.

What is error I done in the code?

  • I'm using `ByteArrayOutputStream` writing the creating iText file to the memory(not saving,temporary file)@Satya –  Jul 29 '15 at 06:23

1 Answers1

1

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);
vadchen
  • 1,442
  • 1
  • 11
  • 14
  • Then showing a `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.bit.project.Newsale.ok_btnActionPerformed(Newsale.java:851)` Line 851 is`DocPrintJob printjob= printService.createPrintJob();` @vadchen –  Jul 29 '15 at 06:55
  • 1
    Looks like `printService` is null – vadchen Jul 29 '15 at 06:57
  • yes I declared as `PrintService printService;` How to fix the error @vadchen –  Jul 29 '15 at 07:04
  • You can see some examples here: http://www.javaexperience.com/java-file-printing-example-to-print-files-in-java/ – vadchen Jul 29 '15 at 07:08
  • Thanks a lot :) I'm facing a new problem now:( changed the question.. I appreciate if you can help :) –  Jul 29 '15 at 11:10
  • @hinata Instead of changing original question you should create new one. Now somebody who have similar problem, will see new question with old (and now irrelevant) answer. – vadchen Jul 29 '15 at 11:18
  • 1
    Yes That's stupid of me :( I rolled back the original question. [My new question](http://stackoverflow.com/questions/31699267/not-printing-created-itext-pdf-file-by-bytearrayoutputstream) @vadchen –  Jul 29 '15 at 11:33