• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing with Java

 
Georg Nieuwoudt
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't sure where to categorize this problem, Hope it's under the correct one...

I read alot bout Print and related classes but i just cannot manage to get it right...

If anyone can send me links, info or example code on the print class and the implementation of i would very much apreciate it...

Pleas dont refer to any doc's examples coz i've done em all and cant make any sence outa them...

Thank You in Advance...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know its very frustrating when you follow docs and it doesn't work, however you might not like them but the Print Service User Guide docs are about as simple as an explanation of this API can be. What have you tried and where do you get stuck?
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a pretty straightforward example of how printing works:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to http://horstmann.com/corejava.html
and click on the "Download code" link. There are several example programs in the v2/v2ch8 directory (PrintTest, PrintServiceTest, BookTest).

Cheers,

Cay
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps.

import java.awt.*;
import java.awt.print.*;


import java.util.Locale;
import javax.print.attribute.standard.*;
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class SimplePrint implements Printable {

private String stringToPrint;

public SimplePrint() {
this("This is the Default String. Go Blues!!!");
}

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("Helvetica", Font.PLAIN, 24));
g.setColor(Color.green);
g.drawString(stringToPrint, 100, 100);

return Printable.PAGE_EXISTS;
}

public static void main(String[] args) {



// 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
if (args.length == 0) {
painter = new SimplePrint();
} else {
painter = new SimplePrint(args[0]);
}

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");
}
}
 
Georg Nieuwoudt
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone of you all... I apreciate the help! Well i got the print together and it finally works, thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic