How would I go about printing text from a java program? (Java)

后端 未结 4 1838
鱼传尺愫
鱼传尺愫 2021-01-16 05:38

I don\'t even know if this is possible and I highly doubt it is, but if you can, can you please tell me how? I just want to know how to print some text from a printer.

相关标签:
4条回答
  • 2021-01-16 06:09

    Following Man o War's and bmargulies' comments, take a look at the Java Tutorial Printing Lesson and the Java Print API.

    (Primarily posting this answer in an attempt to either get this question some additional attention / competitive answers, or at the least, to simply remove this from the growing list of unanswered questions.)

    0 讨论(0)
  • 2021-01-16 06:14
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    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.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    
    import javax.print.event.PrintJobAdapter;
    import javax.print.event.PrintJobEvent;
    
    public class PrintText {
    
      public static void main(String[] args) throws PrintException, IOException {
    
        String defaultPrinter = 
          PrintServiceLookup.lookupDefaultPrintService().getName();
        System.out.println("Default printer: " + defaultPrinter);
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    
        // prints the famous hello world! plus a form feed
        InputStream is = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
    
        PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));
    
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc doc = new SimpleDoc(is, flavor, null);
        DocPrintJob job = service.createPrintJob();
    
        PrintJobWatcher pjw = new PrintJobWatcher(job);
        job.print(doc, pras);
        pjw.waitForDone();
        is.close();
      }
    }
    
    class PrintJobWatcher {
      boolean done = false;
    
      PrintJobWatcher(DocPrintJob job) {
        job.addPrintJobListener(new PrintJobAdapter() {
          public void printJobCanceled(PrintJobEvent pje) {
            allDone();
          }
          public void printJobCompleted(PrintJobEvent pje) {
            allDone();
          }
          public void printJobFailed(PrintJobEvent pje) {
            allDone();
          }
          public void printJobNoMoreEvents(PrintJobEvent pje) {
            allDone();
          }
          void allDone() {
            synchronized (PrintJobWatcher.this) {
              done = true;
              System.out.println("Printing done ...");
              PrintJobWatcher.this.notify();
            }
          }
        });
      }
      public synchronized void waitForDone() {
        try {
          while (!done) {
            wait();
          }
        } catch (InterruptedException e) {
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-16 06:20

    What you can do is write to a file and then you can use Desktop class to print it. for more infomation on desktop class go here

    here is the program
    
    
    import java.awt.Desktop;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class abc {
    
    
        public static void main(String[] args) throws IOException{
    
            BufferedWriter out = new BufferedWriter(new FileWriter("1.txt"));
            out.write("Hello this is a test");
            out.flush();
            out.close();
    
            File ff = new File("1.txt");
    
            Desktop desktop = Desktop.getDesktop();
          desktop.print(ff);
    
    }
    

    }

    0 讨论(0)
  • 2021-01-16 06:27

    Here is something that is even easier.

    import javax.swing.JTextPane;
    import java.awt.print.PrinterException;
    
    public class TestPrint {
    
    
        public static void main(String[] args) throws PrinterException {
    
            JTextPane textPane = new JTextPane();
    
            textPane.setText("test text string - Hello World! Are you there?");
    
            textPane.print();
    
        }
    }
    

    Output: popup

    0 讨论(0)
提交回复
热议问题