aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Printing - Fit to Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Printing - Fit to Page" Watch "Printing - Fit to Page" New topic
Author

Printing - Fit to Page

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing - Fit to Page