Results 1 to 4 of 4
Thread: printing to printer
- 04-01-2010, 10:02 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 17
- Rep Power
- 0
printing to printer
hi
I am trying to write a java code to print a document file.
I tried some code it compiles and run without exceptions but it prints noting.
In printing device it shows error while printing. I used HP printer.
Can someone helps me out to solve this
I used the code
import java.io.*;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet ;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.CopiesSupported;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.standard.Sides;
public class prit
{
public static void main(String args[])
{
try
{
System.out.println("1");
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("2");
System.out.println("3");
File pdfFile = new File("print.doc");
System.out.println("4");
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
System.out.println("5");
SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(),DocFlavor.URL.AUTOSENSE , null);
System.out.println("6");
printerJob.print(simpleDoc, aset);
System.out.println("7");
}catch(Exception e)
{
System.out.println("error"+e);
}
}
}
- 04-02-2010, 05:59 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Search on the web, I've comment on quite similar questions few weeks ago in the forum.
Read more about java.awt.print.PrinterJob class. That's what all you want. Find more about PrinterJob pj = PrinterJob.getPrinterJob(); means.
- 01-13-2013, 01:53 PM #3
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
Re: printing to printer
have you tried this piece of code it might help you
it prints from a jframe and uses awt.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.print.*; public class PrintUIWindow implements Printable, ActionListener { JFrame frameToPrint; public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { /* We have only one page, and 'page' is zero-based */ return NO_SUCH_PAGE; } /* User (0,0) is typically outside the imageable area, so we must * translate by the X and Y values in the PageFormat to avoid clipping */ Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* Now print the window and its visible contents */ frameToPrint.printAll(g); /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { /* The job did not successfully complete */ } } } public PrintUIWindow(JFrame f) { frameToPrint = f; } public static void main(String args[]) { UIManager.put("swing.boldMetal", Boolean.FALSE); JFrame f = new JFrame("Print UI Example"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JTextArea text = new JTextArea(50, 20); for (int i=1;i<=50;i++) { text.append("Line " + i + "\n"); } JScrollPane pane = new JScrollPane(text); pane.setPreferredSize(new Dimension(250,200)); f.add("Center", pane); JButton printButton = new JButton("Print This Window"); printButton.addActionListener(new PrintUIWindow(f)); f.add("South", printButton); f.pack(); f.setVisible(true); } }
- 01-13-2013, 03:32 PM #4
Re: printing to printer
Do you really think ak88 has been waiting for 3 years? Don't post to old dead threads and unnecessarily bump them to the top of the forum listings, thus pushing the current questions further down.
Two more points:
-- This is a forum, not a code factory. Don't attempt to spoonfeed.
-- Your code doesn't in any way address the problem posted here, which is about the use of the javax.print package and sub-packages, not java.awt.print.
db
THREAD CLOSEDWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Printing Duplex and 4 Copies on HP Printer from Java
By Bozz in forum New To JavaReplies: 4Last Post: 09-10-2009, 01:40 PM -
How to change paper source or printer tray of a printer in java code
By JAVA_ER in forum Advanced JavaReplies: 2Last Post: 02-20-2009, 07:25 AM -
printing simple text as text on printer
By Nicholas Jordan in forum Advanced JavaReplies: 0Last Post: 12-01-2008, 01:42 AM -
Specifying a Network Printer in Printer Servlet
By shapez in forum New To JavaReplies: 0Last Post: 03-06-2008, 03:21 PM -
please help on java program for printer : this printer is connected to system which
By for453 in forum Java 2DReplies: 0Last Post: 08-09-2007, 06:30 AM


LinkBack URL
About LinkBacks


Bookmarks