aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How can I send text characters to a printer device ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I send text characters to a printer device ?" Watch "How can I send text characters to a printer device ?" New topic
Author

How can I send text characters to a printer device ?

Antoine Maranghi
Greenhorn

Joined: Feb 28, 2006
Posts: 5
If I want to write text characters to a file, I can try the following:

BufferedWriter out = new BufferedWriter(new FileWriter("myFile.txt"));
out.write(line1);
out.write(line2);
out.write(line3);
(...)
out.write(line999);
out.flush();
out.close();


What should I change in the above lines in order to write streams of characters to a printer device instead of a file?
Please, keep in mind that I could need to print more than one page of text characters.

Thanks
Roberto
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Welcome to the JavaRanch, Roberto.
I've seen code like this to directly print text to a printer:

It depends on the operating system providing a file-like service for the printer port. I've never tried it.
Keep in mind that PrintWriter has nothing to do with printers, it provides a convenient interface for printing text to a file.
If that code doesn't work or you want to do anything more advanced than printing text, you'll have to use the Java Print Service


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Antoine Maranghi
Greenhorn

Joined: Feb 28, 2006
Posts: 5
If that code doesn't work or you want to do anything more advanced than printing text, you'll have to use the Java Print Service[/QB]


Hi Joe,
unfortunately the code you posted does not print anything.
As for Java Print Service, I tried that but you cannot print a text file with that, you can print the "image" of a text file: any line which is longer than expected is simply cut away.

I would like to print a text file, just a text file, but all of it.
I start to believe that this is beyond the Java capabilities as far as I haven't found anything really working.

I hope to be wrong.

Roberto
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Roberto Regazzoni:

unfortunately the code you posted does not print anything.

This may be obvious, but is your printer attached to LPT1?

Originally posted by Roberto Regazzoni:

As for Java Print Service, I tried that but you cannot print a text file with that, you can print the "image" of a text file: any line which is longer than expected is simply cut away.

Perhaps you missed this example in the link I gave you, which demonstrates printing a text document.
Antoine Maranghi
Greenhorn

Joined: Feb 28, 2006
Posts: 5
Originally posted by Joe Ess:

Perhaps you missed this example in the link I gave you, which demonstrates printing a text document.


Hi Joe,
I had already tried that. Below you can find my version trying to print myFile.txt to the default printer.
The problem with this solution is that if a line is, for instance, 300 characters long, only a portion of the line will be printed and the rest will be cut away.


As for the port LPT1, the answer is no. My default printer is our company network printer and is not on the port LPT1. Do you mean that in Java you can print only to LPT1 ?

Thanks for your help.

Roberto



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.Sides;

public class TestPage9
{
// Input the file
public TestPage9()
{

// Input the file
FileInputStream textStream = null;
try
{
textStream = new FileInputStream("myFile.txt");
}
catch (FileNotFoundException ffne)
{}
if (textStream == null)
{
System.out.println("File not found");
return;
}
// Set the document type
DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;


// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add( new Copies(1) );
aset.add( OrientationRequested.LANDSCAPE );
aset.add( PrintQuality.HIGH );
aset.add( MediaSizeName.ISO_A4 );
aset.add( Sides.ONE_SIDED );

// Create a Doc
Doc myDoc = new SimpleDoc(textStream, myFormat, null);

// discover the printers that can print the format according to the
// instructions in the attribute set

PrintService services = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("DEFAULT PRINTER= " + services);



//PrintService[] services = PrintServiceLookup.lookupPrintServices(myFormat, aset);
if (! services.equals(null)) // Create a print job from one of the print services
{
DocPrintJob job = services.createPrintJob();
try
{
job.print(myDoc, aset);
System.out.println("aset = " + aset.toString());
System.out.println("aset = " + aset.get(OrientationRequested.class));
System.out.println("aset = " + aset.get(PrintQuality.class));
System.out.println("aset = " + aset.get(MediaSizeName.class));
System.out.println("aset = " + aset.get(Sides.class));
//System.out.println("STAMPA IN CORSO");
}
catch (PrintException pe)
{
System.out.println("No connection to the printer " + services);
pe.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
System.out.println("No printer available");

} // fine costruttore

private void docFlavorsSupportati(PrintService services)
{
DocFlavor arrayDocFlavor[] = services.getSupportedDocFlavors();
for(int i=0; i<arrayDocFlavor.length; i++)
{
System.out.println("\nElemento numero " + i);
System.out.println("getMediaSubtype() " + arrayDocFlavor[i].getMediaSubtype() );
System.out.println("getMediaType() " + arrayDocFlavor[i].getMediaType() );
System.out.println("getMimeType() " + arrayDocFlavor[i].getMimeType() );
System.out.println("getRepresentationClassName() " + arrayDocFlavor[i].getRepresentationClassName() );
System.out.println("toString() " + arrayDocFlavor[i].toString() );
System.out.println("getClass() " + arrayDocFlavor[i].getClass() );
}

}

public static void main(String args[])
{
new TestPage9();

}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Roberto Regazzoni:

The problem with this solution is that if a line is, for instance, 300 characters long, only a portion of the line will be printed and the rest will be cut away.

Ah, I understand your issue now. I've only worked with printing Swing controls and there one has to measure the length of a String formatted in a particular font against the printable area of a page and line-wrap where appropriate. You will probably need to create a temp file, wrapping long lines where needed and print that file.

Originally posted by Roberto Regazzoni:

As for the port LPT1, the answer is no. My default printer is our company network printer and is not on the port LPT1. Do you mean that in Java you can print only to LPT1 ?

No. The snippet of code I gave you declares LPT1 (i.e. the local printer port). I don't know how to address a network printer as a file.
I'm going to move this question to the Swing forum to see if anyone over there has better insight as to printing and line wrapping.
[ March 01, 2006: Message edited by: Joe Ess ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I send text characters to a printer device ?