aspose file tools*
The moose likes Java in General and the fly likes Using printer to print string object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using printer to print string object" Watch "Using printer to print string object" New topic
Author

Using printer to print string object

Ashish Chopra
Ranch Hand

Joined: Nov 30, 2004
Posts: 134
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");
}
}


Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Oscar Reitsma
Greenhorn

Joined: Dec 03, 2004
Posts: 7
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

Joined: Nov 30, 2004
Posts: 134
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using printer to print string object