aspose file tools*
The moose likes Beginning Java and the fly likes Printing on paper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing on paper" Watch "Printing on paper" New topic
Author

Printing on paper

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
I work on two platforms : IBM mainframe and Oracle,PL/Sql,Unix.
On the IBM I (sort of) format data in strings and then write the entire string as a record counting the number of lines written before I explicitly write report headings strings. A control character in the first byte of the record does spaceing and page throws. No GUI or fonts.
Oracle Reports is a 4GL with builtin logic + 'user exits'+ GUI + fonts.

So what ACTUALLY happens in an enterprise Java installation ?
1. Use Java IO - but how to page throw ?
2. Use printf ?
3. "Java's scheme for printing onto paper uses the same graphics model
as used by AWT" (Ian Darwin Java Cookbook)
4. Printing - who's ever done that ?
Please admit to this if it's the case.
5. Finally, what do C programmers do ?
Thank you
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

This hasn't had any traction so far so I will post what little experience I have had...

Originally posted by Graeme Byers:

So what ACTUALLY happens in an enterprise Java installation ?
1. Use Java IO - but how to page throw ?
2. Use printf ?


I have seen people on forums and blogs refer to opening an OutputStream to the printer and feeding bytes to it directly. I haven't seen any code for it, never done it myself, and would imagine this would be for those who are writing Java-based drivers for a particular printer - or know they will print to one printer and know the details about their printer's communications.

Originally posted by Graeme Byers:

3. "Java's scheme for printing onto paper uses the same graphics model
as used by AWT" (Ian Darwin Java Cookbook)


Java has 2 different print schemes, the java.awt.print package and the javax.print package. java.awt.print was developed early on and uses the AWT graphics model to configure printing, as the book says. The javax.print package was developed later (Java 1.4) and uses direct streams to the printer.

I have tried to use the javax.print package on a handful of printers but was never able to print anything but images with it, so I think the usefulness of this package is limited to having a printer that supports it (which apparently common consumer HP, Canon, Epson, and Lexmark printers don't) - or I have completely don't understand how to use the package (which is quite likely). All I can say is there are a small number of tutorials out there for printing using the javax.print package, all of their image examples worked in my hands, none of the text based examples worked in my hands.

Originally posted by Graeme Byers:

4. Printing - who's ever done that ?
Please admit to this if it's the case.


I tried for one project, and as mentioned, got pretty much nowhere. We normally send all text based logs to disk then when report time comes along an external application (not Java) comes along and prints the data.

In my one application I was unable to get javax.print to work or find any kind of library of java drivers for printers so I started down the java.awt.print path, stopped when time constraints forced me to and ended up using existing 3rd party libraries for converting to PDF and printing the PDF. That library worked fine, and I haven't looked into how they do printing, but I suspect it is the AWT model.

My main point in posting this is because I am interested in others experience, sorry for the lack of useful info.


Steve
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
Printing can be tricky. There were/are a number of approaches, which are briefly described in this article.

I've used printing in a few GUI applications, both the 1.1 model and the 1.2 model. Like Pat, I've had problems getting the 1.4 model to work, and eventually gave up, because the older APIs still work fine for me.

Printing is relatively easy to set up for GUI applications, because -apart from setting up some scaffolding- it's done by calling the application's paint (for AWT apps) or paintComponent (for Swing apps) methods. This is a relevant excerpt from one of my applications:


This is an easy case, because this app only ever prints a single page. For multi-page printouts one needs to work with the pageIndex variable in the print method.

A few more examples of this print AP can be found here.
[ August 16, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Thank you for these excellent replies.
Printing on paper is for beginners on IBM and Oracle and then I suddenly realized that it was not so simple on Java.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
Small addendum: If you wanted the implement it the way I outlined above, then you'd need to replace the "getContentPane().paint(g2);" with code that paints onto the g2 Graphics object whatever needs printing on page no. 'pageIndex'.

You probably need to keep track of page breaks yourself (meaning you need to count lines, and make sure that you're not painting more lines onto a page than the paper can fit). The same is probably true for line breaks, if you have long lines.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing on paper