Link to home
Start Free TrialLog in
Avatar of rspahitz
rspahitzFlag for United States of America

asked on

Printing a string

I'm looking for a "standard" solution to taking String contents from a textbox, asking the user to select a printer, then outputing the string to that printer.

I've tried the code below and it never finds any printers.  Granted, I have none connected to my system, but two are located in my Windows printers list.

DocFlavor objInputFormat = DocFlavor.STRING.TEXT_PLAIN;
Doc objDoc = new SimpleDoc(strXML, objInputFormat, null);

PrintRequestAttributeSet objAttributeSet = new HashPrintRequestAttributeSet();
objAttributeSet.add(new Copies(1));
objAttributeSet.add(Sides.ONE_SIDED);

this.lblStatus.setText("Looking for valid printers...");

PrintService[] services = PrintServiceLookup.lookupPrintServices(objInputFormat, objAttributeSet);

if (services.length > 0)
{
...
} else {
   System.out.println("No printers found.");
}
ASKER CERTIFIED SOLUTION
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Giant2
Giant2

You must select the printer (default) you want to use.

Giant.
...continue.

Use PrinterJob (like doc says).

Bye, Giant.
Avatar of rspahitz

ASKER

PCable, that seems to have worked, although the formatting has much to be desired.  I guess I'll have to spend some tiem figuring the rest out.

Thx.

Giant, not sure what you mean about default printer.  Cable's ref has PrinterJob.printDialog(); which brings up the dialog box, which was a step in the right direction.
Ok, good luck...