Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default Sending text to printer

    Example below sends the text to the printer for printing. Its a very simple way of doing things.

    Java Code:
    import java.io.*;
    public class SimplePrinting {
      public static void main(String[] args) {
        try {
          FileWriter out = new FileWriter("lpt1");
          out.write("Hello world");
          out.write(0x0D); // CR
          out.close();
          }
        catch (IOException e) {
          e.printStackTrace();
          }
        }
    }

  2. #2
    Deo Favente is offline Member
    Join Date
    Feb 2008
    Posts
    8
    Rep Power
    0

    Default

    With JAVA, this writes to a file called "lpt1". Yes, this is extremely useful. Here's a simple method that takes a string and produces a physical piece of paper with words on it. Better yet it provides a dialog to which the user can select what printer and many (but usually not all) printer settings.

    Java Code:
    PrintingTask.print("Hello World");
    Java Code:
    class PrintingTask extends SwingWorker<Object, Object> {
            private JTextArea text = new JTextArea();
    
            public PrintingTask(String data) {
                text.setText(data);
                this.execute();
            }
    
            public static void print(String data) {
                new PrintingTask(data);
            }
    
            protected Object doInBackground() {
                try {
                    text.print(new MessageFormat(""), new MessageFormat(""), true, null, null, true);
                } catch (PrinterException ex) {
                    //Opps... cant print. Insert error handling code here.
                }
                return null;
            }
    }

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 09:50 AM
  2. How to print text file in java(dotmatrix printer)
    By yoganeethi in forum Advanced Java
    Replies: 4
    Last Post: 12-01-2010, 02:45 PM
  3. Need help with POS printer
    By bkirt in forum Java 2D
    Replies: 13
    Last Post: 08-30-2010, 12:11 AM
  4. Specifying a Network Printer in Printer Servlet
    By shapez in forum New To Java
    Replies: 0
    Last Post: 03-06-2008, 04:21 PM
  5. Replies: 0
    Last Post: 08-09-2007, 07:30 AM

Posting Permissions

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