Forums Register Login

Printing - Fit to Page

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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.
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5612 times.
Similar Threads
Printing TextArea which span more than 1 page
Printing multiple pages!
Printing
Print a JTextArea
Print Preview in Java swing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:17:23.