• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Printing with Java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic