1

I am trying to test printing from a Java program. I currently have

 Socket s = new Socket(printer_ip, 9100);

 PrintWriter pw = new PrintWriter(s.getOutputStream());

 pw.println("This is a print test");

 pw.println("\n\n\n");

 pw.flush();

 pw.close();

 s.close();

The program does print the text to the printer but it doesn't eject the paper. ?????? What else am I missing?

I thought after flush() it would recognize that the input is done and it should end the stream after the close().

itgeek25
  • 203
  • 2
  • 7
  • 18

2 Answers2

3

PrintWriter is not for printing to a literal printer. It's for writing characters to an output device (usually a console). You should take a look at this tutorial on Java Printing, since it's a little more complex than your code sample

Peter Elliott
  • 3,273
  • 16
  • 30
  • 1
    thanks but I just found what I was looking for too. The following link shows that all I needed was to add a \f to the end of my text. The \f means feed new page. http://stackoverflow.com/questions/8657702/what-do-t-and-b-do/8657733#8657733 thanks for the link too. – itgeek25 Jan 15 '13 at 15:06
1

It sort of depends on the printer that you are using and the interface used to print to it.

The fact that you are able to print at all tells me that your printer is in a mode that prints ASCII text. This is how all of the old tractor fed dot matrix printers worked back in the day, but it's not how modern laser and ink jet printers work. The modern printers print full pages, rather than just lines.

But if you're using an older printer, or an interface that mimics one, you need to do one of two things.

One, you could send the printer a FORMFEED character, which is ASCII code 14 (or Control-L). Many printers will take that character and advance to the next page.

The other technique is to know up front how many lines are on a page, keep track of how many lines are printed, and then when you want to advance the page, simply print that many extra new lines to the printer to advance it line by line.

Most generic printers with standard paper and font sizes, at if you're using 8.5 x 11" paper, are 66 lines per page, or 6 lines per inch. But printer can use different fonts and what not that can affect this. So, check with your printer. An easy test is simply print 100 numbered lines, and see how many are on the page.

So for your example, you printed 4 lines, and you would need to print an extra 62 lines to advance to the next page.

Both techniques have their purposes and uses. The FORMFEED is nice as it's a simple command and you don't have to keep track of anything, but it also means that the printer must "know" how long a page is.

If you use the same printer to print a bunch of different size forms, reseting the printer to the different sizes can be a pain for users to do (printers are not known for having the best user interfaces in the world), so, rather, the computer knows the page sizes and simply advances the lines. This can make form swapping much easier.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203