File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Printing with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing with Java" Watch "Printing with Java" New topic

Printing with Java

Georg Nieuwoudt

Joined: Nov 02, 2004
Posts: 13
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...

[SCJP 1.4]
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

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?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Here's a pretty straightforward example of how printing works:
Cay Horstmann
Ranch Hand

Joined: Nov 14, 2004
Posts: 137
Go to
and click on the "Download code" link. There are several example programs in the v2/v2ch8 directory (PrintTest, PrintServiceTest, BookTest).



Author of Java 8 for the Really Impatient
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
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.*;

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.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));

// Find all services that support a set of print job capabilities;
// in this case, color
aset = new HashAttributeSet();

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];

// 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]);


// Show the print dialog
if (pj.printDialog()) {
try {
} catch (PrinterException pe) {
System.out.println("Exception while printing.\n");

new PrintPreview(painter, "Print Preview - SimplePrint");
Georg Nieuwoudt

Joined: Nov 02, 2004
Posts: 13
Thanks to everyone of you all... I apreciate the help! Well i got the print together and it finally works, thanks again.
I agree. Here's the link:
subject: Printing with Java
It's not a secret anymore!