| Author |
Printing with Java
|
Georg Nieuwoudt
Greenhorn
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
Bartender
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: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
|
 |
Cay Horstmann
author
Ranch Hand
Joined: Nov 14, 2004
Posts: 77
|
|
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
|
Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482025/ref=jranch-20" target="_blank" rel="nofollow">Core Java 2, Volume I - Fundamentals (7th Edition)</a>
|
 |
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.*; 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
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.
|
 |
 |
|
|
subject: Printing with Java
|
|
|