I coded for creating iText PDF file in the memory by ByteArrayOutputStream
by 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?
ByteArrayOutputStream
writing the creating iText file to the memory(not saving,temporary file)@Satya – user5167914