• 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

Using printer to print string object

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the following code snippet to print the contents of a string on paper.however all the text that is present in the string is being printed in a single line(the string is formatted and \r is to go to a new line)by the printer. Can someone tell me how to retain the formatting???

//code snippet

import java.awt.*;
import java.awt.print.*;
import javax.print.attribute.standard.*;
import javax.print.*;
import javax.print.attribute.*;


public class SimplePrint implements Printable {

private String stringToPrint;

//Creating the constructor. We are passing
//string (that is to b printed) as parameter

public SimplePrint(String stringToPrint) {
this.stringToPrint = stringToPrint;
}

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
g.setFont(new Font("Arial", Font.PLAIN, 11));
g.setColor(Color.black);
g.drawString(stringToPrint, 100, 100);

return Printable.PAGE_EXISTS;
}

public void PrintReport() {

// Look up all services



// Look up the default print service
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
PrintService designatedService = null;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

// Find a particular service by name;
// in this case "HP LaserJet 6MP PS"
AttributeSet aset = new HashAttributeSet();
// aset.add(new PrinterName("\\\\GPNT43\\INET_1", null));

String DESINATED_PRINTER = "\\\\GPNT43\\INET_1";
// Find all services that support a set of print job capabilities;
// in this case, color
aset = new HashAttributeSet();
aset.add(ColorSupported.SUPPORTED);


for (int i = 0 ; i < printServices.length; i++)
{
System.out.println(" service found " + printServices[i].getName());
if (printServices[i].getName().equalsIgnoreCase(DESINATED_PRINTER))
{
System.out.println("I want this one: " + printServices[i].getName());
designatedService = printServices[i];
break;
}
}



// Get a PrintJob
PrinterJob pj = PrinterJob.getPrinterJob();
try {
pj.setPrintService(designatedService) ;
}
catch(PrinterException e)
{}
Printable painter;

// Specify the painter
painter = new SimplePrint(stringToPrint);
pj.setPrintable(painter);



// Show the print dialog
if (pj.printDialog()) {
try {
pj.print();

} catch (PrinterException pe) {
System.out.println("Exception while printing.\n");
pe.printStackTrace();
}
}

//new PrintPreview(painter, "Print Preview - SimplePrint");
}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether this is the best way to do it, but it will hopefully work:

String[] aLines = stringToPrint.split("\r");
int yStart = 100;
FontMetrics fm = g.getFontMetrics();
for (int i=0; i < aLines.length; i++)
{
g.drawString(aLines[i], 100, yStart);
yStart += fm.getHeight();
}
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the way that you have suggested works only when i give a \r delimiter at the end of every line. however the problem persists in case the contents of the line exceed the line width. In that case the printing continues on the same line. Is there any way to avoid this?
reply
    Bookmark Topic Watch Topic
  • New Topic