我如何从java程序中打印文本? (JAVA)

时间:2011-08-26 00:38:25

标签: java text printing

我甚至不知道这是否可行,我对此表示高度怀疑,但如果可以的话,请告诉我如何? 我只是想知道如何从打印机打印一些文本。

有什么想法吗?

4 个答案:

答案 0 :(得分:8)

这是更容易的事情。

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();

    }
}

输出:弹出

enter image description here

答案 1 :(得分:3)

您可以做的是写入文件,然后您可以使用Desktop类进行打印。有关桌面课程的详细信息,请转到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);

}

}

答案 2 :(得分:2)

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) {
    }
  }
}

答案 3 :(得分:-1)

关注Man o War和bmargulies的评论,请查看Java Tutorial Printing LessonJava Print API

(主要是发布这个答案,试图让这个问题得到一些额外的关注/竞争性答案,或者至少,只是将其从未解决的问题列表中删除。)