This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing - Fit to Page

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I have some applications that I want to enable users to do print screens on. I have been able to get the screen to print, however part of the screen gets cut off when printing on 8-1/2 x 11. Is there any way that I can automatically tell it to "Fit to Page" when printing? This is currently what I'm using to print:



Another thing.... I got this code from the web, but I don't think I really understand it. At what point does the paint(Grahpics g,PageFormat pf,int pageIndex) method ever get called? I had put a System.out.println in this method and it never showed up when I ran the program.
Thanks for any help!
[ June 27, 2008: Message edited by: Jennifer Sohl ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jennifer Sohl:
Hi there. I have some applications that I want to enable users to do print screens on. I have been able to get the screen to print, however part of the screen gets cut off when printing on 8-1/2 x 11. Is there any way that I can automatically tell it to "Fit to Page" when printing? This is currently what I'm using to print:

...

Another thing.... I got this code from the web, but I don't think I really understand it. At what point does the paint(Grahpics g,PageFormat pf,int pageIndex) method ever get called? I had put a System.out.println in this method and it never showed up when I ran the program.
Thanks for any help!

[ June 27, 2008: Message edited by: Jennifer Sohl ]


You need to apply a scale transform on the Graphics. After you call translate, call scale, which takes doubles for x,y that represent the scale factor. You can think of it as a percentage. 1.0 means no change, 2.0 means twice as big, .5 means half size, etc.

You'll have to calculate how much you need to scale. Get the size of you component, compare to the size of your imageable area to compute a ratio, and that will tell you the scale factor. You'll want to keep the same aspect ratio though, so you want to pass the same values for x,y to scale, so just use the smaller of the ratios of the width,height of your component.

The printer job calls your paint() method whenever it needs it to paint a page. It can call it multiple times per page, so don't change any object state in your paint() method.

Printing is done internally on a background thread, so that's why you don't see any println output. It's a pain in the you-know-what to debug printing problems. I've taken to actually using drawString commands, so I can just paint debug messages to the Graphics when I'm having trouble. When debugging I always just do a print-preview from my OS print dialog so I don't waste paper.

Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic