Results 1 to 4 of 4
  1. #1
    ak88 is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Default 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);
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  3. #3
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: printing to printer

    have you tried this piece of code it might help you

    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);
        }
    }
    it prints from a jframe and uses awt.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: printing to printer

    Quote Originally Posted by harshit shah View Post
    have you tried this piece of code it might help you
    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 CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 09-10-2009, 01:40 PM
  2. Replies: 2
    Last Post: 02-20-2009, 07:25 AM
  3. printing simple text as text on printer
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2008, 01:42 AM
  4. Specifying a Network Printer in Printer Servlet
    By shapez in forum New To Java
    Replies: 0
    Last Post: 03-06-2008, 03:21 PM
  5. Replies: 0
    Last Post: 08-09-2007, 06:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •