hey guys,
I have problem in printing from java
i have used PrinterJob in awt
but it's going mad sometimes . it's going to it's print method
two times
some times doesn't print anything
the code is
=========================================================
import java.awt.print.Printable;
import java.awt.print.Book;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.*;
import javax.print.*;
import java.awt.print.*;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.Copies;
import javax.print.PrintServiceLookup;
import javax.print.attribute.PrintJobAttributeSet;
class PrintStringTest {
public static void main(String args[]){
int lines = 0;
int totalLines = 30;
String str = "bgfgkjdffffffffffffffffffffffffffffffffffffffffdghdfkjgkjdfgkjdfgk"+
"fdfdsf"+System.getProperty("line.separator")+"dfdsfsssssssssssssss"+
"fdfdxxxxxxxxsf"+System.getProperty("line.separator")+"dfdsfsssssssssssssss"+
"fxxxdfdsf"+System.getProperty("line.separator")+"dfdaaaaaaaasssssssssssssssssssss"+
"fdfxxxxxxxdsf"+System.getProperty("line.separator")+"dfdsfssssssssssssxxxxxxxxxsss";
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat landscape = job.defaultPage();
System.out.println ("hight"+landscape.getImageableHeight());
System.out.println ("how many lines"+landscape.getHeight()/10);
System.out.println ("orientation"+landscape.getOrientation());
System.out.println ("orientation"+PageFormat.PORTRAIT);
String[] array = str.split(System.getProperty("line.separator"));
int linex = (int)landscape.getImageableHeight()/10;
int total = (int)array.length/linex;
System.out.println (total);
test t = new test();
Book book = new Book();
System.out.println ("length of string"+str.length());
System.out.println (str);
landscape.setOrientation(PageFormat.PORTRAIT);
StringBuffer strb = new StringBuffer();
int i = 0;
while(total-- >= 0){
strb = new StringBuffer();
for(lines = 0;lines < linex && lines < array.length ;lines++)
strb.append( array[lines]+"@");
t.setString(strb.toString());
book.append(t,landscape, 1);
System.out.println ("content to print"+t.getString());
// i+=lines;
}
System.out.println (book.getNumberOfPages());
job.setPageable(book);
if(job.printDialog()){
try {
job.print();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class test implements Printable{
private String str;
private boolean isPrinted;
public void setString(String str){
this.str = str;
}
public String getString(){
return this.str;
}
public int print(Graphics g,PageFormat p,int index){
int count = 0;
System.out.println("print "+index);
if(!isPrinted) {
isPrinted = true;
return Printable.PAGE_EXISTS;
}
try {
if(index >= 2){
return Printable.NO_SUCH_PAGE;
}
int y = 10;
int i=0;
if(str == null) {
System.out.println ("NO_SUCH_PAGE");
return Printable.NO_SUCH_PAGE;
}
String[] s= str.split("@");
System.out.println ("====>"+str);
while( i < s.length && s[i] != null ) {
drawGraphics(g,p,str,y);
y+=10;
i++;
}
}catch(Exception e){
e.printStackTrace();
}
return Printable.PAGE_EXISTS;
}
public void drawGraphics(Graphics g,PageFormat p,String str,int y){
System.out.println ("upto drawGraphics");
System.out.println ("**"+str);
g.drawString(str,75,y);
g.drawString("hey upto here",75,y+=5);
}
}
=====================================================
can anybdy help me in this thing
thankx in advance :))