Java Printing only one page problem

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

Join Date: Oct 2009
Posts: 23
Reputation: AndreiDMS is an unknown quantity at this point 
Solved Threads: 1
AndreiDMS AndreiDMS is offline Offline
Newbie Poster

Java Printing only one page problem

 
0
  #1
Oct 8th, 2009
Hi,
I have a printable class that needs to print some text only on 1 page.
The thing is that when I hit print the printer prints 3 pages:
page 1 - with correct content
page 2 - empty page
page 3 - page with content translated some how
then the printing stops.

Here is the code:
  1. private static final double FACTOR = 2.83;
  2. private double width, height;
  3.  
  4. private void setupJob(){
  5.  
  6. // page width and height
  7. width = 75*FACTOR;
  8. height = 50*FACTOR;
  9.  
  10. job = PrinterJob.getPrinterJob();
  11.  
  12. pf = new PageFormat();
  13. //PageFormat pf =job.pageDialog(job.defaultPage());
  14.  
  15. aset = new HashPrintRequestAttributeSet();
  16.  
  17. // Create a letter sized piece of paper with one inch margins
  18. paper = new Paper();
  19. // resize the paper and the imageable area
  20. paper.setSize(width, height);
  21. paper.setImageableArea(0, 0, width, height);
  22. // set paper for the pageFormat
  23. pf.setPaper(paper);
  24.  
  25. // set the job printable by calling the painter of this object with the pf pageFormat
  26. job.setPrintable(this, pf);
  27. job.setCopies(1);
  28.  
  29. // set the media printable area in mm
  30. aset.add(new MediaPrintableArea( 0f, 0f, 75f, 50f, MediaPrintableArea.MM));
  31.  
  32. try {
  33. job.print(aset);
  34. } catch (PrinterException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. @Override
  40. public int print(Graphics g, PageFormat pf, int page)
  41. throws PrinterException {
  42. if (page > 0) { // We have only one page
  43. return NO_SUCH_PAGE;
  44. }else{
  45.  
  46. Graphics2D g2d = (Graphics2D)g;
  47. // no translation needed
  48. // g2d.translate(11.34f, 0);
  49. // scale to fit page size
  50. double sx = 0, sy = 0;
  51. sx = width/(75*4);
  52. sy = height/(50*4);
  53. double scale = Math.min(sx, sy);
  54. g2d.scale(scale,scale);
  55.  
  56. // draw the elements
  57. for (int i=0; i<4; i++){
  58. texts[i].drawText(g);
  59. }
  60. }
  61. return PAGE_EXISTS;
  62. }

Any help would be appreciated!
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
Oct 9th, 2009
PageFormat.LANDSCAPE ?
check Class PrinterJob , method pageDialog(PageFormat page) : displays page set up dialog.
your width=212.25
height=141.5
Last edited by quuba; Oct 9th, 2009 at 5:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: AndreiDMS is an unknown quantity at this point 
Solved Threads: 1
AndreiDMS AndreiDMS is offline Offline
Newbie Poster
 
0
  #3
Oct 10th, 2009
Thanks,
but I have to setup the page properties without the pageDialog.

With LANDSCAPE orientation the printed text is rotated ...

Your saying that the width must be < the height?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
Oct 10th, 2009
In case LANDSCAPE orientation , how many pages are printing? Three or one?
With 212.25 exceeds 210 A4 - size.
Method pageDialog shows you your printer set up.
Your saying that the width must be < the height?
No. Proportions gives info about orientation to set up.
Combine.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: Bob Walsh is an unknown quantity at this point 
Solved Threads: 0
Bob Walsh Bob Walsh is offline Offline
Newbie Poster

Re: Java Printing only one page problem

 
0
  #5
9 Days Ago
I don't know why it's not working for you. This class definitely allows me to print multi-page files. Are you sure the page is fully loaded before you try to start printing? Could that be your problem
Techstore are specialists in a range of Printing Services.
Reply With Quote Quick reply to this message  
Reply

Tags
java, print, problem

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC