944,015 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4530
  • Java RSS
Oct 8th, 2009
0

Java Printing only one page problem

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Oct 9th, 2009
0
Re: Java Printing only one page problem
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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 10th, 2009
0
Re: Java Printing only one page problem
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?
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Oct 10th, 2009
0
Re: Java Printing only one page problem
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.
Quote ...
Your saying that the width must be < the height?
No. Proportions gives info about orientation to set up.
Combine.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 26th, 2009
0

Re: Java Printing only one page problem

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bob Walsh is offline Offline
2 posts
since Nov 2009
Jun 1st, 2010
0
Re: Java Printing only one page problem
Printing in Java is a pretty complex thing but if you know the right settings it is very user friendly
Reputation Points: 10
Solved Threads: 1
Newbie Poster
charlesying is offline Offline
23 posts
since Apr 2010
Jul 5th, 2010
0
Re: Java Printing only one page problem
How can we know whether the print job print the page successfully or not(on printer).......?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
software2down is offline Offline
2 posts
since Jul 2010
Jul 5th, 2010
0
Re: Java Printing only one page problem
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Jul 30th, 2010
0

vow

vow vow vow vow
Reputation Points: 10
Solved Threads: 0
Newbie Poster
queen123 is offline Offline
1 posts
since Jul 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Arrays, extracting of coordinates from txt file
Next Thread in Java Forum Timeline: code to download from net works for text files, but not for video.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC