Printing from java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 35
Reputation: tigerxx is an unknown quantity at this point 
Solved Threads: 0
tigerxx tigerxx is offline Offline
Light Poster

Printing from java

 
0
  #1
Feb 23rd, 2005
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

=========================================================
  1. import java.awt.print.Printable;
  2. import java.awt.print.Book;
  3. import java.awt.Graphics;
  4. import java.awt.print.PageFormat;
  5. import java.awt.print.PrinterJob;
  6. import java.io.File;
  7. import java.io.BufferedReader;
  8. import java.io.FileInputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.*;
  11. import javax.print.*;
  12. import java.awt.print.*;
  13. import javax.print.attribute.HashAttributeSet;
  14. import javax.print.attribute.standard.Media;
  15. import javax.print.attribute.standard.MediaSize;
  16. import javax.print.attribute.standard.Copies;
  17. import javax.print.PrintServiceLookup;
  18. import javax.print.attribute.PrintJobAttributeSet;
  19.  
  20.  
  21. class PrintStringTest {
  22.  
  23.  
  24.  
  25. public static void main(String args[]){
  26.  
  27.  
  28. int lines = 0;
  29. int totalLines = 30;
  30. String str = "bgfgkjdffffffffffffffffffffffffffffffffffffffffdghdfkjgkjdfgkjdfgk"+
  31. "fdfdsf"+System.getProperty("line.separator")+"dfdsfsssssssssssssss"+
  32. "fdfdxxxxxxxxsf"+System.getProperty("line.separator")+"dfdsfsssssssssssssss"+
  33. "fxxxdfdsf"+System.getProperty("line.separator")+"dfdaaaaaaaasssssssssssssssssssss"+
  34. "fdfxxxxxxxdsf"+System.getProperty("line.separator")+"dfdsfssssssssssssxxxxxxxxxsss";
  35.  
  36. PrinterJob job = PrinterJob.getPrinterJob();
  37.  
  38. PageFormat landscape = job.defaultPage();
  39.  
  40. System.out.println ("hight"+landscape.getImageableHeight());
  41. System.out.println ("how many lines"+landscape.getHeight()/10);
  42. System.out.println ("orientation"+landscape.getOrientation());
  43. System.out.println ("orientation"+PageFormat.PORTRAIT);
  44. String[] array = str.split(System.getProperty("line.separator"));
  45.  
  46. int linex = (int)landscape.getImageableHeight()/10;
  47.  
  48. int total = (int)array.length/linex;
  49.  
  50. System.out.println (total);
  51.  
  52. test t = new test();
  53.  
  54. Book book = new Book();
  55.  
  56. System.out.println ("length of string"+str.length());
  57. System.out.println (str);
  58.  
  59. landscape.setOrientation(PageFormat.PORTRAIT);
  60.  
  61. StringBuffer strb = new StringBuffer();
  62. int i = 0;
  63.  
  64. while(total-- >= 0){
  65.  
  66.  
  67. strb = new StringBuffer();
  68. for(lines = 0;lines < linex && lines < array.length ;lines++)
  69. strb.append( array[lines]+"@");
  70. t.setString(strb.toString());
  71.  
  72. book.append(t,landscape, 1);
  73.  
  74. System.out.println ("content to print"+t.getString());
  75. // i+=lines;
  76.  
  77.  
  78. }
  79.  
  80.  
  81. System.out.println (book.getNumberOfPages());
  82.  
  83. job.setPageable(book);
  84.  
  85. if(job.printDialog()){
  86.  
  87.  
  88. try {
  89.  
  90. job.print();
  91.  
  92. }catch(Exception e){
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97.  
  98.  
  99. }
  100. }
  101.  
  102. class test implements Printable{
  103.  
  104. private String str;
  105. private boolean isPrinted;
  106.  
  107.  
  108. public void setString(String str){
  109. this.str = str;
  110. }
  111.  
  112. public String getString(){
  113. return this.str;
  114. }
  115.  
  116. public int print(Graphics g,PageFormat p,int index){
  117. int count = 0;
  118. System.out.println("print "+index);
  119. if(!isPrinted) {
  120. isPrinted = true;
  121. return Printable.PAGE_EXISTS;
  122. }
  123.  
  124.  
  125.  
  126. try {
  127.  
  128. if(index >= 2){
  129.  
  130. return Printable.NO_SUCH_PAGE;
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. int y = 10;
  138. int i=0;
  139. if(str == null) {
  140. System.out.println ("NO_SUCH_PAGE");
  141. return Printable.NO_SUCH_PAGE;
  142. }
  143.  
  144.  
  145.  
  146. String[] s= str.split("@");
  147.  
  148. System.out.println ("====>"+str);
  149.  
  150.  
  151. while( i < s.length && s[i] != null ) {
  152.  
  153.  
  154. drawGraphics(g,p,str,y);
  155. y+=10;
  156. i++;
  157. }
  158.  
  159.  
  160. }catch(Exception e){
  161. e.printStackTrace();
  162. }
  163. return Printable.PAGE_EXISTS;
  164. }
  165.  
  166.  
  167. public void drawGraphics(Graphics g,PageFormat p,String str,int y){
  168.  
  169. System.out.println ("upto drawGraphics");
  170. System.out.println ("**"+str);
  171. g.drawString(str,75,y);
  172. g.drawString("hey upto here",75,y+=5);
  173.  
  174.  
  175. }
  176. }
=====================================================
can anybdy help me in this thing
thankx in advance )
Last edited by alc6379; Mar 1st, 2005 at 11:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC