• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing - AWT/Swing & A4 Paper - RESOLVED  RSS feed

 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm completely new at printing (to a printer!) from Java, and have a couple of questions. I spent yesterday wading through tutorials and the like, which was relatively successful, in that I managed to print what I was trying to print.

I'm trying to print a component of my own, and I've simply made it Printable, and added the print() method. I play around with the Graphics object of the component a bit, to make it fit on one page, and it works just fine. I've only used the AWT stuff, but I saw one reference to the Swing libraries, which was someone advising that you should avoid the AWT libraries, and use Swing instead.

I've looked at Swing, and can't see how I go about actually using it to print something, it all seems to be support-type classes and methods, but maybe I'm missing something. Does anyone have any general printing advice (or just point me towards some documentation - although I've probably seen it!)

My second question, which might be answered as part of the above, is how do I set my default paper size in the Print dialog? It defaults to Letter, but, being in the UK, I'd like it to default to A4. I'm sure that there should be some way of pulling that default from the printer, or from regional settings somewhere, but haven't found anything - perhaps that's in the Swing stuff?

Any general printing help would be much appreciated!

David.
[ March 07, 2006: Message edited by: David Payne ]
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

in which cases the selection of a certain paper size has any effect at all and in which cases it doesn't is a bit complicated. To print in A4 you should set the size of your Paper instance to 394.936 * 841.536 (setSize) in your program, unlikely as this sounds. I don't know how to default it in your print dialog, but as I said: It is questionable if that would even help.

To print your own component you could implement Printable. A good example for a Printable implementation is the DocumentRenderer by Kei G. Gauthier. You probably have a custom paintComponent method already, and much of the code can be used in pretty much the same way in the print method of Printable, which also takes a Graphics reference. Or you could instead find out how to transform your Component into an image and print it (for example the new print API, javax.print, supports printing of images directly). For transforming into an image there are libraries and tutorials.

In most cases it is better to use the new print API, javax.print. What is needed most of the time can be learned from the package description. However, I do not know if in your case it really makes things any easier. The advantage of the new API is that the strange annoyances like this A4 problem are gone (there you specify that you want to use A4 rather than passing in some double values for the page size, and besides the paper size can also be specified in the print dialog and that specified paper format is really used then, rather than ignored most of the time).

Kai
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much, that answered most of my questions, and for anyone else interested, I found a useful tutorial on the javax.print API here:
http://www-128.ibm.com/developerworks/java/library/j-mer0322/

Dave.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!