• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I send text characters to a printer device ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Antoine Maranghi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic